Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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