Skip to content

fix(core): Improve waiting for tracing channel bindings#21815

Open
mydea wants to merge 7 commits into
developfrom
fn/tracing-channel-bindings
Open

fix(core): Improve waiting for tracing channel bindings#21815
mydea wants to merge 7 commits into
developfrom
fn/tracing-channel-bindings

Conversation

@mydea

@mydea mydea commented Jun 26, 2026

Copy link
Copy Markdown
Member

We rely on the async local storage instance for the tracing channel bindings.

Previously, this was picked up from the otel context manager (in otel-mode). However, this is only setup after the integrations run, leading to a race condition where this was not available yet.

The "fix" for this we used so far was to setup the channel-based listeners in the next tick via Promise.resolve(). Apart from being a bit hacky, this also has a concrete problem: If code runs synchrounsly after Sentry.init(), this could be unhandled by our integration (this was surfaced when moving vercel ai v6 to use orchestrion, where this failed in CJS).

So this PR changes this a bit, so that in the happy path where we use our own context manager, we can use a consistent instance of async local storage even before the manager is setup. In this case, we can setup the integrations in a sync way and everything just works.

If users use a custom context manager, this will/may not work though. So in this case we continue to use the lookup from the context manager (this can be removed in v11). Since this means we have the same problems as before, I added a small utility waitForTracingChannelBinding to abstract this away - this will try to see if this is setup and if so, exectute the provided callback synchronously. Else, it will wait a tick and try again (mirroring what we had before).

While at this, I also cleaned up internal exports from core a bit - one of them can be reproduced with more public APIs already, we just never exported getAsyncContextStrategy from core but this is fine to export IMHO.

Comment thread packages/core/src/asyncContext/tracing-channel-binding.ts Outdated
@github-actions

github-actions Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.47 kB - -
@sentry/browser - with treeshaking flags 25.91 kB - -
@sentry/browser (incl. Tracing) 45.97 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.72 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.76 kB - -
@sentry/browser (incl. Tracing, Replay) 85.22 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.81 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.91 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.57 kB - -
@sentry/browser (incl. Feedback) 44.66 kB - -
@sentry/browser (incl. sendFeedback) 32.26 kB - -
@sentry/browser (incl. FeedbackAsync) 37.4 kB - -
@sentry/browser (incl. Metrics) 28.54 kB - -
@sentry/browser (incl. Logs) 28.78 kB - -
@sentry/browser (incl. Metrics & Logs) 29.47 kB - -
@sentry/react 29.27 kB - -
@sentry/react (incl. Tracing) 48.28 kB - -
@sentry/vue 32.63 kB - -
@sentry/vue (incl. Tracing) 47.84 kB - -
@sentry/svelte 27.5 kB - -
CDN Bundle 29.89 kB +0.01% +1 B 🔺
CDN Bundle (incl. Tracing) 47.9 kB +0.02% +5 B 🔺
CDN Bundle (incl. Logs, Metrics) 31.44 kB +0.02% +5 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 49.24 kB +0.02% +6 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 70.77 kB -0.02% -10 B 🔽
CDN Bundle (incl. Tracing, Replay) 85.41 kB +0.02% +12 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.68 kB +0.01% +3 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 91.2 kB +0.02% +15 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.45 kB +0.01% +5 B 🔺
CDN Bundle - uncompressed 88.95 kB +0.01% +5 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 145.02 kB -0.01% -3 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.65 kB +0.01% +6 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 149 kB -0.01% -2 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.63 kB +0.01% +2 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.04 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 268 kB -0.01% -7 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 277.74 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 281.69 kB -0.01% -7 B 🔽
@sentry/nextjs (client) 50.67 kB - -
@sentry/sveltekit (client) 46.37 kB - -
@sentry/core/server 77.6 kB +0.08% +57 B 🔺
@sentry/core/browser 63.92 kB +0.07% +43 B 🔺
@sentry/node-core 61.44 kB -0.12% -72 B 🔽
@sentry/node 123.44 kB -0.04% -41 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.95 kB - -
@sentry/node/light 50.4 kB -0.01% -1 B 🔽
@sentry/node - without tracing 73.56 kB -0.07% -48 B 🔽
@sentry/aws-serverless 84.36 kB -0.05% -37 B 🔽
@sentry/cloudflare (withSentry) - minified 180.31 kB - -
@sentry/cloudflare (withSentry) 446.3 kB +0.02% +68 B 🔺

View base workflow run

Comment thread packages/opentelemetry/src/nodeAsyncContextStrategy.ts Outdated
Comment thread packages/opentelemetry/src/index.ts
@mydea mydea requested a review from logaretm June 26, 2026 11:50
@mydea mydea marked this pull request as ready for review June 26, 2026 11:50
@mydea mydea requested a review from a team as a code owner June 26, 2026 11:50
@mydea mydea requested review from JPeer264 and andreiborza and removed request for a team June 26, 2026 11:50

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 113ee44. Configure here.

Comment thread packages/vercel-edge/src/sdk.ts Outdated
setOpenTelemetryContextAsyncContextStrategy();
// We force skipOpenTelemetrySetup: true here, because this triggers the custom lookup for the AsyncLocalStorage instance
// Since we use a custom Context Manager here (because AsyncLocalStorage is looked up differently than in Node), we need to do this
setOpenTelemetryContextAsyncContextStrategy({ skipOpenTelemetrySetup: true });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The vercel-edge SDK calls setOpenTelemetryContextAsyncContextStrategy with skipOpenTelemetrySetup: true, but the browser bundle now exports a version that silently ignores this option, breaking async context setup.
Severity: HIGH

Suggested Fix

The vercel-edge SDK needs to be updated to work with the new async context strategy. Instead of relying on the skipOpenTelemetrySetup option, it should be adapted to the new mechanism for setting up the async local storage instance, possibly by directly providing a getTracingChannelBinding function. Alternatively, ensure the vercel-edge package always resolves the Node.js bundle of @sentry/opentelemetry if that is the intended behavior.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/vercel-edge/src/sdk.ts#L68

Potential issue: The `vercel-edge` SDK calls
`setOpenTelemetryContextAsyncContextStrategy({ skipOpenTelemetrySetup: true })` to
enable a custom context manager. However, recent changes mean that when the browser
bundle of `@sentry/opentelemetry` is used, this function resolves to a generic version
that no longer accepts the `skipOpenTelemetrySetup` option. The option is silently
ignored, which prevents the custom `getTracingChannelBinding` from being configured.
This breaks the async context strategy for the Vercel Edge environment, as it relies on
this specific setup path for proper tracing.

Also affects:

  • packages/opentelemetry/src/index.browser.ts:15~17

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess this is fine either way, as DC stuff does not work in vercel-edge anyhow so this is fine if it noops? cc @logaretm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As long as we guard in the instrumentations, that should be fine. The bind function doesn't import tracing channels in runtime, so the integrations should noop.

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.

2 participants