Skip to content

Commit

Permalink
feat(@opentelemetry-instrumentation-fetch): support reading response …
Browse files Browse the repository at this point in the history
…body from the hook applyCustomAttributesOnSpan
  • Loading branch information
echoontheway committed Sep 26, 2021
1 parent 6a78db1 commit 3fe7175
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,15 @@ 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();
const read = (): void => {
reader.read().then(
({ done }) => {
if (done) {
endSpanOnSuccess(span, response);
endSpanOnSuccess(span, resClone4Hook);
} else {
read();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3fe7175

Please sign in to comment.