diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 5d9636d4fa..9f8315a410 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -21,6 +21,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2 * fix(instrumentation-fetch): preserve init overrides when input is a Request object [#6421](https://github.com/open-telemetry/opentelemetry-js/issues/6421) @akandic47 * fix(otlp-exporter-base): limit Node.js HTTP transport response body to 4 MiB [#6552](https://github.com/open-telemetry/opentelemetry-js/pull/6552) @kartikgola +* fix(instrumentation-fetch): avoid unwrapping fetch API when disabling [#6575](https://github.com/open-telemetry/opentelemetry-js/pull/6575) @david-luna * fix(web-common): add check for possible unsafe json parse [#6589](https://github.com/open-telemetry/opentelemetry-js/pull/6589) @maryliag ### :books: Documentation diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index 3d7eb7cb6a..d5eb4a568d 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -15,7 +15,6 @@ import type { InstrumentationConfig } from '@opentelemetry/instrumentation'; import { SemconvStability, semconvStabilityFromStr, - isWrapped, InstrumentationBase, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; @@ -111,6 +110,15 @@ export class FetchInstrumentation extends InstrumentationBase ): Promise { + if (!plugin._isEnabled) { + return original.apply(this, args); + } const self = this; const url = web.parseUrl( args[0] instanceof Request ? args[0].url : String(args[0]) @@ -611,21 +622,31 @@ export class FetchInstrumentation extends InstrumentationBase(); } } diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 47d622265a..8a6cbcf783 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -122,6 +122,7 @@ function waitFor(timeout: number): Promise { } describe('fetch', () => { + const originalFetch = globalThis.fetch; let workerStarted = false; const startWorker = async ( @@ -202,15 +203,18 @@ describe('fetch', () => { ); } finally { sinon.restore(); + globalThis.fetch = originalFetch; } }); describe('enabling/disabling', () => { + // const originalFetch = globalThis.fetch; let fetchInstrumentation: FetchInstrumentation | undefined; afterEach(() => { fetchInstrumentation?.disable(); fetchInstrumentation = undefined; + // globalThis.fetch = originalFetch; }); it('should wrap global fetch when instantiated', () => { @@ -227,11 +231,11 @@ describe('fetch', () => { assert.ok(isWrapped(window.fetch)); }); - it('should unwrap global fetch when disabled', () => { + it('should not unwrap global fetch when disabled', () => { fetchInstrumentation = new FetchInstrumentation(); assert.ok(isWrapped(window.fetch)); fetchInstrumentation.disable(); - assert.ok(!isWrapped(window.fetch)); + assert.ok(isWrapped(window.fetch)); // Avoids ERROR in the logs when calling `disable()` again during cleanup fetchInstrumentation = undefined;