Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c354d31
test: normalize ContinueTrace tests
Flash0ver Jul 21, 2025
37908a3
test: use Assertion over type-cast
Flash0ver Jul 21, 2025
23a45c2
fix: DSC contains actual SampleRate
Flash0ver Jul 21, 2025
5cace1f
test: fix catch and handle failed Assertions
Flash0ver Jul 21, 2025
4401a98
style: reformat HubTests
Flash0ver Jul 21, 2025
3197ab6
test: fix HubTests
Flash0ver Jul 21, 2025
bc91dd3
docs: update CHANGELOG.md
Flash0ver Jul 21, 2025
755bbef
test: skip failing test on Android & iOS (device tests)
Flash0ver Jul 23, 2025
6259233
fix: DSC sample_rate when sampling forced
Flash0ver Jul 24, 2025
fd39406
test: fix PushAndLockScope when GlobalMode
Flash0ver Jul 24, 2025
3be7eee
Merge branch 'main' into fix/update-dsc-sample-rate
Flash0ver Jul 24, 2025
cd5355b
fix: forced sampling is only considered when TracesSampler has not de…
Flash0ver Jul 24, 2025
8dbe5e3
fix: no longer overwrite DSC sample_rate when upstream sampling decis…
Flash0ver Jul 25, 2025
2dc8666
Merge branch 'main' into fix/update-dsc-sample-rate
Flash0ver Jul 25, 2025
fa43554
fix: overwrite DSC sample_rate only when TracesSampler or TracesSampl…
Flash0ver Jul 28, 2025
2382943
ref: remove Debug.Assert
Flash0ver Jul 28, 2025
8c5588f
docs: add comment to test
Flash0ver Jul 28, 2025
50edc11
Merge branch 'main' into fix/update-dsc-sample-rate
Flash0ver Jul 30, 2025
758028c
docs: update CHANGELOG
Flash0ver Jul 30, 2025
a13b7e0
fix: TransactionTracer was assumed to be considered even when TracesS…
Flash0ver Jul 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: forced sampling is only considered when TracesSampler has not de…
…cided
  • Loading branch information
Flash0ver committed Jul 24, 2025
commit cd5355b598ad65ec722d613979b5db81993018d5
15 changes: 11 additions & 4 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,23 @@ internal ITransactionTracer StartTransaction(
// The TracesSampler trumps all other sampling decisions (even the trace header)
sampleRate = samplerSampleRate;
isSampled = SampleRandHelper.IsSampled(sampleRand, sampleRate.Value);

// Ensure the actual sampleRate is set on the provided DSC (if any)
dynamicSamplingContext = dynamicSamplingContext?.WithSampleRate(null, sampleRate.Value);
}
}

// If the sampling decision isn't made by a trace sampler we check the trace header first (from the context) or
// finally fallback to Random sampling if the decision has been made by no other means
sampleRate ??= _options.TracesSampleRate ?? 0.0;
isSampled ??= context.IsSampled ?? SampleRandHelper.IsSampled(sampleRand, sampleRate.Value);
if (isSampled == null)
{
Debug.Assert(sampleRate == null);
sampleRate = _options.TracesSampleRate ?? 0.0;
isSampled = context.IsSampled ?? SampleRandHelper.IsSampled(sampleRand, sampleRate.Value);

// Ensure the actual sampleRate is set on the provided DSC (https://github.com/getsentry/sentry-dotnet/pull/4374)
dynamicSamplingContext = dynamicSamplingContext?.WithSampleRate(context.IsSampled, sampleRate.Value);
// Ensure the actual sampleRate is set on the provided DSC (if any)
dynamicSamplingContext = dynamicSamplingContext?.WithSampleRate(context.IsSampled, sampleRate.Value);
}

// Make sure there is a replayId (if available) on the provided DSC (if any).
dynamicSamplingContext = dynamicSamplingContext?.WithReplayId(_replaySession);
Expand Down
75 changes: 26 additions & 49 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,15 +714,28 @@ public void StartTransaction_DynamicSamplingContextWithSampleRate_UsesSampleRate
transactionTracer.DynamicSamplingContext.Should().BeSameAs(dsc);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void StartTransaction_DynamicSamplingContextWithSampleRate_UsesActualSampleRate(bool useTracesSampler, bool useTracesSampleRate)
[SkippableTheory]
[InlineData(null, 0.3, 0.4, true, 0.3)]
[InlineData(null, 0.3, null, true, 0.3)]
[InlineData(null, null, 0.4, true, 0.4)]
[InlineData(null, null, null, false, 0.0)]
[InlineData(true, 0.3, 0.4, true, 0.3)]
[InlineData(true, 0.3, null, true, 0.3)]
[InlineData(true, null, 0.4, true, 0.4, 1.0)]
[InlineData(true, null, null, true, 0.0, 1.0)]
[InlineData(false, 0.3, 0.4, true, 0.3)]
[InlineData(false, 0.3, null, true, 0.3)]
[InlineData(false, null, 0.4, false, 0.4, 0.0)]
[InlineData(false, null, null, false, 0.0, 0.0)]
public void StartTransaction_DynamicSamplingContextWithSampleRate_OverwritesSampleRate(bool? isSampled, double? tracesSampler, double? tracesSampleRate, bool expectedIsSampled, double expectedSampleRate, double? expectedDscSampleRate = null)
{
#if DEBUG
Skip.If(isSampled is false && expectedIsSampled, $"Forced sampling via {nameof(ITransactionContext.IsSampled)} is not considered when {nameof(SentryOptions.TracesSampler)} is used, resulting in `Debug.Assert` of {nameof(TransactionTracer)} `.ctor`.");
#endif
expectedDscSampleRate ??= expectedSampleRate;

// Arrange
var transactionContext = new TransactionContext("name", "operation");
var transactionContext = new TransactionContext("name", "operation", isSampled: isSampled);
var dsc = BaggageHeader.Create(new List<KeyValuePair<string, string>>
{
{"sentry-trace_id", "43365712692146d08ee11a729dfbcaca"},
Expand All @@ -731,67 +744,31 @@ public void StartTransaction_DynamicSamplingContextWithSampleRate_UsesActualSamp
{"sentry-sample_rand", "0.1234"},
}).CreateDynamicSamplingContext();

_fixture.Options.TracesSampler = useTracesSampler ? _ => 0.3 : _ => null;
_fixture.Options.TracesSampleRate = useTracesSampleRate ? 0.4 : null;
_fixture.Options.TracesSampler = _ => tracesSampler;
_fixture.Options.TracesSampleRate = tracesSampleRate;

var hub = _fixture.GetSut();

// Act
var transaction = hub.StartTransaction(transactionContext, new Dictionary<string, object>(), dsc);

// Assert
if (useTracesSampler || useTracesSampleRate)
if (expectedIsSampled)
{
var expectedSampleRate = useTracesSampler ? 0.3 : 0.4;
var transactionTracer = transaction.Should().BeOfType<TransactionTracer>().Subject;
transactionTracer.SampleRate.Should().Be(expectedSampleRate);
transactionTracer.DynamicSamplingContext.Should().NotBeSameAs(dsc);
transactionTracer.DynamicSamplingContext.Should().BeEquivalentTo(dsc.ReplaceSampleRate(expectedSampleRate));
transactionTracer.DynamicSamplingContext.Should().BeEquivalentTo(dsc.ReplaceSampleRate(expectedDscSampleRate.Value));
}
else
{
var unsampledTransaction = transaction.Should().BeOfType<UnsampledTransaction>().Subject;
unsampledTransaction.SampleRate.Should().Be(0.0);
unsampledTransaction.SampleRate.Should().Be(expectedSampleRate);
unsampledTransaction.DynamicSamplingContext.Should().NotBeSameAs(dsc);
unsampledTransaction.DynamicSamplingContext.Should().BeEquivalentTo(dsc.ReplaceSampleRate(0));
unsampledTransaction.DynamicSamplingContext.Should().BeEquivalentTo(dsc.ReplaceSampleRate(expectedDscSampleRate.Value));
}
}

[SkippableTheory]
[InlineData(null, 0.3)]
[InlineData(true, 1.0)]
[InlineData(false, 0.0)]
public void StartTransaction_DynamicSamplingContextWithForcedSampling_UsesForcedValue(bool? isSampled, double expectedSampleRate)
{
#if DEBUG
Skip.If(isSampled is false, "Should return an 'UnsampledTransaction', not a 'TransactionTracer'. See https://github.com/getsentry/sentry-dotnet/issues/4386.");
#endif

// Arrange
var transactionContext = new TransactionContext("name", "operation", isSampled: isSampled);
var dsc = BaggageHeader.Create(new List<KeyValuePair<string, string>>
{
{"sentry-trace_id", "43365712692146d08ee11a729dfbcaca"},
{"sentry-public_key", "d4d82fc1c2c4032a83f3a29aa3a3aff"},
{"sentry-sample_rate", "0.5"},
{"sentry-sample_rand", "0.1234"},
}).CreateDynamicSamplingContext();

_fixture.Options.TracesSampler = _ => 0.3;
_fixture.Options.TracesSampleRate = 0.4;

var hub = _fixture.GetSut();

// Act
var transaction = hub.StartTransaction(transactionContext, new Dictionary<string, object>(), dsc);

// Assert
var transactionTracer = transaction.Should().BeOfType<TransactionTracer>().Subject;
transactionTracer.SampleRate.Should().Be(0.3);
transactionTracer.DynamicSamplingContext.Should().NotBeSameAs(dsc);
transactionTracer.DynamicSamplingContext.Should().BeEquivalentTo(dsc.ReplaceSampleRate(expectedSampleRate));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Loading