Skip to content

Commit

Permalink
fix(opentelemetry-instrumentation-fetch): fixed override of headers (#…
Browse files Browse the repository at this point in the history
…2426)

* fix(opentelemetry-instrumentation-fetch): fixed override of headers

Signed-off-by: Philip Szalla <philip@szalla.de>

* style(opentelemetry-instrumentation-fetch): removed irritating comment

Signed-off-by: Philip Szalla <philip@szalla.de>

* test(opentelemetry-instrumentation-fetch): added tests for custom headers

Signed-off-by: Philip Szalla <philip@szalla.de>

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
philipszalla and dyladan committed Aug 27, 2021
1 parent c69251e commit dbfabd2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Expand Up @@ -158,6 +158,10 @@ export class FetchInstrumentation extends InstrumentationBase<
api.propagation.inject(api.context.active(), options.headers, {
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
});
} else if(options.headers instanceof Headers) {
api.propagation.inject(api.context.active(), options.headers, {
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
});
} else {
const headers: Partial<Record<string, unknown>> = {};
api.propagation.inject(api.context.active(), headers);
Expand Down
26 changes: 26 additions & 0 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Expand Up @@ -535,6 +535,32 @@ describe('fetch', () => {
assert.ok(typeof r.headers.get(X_B3_TRACE_ID) === 'string');
});

it('should keep custom headers with a request object and a headers object', () => {
const r = new Request('url', {
headers: new Headers({'foo': 'bar'})
});
window.fetch(r).catch(() => {});
assert.ok(r.headers.get('foo') === 'bar');
});

it('should keep custom headers with url, untyped request object and typed headers object', () => {
const url = 'url';
const init = {
headers: new Headers({'foo': 'bar'})
};
window.fetch(url, init).catch(() => {});
assert.ok(init.headers.get('foo') === 'bar');
});

it('should keep custom headers with url, untyped request object and untyped headers object', () => {
const url = 'url';
const init = {
headers: {'foo': 'bar'}
};
window.fetch(url, init).catch(() => {});
assert.ok(init.headers['foo'] === 'bar');
});

it('should pass request object as first parameter to the original function (#2411)', () => {
const r = new Request(url);
return window.fetch(r).then(() => {
Expand Down

0 comments on commit dbfabd2

Please sign in to comment.