feat(@opentelemetry-instrumentation-fetch): support reading response body from the hook applyCustomAttributesOnSpan (#2497)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
This commit is contained in:
echoontheway 2021-10-09 22:10:46 +08:00 committed by GitHub
parent d3f5163267
commit faca317da1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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();
}

View File

@ -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', () => {