@@ -127,52 +127,55 @@ internal ITransactionTracer StartTransaction(
127127 IReadOnlyDictionary < string , object ? > customSamplingContext ,
128128 DynamicSamplingContext ? dynamicSamplingContext )
129129 {
130- var transaction = new TransactionTracer ( this , context ) ;
131-
132130 // If the hub is disabled, we will always sample out. In other words, starting a transaction
133131 // after disposing the hub will result in that transaction not being sent to Sentry.
134132 // Additionally, we will always sample out if tracing is explicitly disabled.
135133 // Do not invoke the TracesSampler, evaluate the TracesSampleRate, and override any sampling decision
136134 // that may have been already set (i.e.: from a sentry-trace header).
137135 if ( ! IsEnabled )
138136 {
139- transaction . IsSampled = false ;
140- transaction . SampleRate = 0.0 ;
137+ return NoOpTransaction . Instance ;
141138 }
142- else
143- {
144- // Except when tracing is disabled, TracesSampler runs regardless of whether a decision
145- // has already been made, as it can be used to override it.
146- if ( _options . TracesSampler is { } tracesSampler )
147- {
148- var samplingContext = new TransactionSamplingContext (
149- context ,
150- customSamplingContext ) ;
151139
152- if ( tracesSampler ( samplingContext ) is { } sampleRate )
153- {
154- transaction . IsSampled = _randomValuesFactory . NextBool ( sampleRate ) ;
155- transaction . SampleRate = sampleRate ;
156- }
157- }
140+ double ? sampleRate = null ;
158141
159- // Random sampling runs only if the sampling decision hasn't been made already.
160- if ( transaction . IsSampled == null )
161- {
162- var sampleRate = _options . TracesSampleRate ?? 0.0 ;
163- transaction . IsSampled = _randomValuesFactory . NextBool ( sampleRate ) ;
164- transaction . SampleRate = sampleRate ;
165- }
142+ // Except when tracing is disabled, TracesSampler runs regardless of whether a decision
143+ // has already been made, as it can be used to override it.
144+ if ( _options . TracesSampler is { } tracesSampler )
145+ {
146+ var samplingContext = new TransactionSamplingContext (
147+ context ,
148+ customSamplingContext ) ;
166149
167- if ( transaction . IsSampled is true &&
168- _options . TransactionProfilerFactory is { } profilerFactory &&
169- _randomValuesFactory . NextBool ( _options . ProfilesSampleRate ?? 0.0 ) )
150+ if ( tracesSampler ( samplingContext ) is { } samplerSampleRate )
170151 {
171- // TODO cancellation token based on Hub being closed?
172- transaction . TransactionProfiler = profilerFactory . Start ( transaction , CancellationToken . None ) ;
152+ sampleRate = samplerSampleRate ;
173153 }
174154 }
175155
156+ // If the sampling decision isn't made by a trace sampler then fallback to Random sampling
157+ sampleRate ??= _options . TracesSampleRate ?? 0.0 ;
158+
159+ var isSampled = _randomValuesFactory . NextBool ( sampleRate . Value ) ;
160+ if ( ! isSampled )
161+ {
162+ // var unsampledTransaction = new UnsampledTransaction(this, context);
163+ // return unsampledTransaction;
164+ return new UnsampledTransaction ( this , context ) ;
165+ }
166+
167+ var transaction = new TransactionTracer ( this , context )
168+ {
169+ IsSampled = true ,
170+ SampleRate = sampleRate
171+ } ;
172+ if ( _options . TransactionProfilerFactory is { } profilerFactory &&
173+ _randomValuesFactory . NextBool ( _options . ProfilesSampleRate ?? 0.0 ) )
174+ {
175+ // TODO cancellation token based on Hub being closed?
176+ transaction . TransactionProfiler = profilerFactory . Start ( transaction , CancellationToken . None ) ;
177+ }
178+
176179 // Use the provided DSC, or create one based on this transaction.
177180 // DSC creation must be done AFTER the sampling decision has been made.
178181 transaction . DynamicSamplingContext =
@@ -213,6 +216,8 @@ public SentryTraceHeader GetTraceHeader()
213216 public BaggageHeader GetBaggage ( )
214217 {
215218 var span = GetSpan ( ) ;
219+ // TODO: Things like the SampleRand won't get propagated unless we get them from a DSC
220+ // ... so we'd need to get these from an UnsampledTransaction as well as a TransactionTracer
216221 if ( span ? . GetTransaction ( ) is TransactionTracer { DynamicSamplingContext : { IsEmpty : false } dsc } )
217222 {
218223 return dsc . ToBaggageHeader ( ) ;
0 commit comments