Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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