Skip to content

fix(core): Respect manually set sentry tracing headers in fetch calls #16183

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

Merged
merged 4 commits into from
May 5, 2025

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented May 2, 2025

This PR fixes a bit of a weird fetch instrumentation problem:

If users manually pass in sentry-trace and baggage headers, we previously overwrote the values with the ones we computed. While in #13907 we specifically decided that we want to remove any previously set sentry tracing header values and replace them with the current ones, I think the main motivation was to avoid duplicated header values.

With this PR, we:

  • still check for pre-existing sentry headers, but do nothing if we find them
  • still ensure that we merge pre-existing non-sentry baggage values with our baggage values
  • add a bunch of unit tests for the various header and request types (I feel like I wrote these before but couldn't find em)
  • add an integration test to demonstrate that manually passed in headers are still taken

nice side-effect: slight bundle size reduction :D

came up in https://linear.app/getsentry/issue/FE-380/debug-trace-context-propagation-issue-from-tanstack-to-go-backend

@Lms24 Lms24 changed the title fix(core): Handle manually set in sentry tracing headers in fetch calls fix(core): Respect manually set in sentry tracing headers in fetch calls May 2, 2025
@Lms24 Lms24 self-assigned this May 2, 2025
@Lms24 Lms24 requested a review from lforst May 2, 2025 12:28
Copy link
Contributor

github-actions bot commented May 2, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.32 KB - -
@sentry/browser - with treeshaking flags 23.13 KB - -
@sentry/browser (incl. Tracing) 36.96 KB -0.08% -28 B 🔽
@sentry/browser (incl. Tracing, Replay) 74.14 KB -0.02% -9 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.48 KB -0.02% -13 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 78.79 KB -0.01% -7 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 90.59 KB -0.01% -7 B 🔽
@sentry/browser (incl. Feedback) 39.71 KB - -
@sentry/browser (incl. sendFeedback) 27.94 KB - -
@sentry/browser (incl. FeedbackAsync) 32.7 KB - -
@sentry/react 25.12 KB - -
@sentry/react (incl. Tracing) 38.94 KB -0.05% -17 B 🔽
@sentry/vue 27.55 KB - -
@sentry/vue (incl. Tracing) 38.71 KB -0.05% -18 B 🔽
@sentry/svelte 23.34 KB - -
CDN Bundle 24.52 KB - -
CDN Bundle (incl. Tracing) 36.98 KB -0.03% -11 B 🔽
CDN Bundle (incl. Tracing, Replay) 72.02 KB -0.02% -11 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 77.33 KB -0.03% -19 B 🔽
CDN Bundle - uncompressed 71.57 KB - -
CDN Bundle (incl. Tracing) - uncompressed 109.34 KB -0.06% -66 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.63 KB -0.03% -66 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 233.15 KB -0.03% -66 B 🔽
@sentry/nextjs (client) 40.55 KB -0.02% -8 B 🔽
@sentry/sveltekit (client) 37.43 KB -0.04% -15 B 🔽
@sentry/node 143.45 KB -0.01% -1 B 🔽
@sentry/node - without tracing 96.53 KB -0.01% -1 B 🔽
@sentry/aws-serverless 120.86 KB - -

View base workflow run

@Lms24 Lms24 requested review from mydea and chargome May 2, 2025 12:44
@Lms24 Lms24 changed the title fix(core): Respect manually set in sentry tracing headers in fetch calls fix(core): Respect manually set sentry tracing headers in fetch calls May 2, 2025
@Lms24 Lms24 force-pushed the lms/fix-core-fetch-custom-trace-headers branch from 844a168 to e044661 Compare May 2, 2025 12:59
requests.forEach(request => {
const headers = request.headers();

// No merged sentry trace headers
expect(headers['sentry-trace']).not.toContain(',');

// No multiple baggage entries
expect(headers['baggage'].match(/sentry-trace_id/g) ?? []).toHaveLength(1);
expect(headers['baggage'].match(/sentry-release/g) ?? []).toHaveLength(1);
Copy link
Member Author

@Lms24 Lms24 May 2, 2025

Choose a reason for hiding this comment

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

this seems harmless but it demonstrates the behaviour change for reused request options:

previously, we'd simple disregard what sentry values were in the request options and always overwrite them. Now:

  • we still don't blindly append our values to pre-existing ones
  • but instead, we do nothing and just roll with the passed-in headers.

Result: All requests in this test sharing the same request options have the same sentry tracing headers (and hence their child trace trees have the same trace parent)

I think this is fine but it's still worth noting.

Copy link
Member

@lforst lforst left a comment

Choose a reason for hiding this comment

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

Lgtm. I wonder whether there will ever be a problem because some downstream SDK wants to set a prefixed baggage entry in addition to what the core SDK sets, but it will not work because adding a prefixed baggage entry will just stomp on our logic... Might be worth checking with Krystof

@Lms24
Copy link
Member Author

Lms24 commented May 5, 2025

Hmm I'm wondering what this downstream SDK would have to do to run into a problem here. This change basically prohibits modifying baggage (or sentry-trace) if the headers already exist on the request. My thinking is that if other higher-level SDKs add entries to the DSC (or baggage), they would do that by using Scope.setPropagationContext({dsc: ...}) instead of manually adding headers to baggage 🤔
But it's a good catch. Let me check with Mobile folks

Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

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

Looks good!

@Lms24
Copy link
Member Author

Lms24 commented May 5, 2025

Double checked with @krystofwoldrich - RN doesn't add request headers to fetch requests.

@Lms24 Lms24 enabled auto-merge (squash) May 5, 2025 12:05
@Lms24 Lms24 merged commit 69224b8 into develop May 5, 2025
154 checks passed
@Lms24 Lms24 deleted the lms/fix-core-fetch-custom-trace-headers branch May 5, 2025 12:16
Lms24 added a commit that referenced this pull request May 5, 2025
…ests (#16184)

Analogously to #16183 for `fetch`, this patch ensures that manually set
`sentry-trace` and `baggage` headers in XHR requests are respected by
our XHR instrumentation and not overwritten.

This patch also fixes a bug where we'd append multiple `sentry-trace` or
`baggage` values in case the header was somehow set more than once
(analogously to
#13907 for `fetch`)
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.

3 participants