fix: overwrite DSC sample_rate when sampling#4374
Merged
Conversation
Flash0ver
commented
Jul 23, 2025
Flash0ver
commented
Jul 23, 2025
This was referenced Jul 24, 2025
…ion has been made
| sampleRate = _options.TracesSampleRate ?? 0.0; | ||
| isSampled = context.IsSampled ?? SampleRandHelper.IsSampled(sampleRand, sampleRate.Value); | ||
|
|
||
| if (context.IsSampled is null && _options.TracesSampleRate is not null) |
Collaborator
There was a problem hiding this comment.
TLDR; I think we can remove the if block here.
Long version
Apologies, I think I got us confused on Slack:
contextis of typeITransactionContext... I think when an upstream span is sampled,context.IsParentSampledwill be true (notcontext.IsSampled).- Whether or not the parent span was sampled is probably not relevant. It's possible that our process has a different sample-rate configured to the upstream node... as long as we use the same sample rand, we maximise (not guarantee) the chance of having a complete trace. If we have a sample-rand of 0.5 and the upstream node has a sample rate of 0.75 but we have a sample rate of 0.25 (and assuming I've understood the requirements) I think we need to sample out in this case and put our own sample-rate (0.25) on the header... so we just overwrite in this scenario.
- I'm not 100% sure whether that's how sampling is implemented at Sentry though... maybe we only support different sample rates on different nodes via the TracesSampler function. @cleptric do you know?
- If
context.IsSampled == trueI think that means the SDK user has forced sampling to true (passing it explicitly via the call toStartTransaction).
On the bright side, if all of the above is correct, it means it should be trivial to implement the logic to force sampling (per the other SDKs)... basically we can force a sample-rate of 0.0 or 1.0 if context.IsSampled is set.
This was referenced Sep 15, 2025
This was referenced Sep 22, 2025
This was referenced Oct 2, 2025
This was referenced Oct 15, 2025
This was referenced Nov 3, 2025
This was referenced Dec 8, 2025
This was referenced Dec 15, 2025
This was referenced Jan 18, 2026
This was referenced Mar 7, 2026
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.
Fixes #4392
sample_ratewhen sampling #4392Overwrite Dynamic Sampling Context (DSC)
sample_ratewhen sampling decision is madeFixes: https://linear.app/getsentry/project/update-the-dscs-sample-rate-e4e8398e6acc/overview
See also: getsentry/team-sdks#117
Symptom
The Trace-View's extrapolation is based on the
"sample_rate"of the Dynamic Sampling Context.Changes
When a DSC is available, then its immutability is ignored and its
sample_rateoverwrittenTracesSampleris used and applied, then its resulting Sample-Rate is setTracesSampleRateis used and applied, that Sample-Rate is setITransactionContext.IsSampledis set (when an upstream sampling decision has been made), then the DSC"sample_rate"is kept unchangedAlso, refactored
test/Sentry.Tests/HubTests.cshub.ConfigureScopewithhub.ScopeManager.ConfigureScopefor assertionssample_ratewhen sampling #4374 (comment)Unchanged
SampleRateon neither the resultingTransactionTracernorUnsampledTransactionNote
The
sample_rateoverwritten in the DSC is now the sameSampleRateset on the resultingTransactionTracerorUnsampledTransaction(TransactionTracer.SampleRate/UnsampledTransaction.SampleRate) with the following precedenceSentryOptions.TracesSamplerreaches a sampling decision (is not null and returns not null)ITransactionContext.IsSampledis set (an upstream sampling decision has been made)SentryOptions.TracesSampleRatereaches a sampling decision (is not null)