Repository navigation
feat: add sentry_scope_set_trace - #2236
Merged
Merged
Conversation
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.
|
Codecov Report❌ Patch coverage is 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:
|
JoshuaMoelans
approved these changes
Oct 23, 2026
mujacica
approved these changes
Oct 23, 2026
This was referenced Oct 26, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
sentry_scope_set_trace()andsentry_scope_set_trace_n(), the scope-level counterparts ofsentry_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.Letting a local scope carry its own trace exposed a mismatch in event envelopes. Their dynamic sampling context (the
traceheader) usedSENTRY_WITH_SCOPEand 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'ssample_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 newSENTRY_SCOPE_DSCmode is set.sentry__envelope_add_event()moves it into thetraceheader, so the envelope no longer reads scopes or options.This mirrors how
sentry-dotnet,sentry-python,sentry-go,sentry-php, andsentry-javascriptcarry 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_sendandbefore_send_feedbackcan see the internaldsckey on events, which is no different from the other Sentry SDKs listed above.Ref: