diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index 29d52b0943..a564b76f5a 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -331,6 +331,7 @@ export class FetchInstrumentation extends InstrumentationBase< ): void { try { const resClone = response.clone(); + const resClone4Hook = response.clone(); const body = resClone.body; if (body) { const reader = body.getReader(); @@ -338,7 +339,7 @@ export class FetchInstrumentation extends InstrumentationBase< reader.read().then( ({ done }) => { if (done) { - endSpanOnSuccess(span, response); + endSpanOnSuccess(span, resClone4Hook); } else { read(); } diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 1de5b795d0..a38dbd8b67 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -683,6 +683,22 @@ describe('fetch', () => { prepare(url, applyCustomAttributes); }); + + it('get response body from callback arguments response', done => { + const applyCustomAttributes: FetchCustomAttributeFunction = async ( + span, + request, + response + ) => { + if(response instanceof Response ){ + const rsp = await response.json(); + assert.deepStrictEqual(rsp.args, {}); + done(); + } + }; + + prepare(url, applyCustomAttributes); + }); }); describe('when url is ignored', () => {