Skip to content

fix: Inherit parentSampleRate and parentSampleRand when the parent decided sampling - #9014

Open
cuva wants to merge 1 commit into
getsentry:mainfrom
raycast:fix/inherit-parent-sample-rate-and-rand
Open

cuva wants to merge 1 commit into
getsentry:mainfrom
raycast:fix/inherit-parent-sample-rate-and-rand

Conversation

@cuva

@cuva cuva commented Sep 11, 2026

Copy link
Copy Markdown

📜 Description

SentryTransactionContext accepts parentSampleRate and parentSampleRand (added in #4751), but sentry_sampleTrace never reads them. In the parent-decided branch it reports the context's own sampleRate and sampleRand, which the trace-header initializer leaves nil. A transaction that continues a trace from incoming sentry-trace and baggage headers therefore ends up with a trace context without sample_rate and sample_rand, and SentryTraceContext omits them from the outgoing baggage.

This change makes the parent-decided branch inherit parentSampleRate and parentSampleRand, falling back to the context's own sampleRate and sampleRand when the parent values are not set, which preserves the existing behaviour for callers that set those directly.

💡 Motivation and Context

The dynamic sampling context spec requires a received DSC to be treated as frozen and propagated unchanged, and the propagated random value section requires sample_rand to be the same across a trace so Relay reaches one consistent keep-or-drop decision. Today a Cocoa transaction that continues a trace drops both values, so Relay can sample that transaction independently of the rest of the trace.

Our use case is a desktop app where a web view (JavaScript SDK) starts the trace and the native macOS host continues it for the requests it handles. The only workaround is to also set sampleRate and sampleRand on the context by hand, which relies on the sampler not reading the parent fields.

💚 How did you test it?

  • Added four tests to SentryHubTests. The two shouldInheritParentSampleRateAndRand tests and shouldPropagateThemInTraceContext fail on main with nil for both values and pass with this change. The shouldUseContextSampleRateAndRand test covers the fallback.
  • make test-macos FOR_AGENTS=true ONLY_TESTING=SentryTests/SentryHubTests: 122 tests, 0 failures.
  • clang-format reports no changes for SentrySampling.m; make analyze run on the change.

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.
  • If I added a new public API, I also added it to the SentryObjC wrapper.

…cided sampling

SentryTransactionContext accepts parentSampleRate and parentSampleRand, but
sentry_sampleTrace never read them. In the parent-decided branch it reported the
context's own sampleRate and sampleRand, which the trace-header initializer leaves
nil, so a transaction continuing a trace from incoming headers propagated a dynamic
sampling context without sample_rate and sample_rand. Relay could then reach a
different keep-or-drop decision for it than for the rest of the trace.

Inherit the parent values in that branch and fall back to the context's own
sampleRate and sampleRand when they are not set.
@cuva
cuva force-pushed the fix/inherit-parent-sample-rate-and-rand branch from d93fcf4 to 63af451 Compare September 11, 2026 08:29
@NinjaLikesCheez NinjaLikesCheez added the run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request label Sep 14, 2026

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

Thank you for your contribution! This looks good to me - kicked off the CI checks (most failed yesterday due to Homebrew 7) and there's one small changelog conflict to fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants