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:
parent
d3f5163267
commit
faca317da1
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue