Skip to content

Commit e24c778

Browse files
Added HubTests
1 parent 5416caf commit e24c778

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/Sentry/Internal/Hub.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ internal Hub(
4444
ISystemClock? clock = null,
4545
IInternalScopeManager? scopeManager = null,
4646
RandomValuesFactory? randomValuesFactory = null,
47-
IReplaySession? replaySession = null,
48-
BackpressureMonitor? backpressureMonitor = null)
47+
IReplaySession? replaySession = null)
4948
{
5049
if (string.IsNullOrWhiteSpace(options.Dsn))
5150
{
@@ -60,9 +59,10 @@ internal Hub(
6059
_randomValuesFactory = randomValuesFactory ?? new SynchronizedRandomValuesFactory();
6160
_sessionManager = sessionManager ?? new GlobalSessionManager(options);
6261
_clock = clock ?? SystemClock.Clock;
63-
_options.BackpressureMonitor = _options.EnableBackpressureHandling
64-
? backpressureMonitor ?? new BackpressureMonitor(_options.DiagnosticLogger, clock)
65-
: null;
62+
if (_options.EnableBackpressureHandling && _options.BackpressureMonitor is null)
63+
{
64+
_options.BackpressureMonitor = new BackpressureMonitor(_options.DiagnosticLogger, clock);
65+
}
6666
client ??= new SentryClient(options, randomValuesFactory: _randomValuesFactory, sessionManager: _sessionManager);
6767
_replaySession = replaySession ?? ReplaySession.Instance;
6868
ScopeManager = scopeManager ?? new SentryScopeManager(options, client);

test/Sentry.Tests/HubTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,48 @@ public void StartTransaction_DynamicSamplingContextWithSampleRate_UsesSampleRate
714714
transactionTracer.DynamicSamplingContext.Should().BeSameAs(dsc);
715715
}
716716

717+
[Theory]
718+
[InlineData(true)]
719+
[InlineData(false)]
720+
public void StartTransaction_Backpressure_Downsamples(bool usesTracesSampler)
721+
{
722+
// Arrange
723+
var transactionContext = new TransactionContext("name", "operation");
724+
725+
var clock = new MockClock(DateTimeOffset.UtcNow);
726+
var backpressureMonitor = new BackpressureMonitor(null, clock, startImmediately: false);
727+
backpressureMonitor.SetDownsampleLevel(1);
728+
_fixture.Options.BackpressureMonitor = backpressureMonitor;
729+
var sampleRate = 0.5f;
730+
var expectedDownsampledRate = sampleRate * backpressureMonitor.DownsampleFactor;
731+
if (usesTracesSampler)
732+
{
733+
_fixture.Options.TracesSampler = _ => sampleRate;
734+
}
735+
else
736+
{
737+
_fixture.Options.TracesSampleRate = sampleRate;
738+
}
739+
740+
var hub = _fixture.GetSut();
741+
742+
// Act
743+
var transaction = hub.StartTransaction(transactionContext, new Dictionary<string, object>());
744+
745+
switch (transaction)
746+
{
747+
// Assert
748+
case TransactionTracer tracer:
749+
tracer.SampleRate.Should().Be(expectedDownsampledRate);
750+
break;
751+
case UnsampledTransaction unsampledTransaction:
752+
unsampledTransaction.SampleRate.Should().Be(expectedDownsampledRate);
753+
break;
754+
default:
755+
throw new Exception("Unexpected transaction type.");
756+
}
757+
}
758+
717759
// overwrite the 'sample_rate' of the Dynamic Sampling Context (DSC) when a sampling decisions is made in the downstream SDK
718760
// 1. overwrite when 'TracesSampler' reaches a sampling decision
719761
// 2. keep when a sampling decision has been made upstream (via 'TransactionContext.IsSampled')

0 commit comments

Comments
 (0)