Skip to content

Commit dbfabd2

Browse files
fix(opentelemetry-instrumentation-fetch): fixed override of headers (#2426)
* fix(opentelemetry-instrumentation-fetch): fixed override of headers Signed-off-by: Philip Szalla <[email protected]> * style(opentelemetry-instrumentation-fetch): removed irritating comment Signed-off-by: Philip Szalla <[email protected]> * test(opentelemetry-instrumentation-fetch): added tests for custom headers Signed-off-by: Philip Szalla <[email protected]> Co-authored-by: Daniel Dyla <[email protected]>
1 parent c69251e commit dbfabd2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/opentelemetry-instrumentation-fetch/src/fetch.ts

+4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ export class FetchInstrumentation extends InstrumentationBase<
158158
api.propagation.inject(api.context.active(), options.headers, {
159159
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
160160
});
161+
} else if(options.headers instanceof Headers) {
162+
api.propagation.inject(api.context.active(), options.headers, {
163+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
164+
});
161165
} else {
162166
const headers: Partial<Record<string, unknown>> = {};
163167
api.propagation.inject(api.context.active(), headers);

packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,32 @@ describe('fetch', () => {
535535
assert.ok(typeof r.headers.get(X_B3_TRACE_ID) === 'string');
536536
});
537537

538+
it('should keep custom headers with a request object and a headers object', () => {
539+
const r = new Request('url', {
540+
headers: new Headers({'foo': 'bar'})
541+
});
542+
window.fetch(r).catch(() => {});
543+
assert.ok(r.headers.get('foo') === 'bar');
544+
});
545+
546+
it('should keep custom headers with url, untyped request object and typed headers object', () => {
547+
const url = 'url';
548+
const init = {
549+
headers: new Headers({'foo': 'bar'})
550+
};
551+
window.fetch(url, init).catch(() => {});
552+
assert.ok(init.headers.get('foo') === 'bar');
553+
});
554+
555+
it('should keep custom headers with url, untyped request object and untyped headers object', () => {
556+
const url = 'url';
557+
const init = {
558+
headers: {'foo': 'bar'}
559+
};
560+
window.fetch(url, init).catch(() => {});
561+
assert.ok(init.headers['foo'] === 'bar');
562+
});
563+
538564
it('should pass request object as first parameter to the original function (#2411)', () => {
539565
const r = new Request(url);
540566
return window.fetch(r).then(() => {

0 commit comments

Comments
 (0)