1
1
import { getMainCarrier , Hub } from '@sentry/hub' ;
2
- import { CustomSamplingContext , SamplingContext , TransactionContext , TransactionSamplingMethod } from '@sentry/types' ;
2
+ import {
3
+ CustomSamplingContext ,
4
+ Options ,
5
+ SamplingContext ,
6
+ TransactionContext ,
7
+ TransactionSamplingMethod ,
8
+ } from '@sentry/types' ;
3
9
import { logger } from '@sentry/utils' ;
4
10
5
11
import { registerErrorInstrumentation } from './errors' ;
@@ -33,12 +39,9 @@ function traceHeaders(this: Hub): { [key: string]: string } {
33
39
*
34
40
* @returns The given transaction with its `sampled` value set
35
41
*/
36
- function sample < T extends Transaction > ( hub : Hub , transaction : T , samplingContext : SamplingContext ) : T {
37
- const client = hub . getClient ( ) ;
38
- const options = ( client && client . getOptions ( ) ) || { } ;
39
-
40
- // nothing to do if there's no client or if tracing is disabled
41
- if ( ! client || ! hasTracingEnabled ( options ) ) {
42
+ function sample < T extends Transaction > ( transaction : T , options : Options , samplingContext : SamplingContext ) : T {
43
+ // nothing to do if tracing is not enabled
44
+ if ( ! hasTracingEnabled ( options ) ) {
42
45
transaction . sampled = false ;
43
46
return transaction ;
44
47
}
@@ -114,10 +117,6 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
114
117
return transaction ;
115
118
}
116
119
117
- // at this point we know we're keeping the transaction, whether because of an inherited decision or because it got
118
- // lucky with the dice roll
119
- transaction . initSpanRecorder ( options . _experiments ?. maxSpans as number ) ;
120
-
121
120
logger . log ( `[Tracing] starting ${ transaction . op } transaction - ${ transaction . name } ` ) ;
122
121
return transaction ;
123
122
}
@@ -165,12 +164,18 @@ function _startTransaction(
165
164
transactionContext : TransactionContext ,
166
165
customSamplingContext ?: CustomSamplingContext ,
167
166
) : Transaction {
168
- const transaction = new Transaction ( transactionContext , this ) ;
169
- return sample ( this , transaction , {
167
+ const options = this . getClient ( ) ?. getOptions ( ) || { } ;
168
+
169
+ let transaction = new Transaction ( transactionContext , this ) ;
170
+ transaction = sample ( transaction , options , {
170
171
parentSampled : transactionContext . parentSampled ,
171
172
transactionContext,
172
173
...customSamplingContext ,
173
174
} ) ;
175
+ if ( transaction . sampled ) {
176
+ transaction . initSpanRecorder ( options . _experiments ?. maxSpans as number ) ;
177
+ }
178
+ return transaction ;
174
179
}
175
180
176
181
/**
@@ -183,12 +188,18 @@ export function startIdleTransaction(
183
188
onScope ?: boolean ,
184
189
customSamplingContext ?: CustomSamplingContext ,
185
190
) : IdleTransaction {
186
- const transaction = new IdleTransaction ( transactionContext , hub , idleTimeout , onScope ) ;
187
- return sample ( hub , transaction , {
191
+ const options = hub . getClient ( ) ?. getOptions ( ) || { } ;
192
+
193
+ let transaction = new IdleTransaction ( transactionContext , hub , idleTimeout , onScope ) ;
194
+ transaction = sample ( transaction , options , {
188
195
parentSampled : transactionContext . parentSampled ,
189
196
transactionContext,
190
197
...customSamplingContext ,
191
198
} ) ;
199
+ if ( transaction . sampled ) {
200
+ transaction . initSpanRecorder ( options . _experiments ?. maxSpans as number ) ;
201
+ }
202
+ return transaction ;
192
203
}
193
204
194
205
/**
0 commit comments