Skip to content

feat: add sentry_scope_set_trace - #2236

Merged
jpnurmi merged 4 commits into
v1from
jpnurmi/feat/scope-set-trace
Oct 26, 2026
Merged

jpnurmi merged 4 commits into
v1from
jpnurmi/feat/scope-set-trace

Conversation

@jpnurmi

@jpnurmi jpnurmi commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Adds sentry_scope_set_trace() and sentry_scope_set_trace_n(), the scope-level counterparts of sentry_set_trace(). A local scope can now carry its own trace, separate from the isolation scope, for example for concurrent units of work. sentry_set_trace() now forwards to the new setter on the isolation scope.

sentry_scope_t *scope = sentry_local_scope_new();
sentry_scope_set_trace(scope, trace_id, parent_span_id);
sentry_scope_capture_event(scope, event, NULL);

Letting a local scope carry its own trace exposed a mismatch in event envelopes. Their dynamic sampling context (the trace header) used SENTRY_WITH_SCOPE and therefore always came from the isolation scope, even for scoped captures. A scoped event could carry the local scope's trace in its payload, but the isolation scope's sample_rand, sampled, release and environment in the header. The DSC now comes from the scope the event is captured with:

  • sentry__scope_apply_to_event() stamps it on the event under an internal key, when the new SENTRY_SCOPE_DSC mode is set.
  • sentry__envelope_add_event() moves it into the trace header, so the envelope no longer reads scopes or options.

This mirrors how sentry-dotnet, sentry-python, sentry-go, sentry-php, and sentry-javascript carry the DSC on the event.

Side-note: the mode is opt-in because crashpad's on-disk event and the native backend's daemon envelopes bypass sentry__envelope_add_event(), and Relay flags unknown top-level event attributes. Also, before_send and before_send_feedback can see the internal dsc key on events, which is no different from the other Sentry SDKs listed above.

Ref:

Add `sentry_scope_set_trace()` and `sentry_scope_set_trace_n()` to set
the trace on a given scope, so that a local scope can carry a distinct
trace for a concurrent unit of work. The global `sentry_set_trace_n()`
now forwards to the scope-level setter.
Scoped event and feedback capture apply the given scope to the event
payload, but `sentry__envelope_add_event()` read the dynamic sampling
context (DSC) and the propagation context's `sample_rand` from the
isolation scope through `SENTRY_WITH_SCOPE`. Since
`sentry_scope_set_trace()` lets a local scope carry its own
propagation context and DSC, such an envelope mixed metadata from two
scopes:

- the event's trace context came from the local scope,
- the header's `trace_id` matched, because it is copied from the
  event payload,
- `sample_rand`, `sampled`, `release` and `environment` came from the
  isolation scope.

For example, with a `traces_sample_rate` of 0.5, an isolation scope
with a `sample_rand` of 0.25 and a local scope with 0.75, scoped
capture sent `sampled: "true"` instead of the local scope's `"false"`.
Feedback was affected the same way, because
`sentry__envelope_add_feedback_event()` delegates to
`sentry__envelope_add_event()`.

Other Sentry SDKs avoid this by computing the DSC from the scope that
is applied to the event and carrying it on the event as SDK-internal
data, which the envelope builder moves into the header:

- sentry-go stores it in `event.sdkMetaData.dsc` from
  `Scope.ApplyToEvent`,
- sentry-php stores it in the event's SDK metadata from
  `Scope::applyToEvent`,
- sentry-javascript stores it in `event.sdkProcessingMetadata`,
- sentry-dotnet stores it in an internal
  `SentryEvent.DynamicSamplingContext`,
- sentry-python stores it in `contexts.trace.dynamic_sampling_context`,
  which the client pops before building the envelope.

In all of them the envelope is a plain container that never looks up
a scope.

Follow the same pattern: when the new `SENTRY_SCOPE_DSC` mode is set,
`sentry__scope_apply_to_event()` stamps the DSC on non-transaction
events under an internal `dsc` key, and `sentry__envelope_add_event()`
removes it from the payload and uses it as the `trace` header. The
envelope no longer reads any scope or options.

The mode is opt-in because not every scope-applied event goes through
`sentry__envelope_add_event()`. Crashpad's handler uploads the on-disk
`__sentry-event` directly, and the native backend's daemon writes its
own envelopes. Relay reports unknown top-level event attributes as
invalid, so those paths apply their scopes without the mode.
`SENTRY_SCOPE_ALL` includes it, which covers `sentry__prepare_event()`,
and feedback requests it explicitly.

As a result, crashpad envelopes built from reports in a later run no
longer get a `trace` header from that run's isolation scope, which
never described the crash.
Capture an event with a local scope whose trace is set with
`sentry_scope_set_trace()`, while the isolation scope carries a
different trace, and check that both the event's trace context and
the envelope `trace` header come from the local scope. The header's
`sample_rand` must match the event's. Before the DSC was taken from
the capture scope, it came from the isolation scope instead.
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against ab61016

@jpnurmi jpnurmi added the v1 Targets 1.0 (v1 branch) label Oct 23, 2026
@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.73418% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.18%. Comparing base (a6afbb1) to head (ab61016).

Additional details and impacted files
@@            Coverage Diff             @@
##               v1    #2236      +/-   ##
==========================================
+ Coverage   76.15%   76.18%   +0.03%     
==========================================
  Files         104      104              
  Lines       29568    29584      +16     
  Branches     5480     5480              
==========================================
+ Hits        22517    22540      +23     
+ Misses       5675     5674       -1     
+ Partials     1376     1370       -6     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@limbonaut limbonaut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👌

@jpnurmi
jpnurmi merged commit 0fdaebc into v1 Oct 26, 2026
108 of 111 checks passed
@jpnurmi
jpnurmi deleted the jpnurmi/feat/scope-set-trace branch October 26, 2026 12:07
@jpnurmi jpnurmi linked an issue Oct 26, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Targets 1.0 (v1 branch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Align and flesh out trace propagation API

4 participants