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(instrumentation-fetch): compatibility with Map inputs for request headers with fetch #4348

Merged
merged 9 commits into from
Jan 25, 2024
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ All notable changes to experimental packages in this project will be documented
* fix(instrumentation-fetch): only access navigator if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063)
* allows for experimental usage of this instrumentation with non-browser runtimes
* fix(instrumentation-http): memory leak when responses are not resumed
* fix(instrumentation-fetch): compatibility with Map types for fetch headers
rdeavila94 marked this conversation as resolved.
Show resolved Hide resolved
* fix(instrumentation-http): Do not mutate given headers object for outgoing http requests. Fixes aws-sdk signing error on retries. [#4346](https://github.com/open-telemetry/opentelemetry-js/pull/4346)
* fix(instrumentation): support Node.js v18.19.0 by using import-in-the-middle@1.6.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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 Map) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ describe('fetch', () => {
assert.ok(r.headers.get('foo') === 'bar');
});

it('should keep custom headers with url, untyped request object and typed headers object', () => {
it('should keep custom headers with url, untyped request object and typed (Headers) headers object', () => {
const url = 'url';
const init = {
headers: new Headers({ foo: 'bar' }),
Expand All @@ -521,6 +521,16 @@ describe('fetch', () => {
assert.ok(init.headers['foo'] === 'bar');
});

it('should keep custom headers with url, untyped request object and typed (Map) headers object', () => {
const url = 'url';
const init = {
headers: new Map().set('foo', 'bar'),
};
/** @ts-ignore variable init not of RequestInit type*/
window.fetch(url, init).catch(() => {});
assert.ok(init.headers.get('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
Loading