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

Comment thread packages/core/src/tracing/idleSpan.ts Outdated

if (!client || !hasSpansEnabled()) {
const span = new SentryNonRecordingSpan();
const propagationContext = {

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.

I don't think this is actually necessary, do we ever put traceId/spanId/sampled/dsc on the isolation scope? I think this is always on the same type of scope, but not 100% sure...

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.

Right, we only ever need the traceId from there, which isn't affected. Removed in d1d9d2d

Comment thread packages/core/src/utils/traceData.ts Outdated
// A non-recording span is a Tracing-without-Performance placeholder that carries no sampling
// decision of its own — the scope is the source of truth. We keep the placeholder's (stable)
// span id but read the sampling decision from the scope.
const isNonRecordingSpan = span instanceof SentryNonRecordingSpan;

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.

this is fine but possibly brittle, if packages are badly deduped or similar. if we can find a different, non-identidy based way to check this that would possibly be more robust, but not necessarily required here IMHO

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.

Added branding + a utility in 51b7626

Comment thread packages/core/src/utils/traceData.ts Outdated
traceData.traceparent =
span && !isNonRecordingSpan
? spanToTraceparentHeader(span)
: scopeToTraceparentHeader(scope, span?.spanContext().spanId);

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.

does this make sense? if it is non-recording the span.spanContext should not have a valid spanId, I guess...?

@andreiborza andreiborza Jun 15, 2026

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.

It does have a valid id, but I simplified this to not pass a span id. This does mean in some cases the span id might differ, e.g. when starting a manual span in TwP

Sentry.startSpan({...}), () => {
   Sentry.captureException(...);
   fetch(...)
})

The span id of startSpan and the error event trace are the same, but the fetch call ends up with a new span id. This shouldn't be a problem since we aren't sending spans anyway in this mode.

Updated in 6a7c654

// For a non-recording placeholder (Tracing without Performance), the DSC is not carried on the
// span — the scope is the source of truth. Resolve it from the span's captured scope: continued
// traces keep the incoming DSC, new traces derive it from the client (without a local transaction).
if (rootSpan instanceof SentryNonRecordingSpan) {

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.

do we need this, actually? Would we not skip even calling this if the span is a non recording span?

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.

Yes this is needed, this is the one unifying place where we can get the DSC from. This simplifies not having to add checks for SentryNonRecordingSpans at all other callsites (e.g. in getTraceData, applySpanToEvent etc).

Comment thread packages/core/src/utils/traceData.ts Outdated

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

Comment thread packages/core/src/utils/traceData.ts Outdated
@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
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