Skip to content

Commit 3711990

Browse files
fix(instrumentation-fetch): compatibility with Map inputs for request headers with fetch (#4348)
* fix(@opentelemetry-instrumentation-fetch): compatibility with Map inputs for request headers with fetch * Update experimental/CHANGELOG.md Co-authored-by: Marc Pichler <[email protected]> * adding description for ts-ignore * fix(changlog): move entry to unreleased * fix: add lint ignore --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 5afbcdb commit 3711990

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented
2020
* fix(exporter-logs-otlp-grpc): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
2121
* fix(exporter-logs-otlp-http): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
2222
* fix(exporter-logs-otlp-proto): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
23+
* fix(instrumentation-fetch): compatibility with Map types for fetch headers
2324

2425
### :books: (Refine Doc)
2526

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ export class FetchInstrumentation extends InstrumentationBase<
168168
api.propagation.inject(api.context.active(), options.headers, {
169169
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
170170
});
171+
} else if (options.headers instanceof Map) {
172+
api.propagation.inject(api.context.active(), options.headers, {
173+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
174+
});
171175
} else {
172176
const headers: Partial<Record<string, unknown>> = {};
173177
api.propagation.inject(api.context.active(), headers);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ describe('fetch', () => {
503503
assert.ok(r.headers.get('foo') === 'bar');
504504
});
505505

506-
it('should keep custom headers with url, untyped request object and typed headers object', () => {
506+
it('should keep custom headers with url, untyped request object and typed (Headers) headers object', () => {
507507
const url = 'url';
508508
const init = {
509509
headers: new Headers({ foo: 'bar' }),
@@ -521,6 +521,17 @@ describe('fetch', () => {
521521
assert.ok(init.headers['foo'] === 'bar');
522522
});
523523

524+
it('should keep custom headers with url, untyped request object and typed (Map) headers object', () => {
525+
const url = 'url';
526+
const init = {
527+
headers: new Map().set('foo', 'bar'),
528+
};
529+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
530+
// @ts-ignore variable init not of RequestInit type
531+
window.fetch(url, init).catch(() => {});
532+
assert.ok(init.headers.get('foo') === 'bar');
533+
});
534+
524535
it('should pass request object as first parameter to the original function (#2411)', () => {
525536
const r = new Request(url);
526537
return window.fetch(r).then(

0 commit comments

Comments
 (0)