◐ Shell
reader mode source ↗
Skip to content

fix(core): Defer TwP sampling by reading trace state from the scope#21549

Open
andreiborza wants to merge 5 commits into
developfrom
ab/twp-read-dsc-from-scope
Open

fix(core): Defer TwP sampling by reading trace state from the scope#21549
andreiborza wants to merge 5 commits into
developfrom
ab/twp-read-dsc-from-scope

Conversation

@andreiborza

@andreiborza andreiborza commented Jun 15, 2026

Copy link
Copy Markdown
Member

In Tracing-without-Performance (spans disabled), a root placeholder previously froze a negative sampling decision in the DSC, which suppressed downstream sampling instead of leaving the decision to a performance-enabled service further along the trace.

The scope is the source of truth for a TwP placeholder's trace state:

  • getTraceData reads the sampling decision from the scope (deferred for a new trace, the upstream decision for a
    continued trace), so the outgoing sentry-trace header omits the flag instead of asserting -0. The span id comes from the scope's propagationSpanId (a fresh id is generated when the scope has none).

  • getDynamicSamplingContextFromSpan resolves a placeholder's DSC from its captured scope (continued traces keep the incoming DSC; new traces derive it from the client).

The scope is only consulted for genuine TwP placeholders. A non-recording span in tracing mode, the child of an unsampled span, or an ignored span carries an explicit negative decision and keeps propagating -0 via spanToTraceHeader.

A new (head-of-trace) TwP trace does not stamp a local transaction in its DSC; continued traces still propagate the upstream decision and DSC.

No DSC is written to the scope at span start, preserving the browser's "scope stays DSC-free between navigations" behavior.

This is an alternative to #21406

In Tracing-without-Performance (spans disabled), a root placeholder previously
froze a negative sampling decision in the DSC, which suppressed downstream
sampling instead of leaving the decision to a performance-enabled service
further along the trace.

The scope is the source of truth for a TwP placeholder's trace state:

- `getTraceData` reads the sampling decision from the scope (deferred for a new
trace, the upstream decision for a continued trace) while keeping the
placeholder's stable span id, so the outgoing `sentry-trace` header omits the
flag instead of asserting `-0`.

- `getDynamicSamplingContextFromSpan` resolves a placeholder's DSC from its
captured scope (continued traces keep the incoming DSC; new traces derive it
from the client).

A new (head-of-trace) TwP trace does not stamp a local `transaction` in its DSC;
continued traces still propagate the upstream decision and DSC.

No DSC is written to the scope at span start, preserving the browser's
"scope stays DSC-free between navigations" behavior.
@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.43 kB +0.11% +29 B 🔺
@sentry/browser - with treeshaking flags 25.86 kB +0.1% +25 B 🔺
@sentry/browser (incl. Tracing) 45.86 kB +0.35% +158 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 48.09 kB +0.31% +145 B 🔺
@sentry/browser (incl. Tracing, Profiling) 50.63 kB +0.26% +128 B 🔺
@sentry/browser (incl. Tracing, Replay) 85.06 kB +0.17% +140 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.66 kB +0.18% +131 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 89.76 kB +0.17% +144 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 102.41 kB +0.12% +113 B 🔺
@sentry/browser (incl. Feedback) 44.6 kB +0.09% +40 B 🔺
@sentry/browser (incl. sendFeedback) 32.23 kB +0.1% +30 B 🔺
@sentry/browser (incl. FeedbackAsync) 37.35 kB +0.11% +41 B 🔺
@sentry/browser (incl. Metrics) 28.5 kB +0.13% +37 B 🔺
@sentry/browser (incl. Logs) 28.74 kB +0.13% +36 B 🔺
@sentry/browser (incl. Metrics & Logs) 29.44 kB +0.14% +41 B 🔺
@sentry/react 29.23 kB +0.11% +32 B 🔺
@sentry/react (incl. Tracing) 48.15 kB +0.31% +148 B 🔺
@sentry/vue 32.53 kB +0.35% +113 B 🔺
@sentry/vue (incl. Tracing) 47.73 kB +0.29% +134 B 🔺
@sentry/svelte 27.46 kB +0.13% +34 B 🔺
CDN Bundle 29.82 kB +0.13% +36 B 🔺
CDN Bundle (incl. Tracing) 48.27 kB +0.16% +77 B 🔺
CDN Bundle (incl. Logs, Metrics) 31.38 kB +0.17% +51 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 49.56 kB +0.15% +70 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 70.68 kB +0.09% +61 B 🔺
CDN Bundle (incl. Tracing, Replay) 85.6 kB +0.1% +77 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.87 kB +0.12% +98 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 91.45 kB +0.1% +88 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.71 kB +0.1% +91 B 🔺
CDN Bundle - uncompressed 88.75 kB +0.18% +156 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 146.01 kB +0.15% +214 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.45 kB +0.17% +156 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 149.99 kB +0.15% +214 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.27 kB +0.08% +156 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.88 kB +0.09% +214 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 268.84 kB +0.08% +214 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 278.58 kB +0.08% +214 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.53 kB +0.08% +214 B 🔺
@sentry/nextjs (client) 50.56 kB +0.22% +108 B 🔺
@sentry/sveltekit (client) 46.25 kB +0.28% +128 B 🔺
@sentry/core/server 76.14 kB +0.1% +69 B 🔺
@sentry/core/browser 63.29 kB +0.12% +71 B 🔺
@sentry/node-core 61.83 kB +0.17% +104 B 🔺
@sentry/node 129.57 kB +0.06% +69 B 🔺
@sentry/node - without tracing 74.19 kB +0.13% +91 B 🔺
@sentry/aws-serverless 86.44 kB +0.1% +86 B 🔺
@sentry/cloudflare (withSentry) - minified 174.41 kB +0.13% +224 B 🔺
@sentry/cloudflare (withSentry) 436.17 kB +0.18% +763 B 🔺

View base workflow run

@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.

Hide comment

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 6a7c654. Configure here.

@andreiborza andreiborza marked this pull request as ready for review June 15, 2026 19:27
@andreiborza andreiborza requested a review from a team as a code owner June 15, 2026 19:27
@andreiborza andreiborza requested review from JPeer264, logaretm and mydea and removed request for a team June 15, 2026 19:27

@JPeer264 JPeer264 left a comment

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.

Hide comment

Actually pretty smart to put that into the DSC into the scope for TwP.

Not approving just yet because of my comment and the Symbol comment of Francesco.

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