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