Skip to content

Conversation

@m0sa
Copy link
Contributor

@m0sa m0sa commented Oct 22, 2025

Which problem is this PR solving?

Fixes incorrect handling of null-body-status responses, and adds tests to ensure this doesn't go un-noticed again.

Fixes #6036

Short description of the changes

Fixing null-body-status response handling in instrumentation-fetch.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Error was reproduced in unit tests and then fixed.

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@m0sa m0sa requested a review from a team as a code owner October 22, 2025 22:47
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Oct 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.14%. Comparing base (46e4841) to head (99e4837).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6037   +/-   ##
=======================================
  Coverage   95.14%   95.14%           
=======================================
  Files         315      316    +1     
  Lines        9161     9166    +5     
  Branches     2022     2023    +1     
=======================================
+ Hits         8716     8721    +5     
  Misses        445      445           

see 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines 406 to 408
it('should be handled correctly', async () => {
assert.strictEqual(response?.status, 204);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other tests make assertions that the spans were emitted rather then asserting on the response. Something like:

   assert.strictEqual(exportedSpans.length, 1);
Suggested change
it('should be handled correctly', async () => {
assert.strictEqual(response?.status, 204);
});
it('204 (No Content) will correctly end the span', async () => {
assert.strictEqual(exportedSpans.length, 1);
});

We also have untested functionality for other null-body content. 101, 205, 304 we could test those as well.

Copy link
Contributor Author

@m0sa m0sa Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added separate test cases for each status code.

I did find a weird edge-case in 101 that IDK if is already handled correctly. I would expect a span to be exported, with the correct error code, but this either doesn't happen in tests due to how it's set up, or it actually wouldn't happen. @wolfgangcodes any ideas?

The fetch.ts endSpanOnError callback does happen in that case, but somehow the span is not there for inspection.

Copy link
Contributor

@wolfgangcodes wolfgangcodes Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding those!

I don't have any immediate thoughts on what might be happening with 101s, but I like your approach of implementing what we can test easily right now for for common success responses.

We can always follow up with better support for 101s if they crop up or if we have more time to think about it!

I approved to show my support, but we'll need add'l approvals from the JS folks.

const reader = body.getReader();

const wrappedBody = withCancelPropagation(response.body, reader);
const isNullBodyStatus =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these statuses are the ones with null body according to a spec. But my experience tells me some apis may not follow these specs and return body anyways. I wonder if it's okay to not process the body in this scenario

Copy link
Contributor Author

@m0sa m0sa Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Response({ /* non-null-wrapped-body */ }, { status: 204 /* or 101, 205, 304 */ }) constructor always throws:

Image

The only way of processing it further would mean avoid reconstructing it / preserving a code path that was in place prior to #5894

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, backends could be written in different languages and those may let the dev pass a payload. According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status we should not expect payloads for these statuses so I'm fine. If there is interest in reading the body even with these cases we can add it later.

@david-luna
Copy link
Contributor

LGTM. I'll wait a bit so others can give feedback

cc: @open-telemetry/javascript-approvers

@david-luna david-luna added this pull request to the merge queue Oct 31, 2025
Merged via the queue into open-telemetry:main with commit fbe0fff Oct 31, 2025
27 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Oct 31, 2025

Thank you for your contribution @m0sa! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@otelbot-js otelbot-js bot mentioned this pull request Nov 3, 2025
maryliag pushed a commit to maryliag/opentelemetry-js that referenced this pull request Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[instrumentation-fetch] Response with null body status cannot have body errors

3 participants