Skip to content

Commit fbe0fff

Browse files
m0sawolfgangcodes
andauthored
fix(instrumentation-fetch): Handling null-body-status responses (#6037)
Co-authored-by: Wolfgang Therrien <[email protected]>
1 parent e01e48d commit fbe0fff

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
2727

2828
### :bug: Bug Fixes
2929

30+
* fix(instrumentation-fetch): Handling null-body-status responses [#6037](https://github.com/open-telemetry/opentelemetry-js/pull/6037) @m0sa
31+
3032
### :books: Documentation
3133

3234
### :house: Internal

experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,14 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
507507
const body = resClone.body;
508508
if (body) {
509509
const reader = body.getReader();
510-
511-
const wrappedBody = withCancelPropagation(response.body, reader);
510+
const isNullBodyStatus =
511+
// 101 responses and protocol upgrading is handled internally by the browser
512+
response.status === 204 ||
513+
response.status === 205 ||
514+
response.status === 304;
515+
const wrappedBody = isNullBodyStatus
516+
? null
517+
: withCancelPropagation(response.body, reader);
512518

513519
proxiedResponse = new Response(wrappedBody, {
514520
status: response.status,

experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ describe('fetch', () => {
369369
msw.http.get('/boom', () => {
370370
return new msw.HttpResponse(null, { status: 500 });
371371
}),
372+
msw.http.get('/null-body-204', () => {
373+
return new msw.HttpResponse(null, { status: 204 });
374+
}),
375+
msw.http.get('/null-body-205', () => {
376+
return new msw.HttpResponse(null, { status: 205 });
377+
}),
378+
msw.http.get('/null-body-304', () => {
379+
return new msw.HttpResponse(null, { status: 304 });
380+
}),
372381
],
373382
callback = () => fetch('/api/status.json'),
374383
config = {},
@@ -391,6 +400,40 @@ describe('fetch', () => {
391400
return { rootSpan, response };
392401
};
393402

403+
describe('null-bodied response', () => {
404+
// https://chromium.googlesource.com/chromium/src/+/ac85ca2a9cb8c76a37f9d7a6c611c24114f1f05d/third_party/WebKit/Source/core/fetch/Response.cpp#106
405+
it('204 (No Content) will correctly end the span', async () => {
406+
await tracedFetch({
407+
callback: () => fetch('/null-body-204'),
408+
});
409+
assert.strictEqual(exportedSpans.length, 1);
410+
assert.strictEqual(
411+
exportedSpans[0].attributes[ATTR_HTTP_STATUS_CODE],
412+
204
413+
);
414+
});
415+
it('205 (Reset Content) will correctly end the span', async () => {
416+
await tracedFetch({
417+
callback: () => fetch('/null-body-205'),
418+
});
419+
assert.strictEqual(exportedSpans.length, 1);
420+
assert.strictEqual(
421+
exportedSpans[0].attributes[ATTR_HTTP_STATUS_CODE],
422+
205
423+
);
424+
});
425+
it('304 (Not Modified) will correctly end the span', async () => {
426+
await tracedFetch({
427+
callback: () => fetch('/null-body-304'),
428+
});
429+
assert.strictEqual(exportedSpans.length, 1);
430+
assert.strictEqual(
431+
exportedSpans[0].attributes[ATTR_HTTP_STATUS_CODE],
432+
304
433+
);
434+
});
435+
});
436+
394437
describe('simple request', () => {
395438
let rootSpan: api.Span | undefined;
396439
let response: Response | undefined;

0 commit comments

Comments
 (0)