1
1
import { getCurrentHub , Hub } from '@sentry/hub' ;
2
2
import {
3
3
Baggage ,
4
+ BaggageObj ,
4
5
Event ,
5
6
Measurements ,
6
7
Transaction as TransactionInterface ,
@@ -10,11 +11,11 @@ import {
10
11
import {
11
12
createBaggage ,
12
13
dropUndefinedKeys ,
14
+ getSentryBaggageItems ,
15
+ getThirdPartyBaggage ,
13
16
isBaggageMutable ,
14
17
isSentryBaggageEmpty ,
15
18
logger ,
16
- setBaggageImmutable ,
17
- setBaggageValue ,
18
19
} from '@sentry/utils' ;
19
20
20
21
import { Span as SpanClass , SpanRecorder } from './span' ;
@@ -228,40 +229,34 @@ export class Transaction extends SpanClass implements TransactionInterface {
228
229
const hub : Hub = this . _hub || getCurrentHub ( ) ;
229
230
const client = hub && hub . getClient ( ) ;
230
231
231
- const { environment, release } = ( client && client . getOptions ( ) ) || { } ;
232
- const { publicKey } = ( client && client . getDsn ( ) ) || { } ;
233
-
234
- const sampleRate = this . metadata && this . metadata . transactionSampling && this . metadata . transactionSampling . rate ;
235
- const traceId = this . traceId ;
236
- const transactionName = this . name ;
237
-
238
- let userId , userSegment ;
239
- hub . configureScope ( scope => {
240
- const { id, segment } = scope . getUser ( ) || { } ;
241
- userId = id ;
242
- userSegment = segment ;
243
- } ) ;
244
-
245
- environment && setBaggageValue ( baggage , 'environment' , environment ) ;
246
- release && setBaggageValue ( baggage , 'release' , release ) ;
247
- transactionName && setBaggageValue ( baggage , 'transaction' , transactionName ) ;
248
- userId && setBaggageValue ( baggage , 'userid' , userId ) ;
249
- userSegment && setBaggageValue ( baggage , 'usersegment' , userSegment ) ;
250
- sampleRate &&
251
- setBaggageValue (
252
- baggage ,
253
- 'samplerate' ,
254
- // This will make sure that expnent notation (e.g. 1.45e-14) is converted to simple decimal representation
255
- // Another edge case would be something like Number.NEGATIVE_INFINITY in which case we could still
256
- // add something like .replace(/-?∞/, '0'). For the sake of saving bytes, I'll not add this until
257
- // it becomes a problem
258
- sampleRate . toLocaleString ( 'fullwide' , { useGrouping : false , maximumFractionDigits : 16 } ) ,
259
- ) ;
260
- publicKey && setBaggageValue ( baggage , 'publickey' , publicKey ) ;
261
- traceId && setBaggageValue ( baggage , 'traceid' , traceId ) ;
262
-
263
- setBaggageImmutable ( baggage ) ;
264
-
265
- return baggage ;
232
+ if ( ! client ) return baggage ;
233
+
234
+ const { environment, release } = client . getOptions ( ) || { } ;
235
+ const { publicKey : public_key } = client . getDsn ( ) || { } ;
236
+
237
+ const rate = this . metadata && this . metadata . transactionSampling && this . metadata . transactionSampling . rate ;
238
+ const sample_rate =
239
+ rate !== undefined
240
+ ? rate . toLocaleString ( 'fullwide' , { useGrouping : false , maximumFractionDigits : 16 } )
241
+ : undefined ;
242
+
243
+ const scope = hub . getScope ( ) ;
244
+ const { id : user_id , segment : user_segment } = ( scope && scope . getUser ( ) ) || { } ;
245
+
246
+ return createBaggage (
247
+ dropUndefinedKeys ( {
248
+ environment,
249
+ release,
250
+ transaction : this . name ,
251
+ user_id,
252
+ user_segment,
253
+ public_key,
254
+ trace_id : this . traceId ,
255
+ sample_rate,
256
+ ...getSentryBaggageItems ( baggage ) , // keep user-added values
257
+ } as BaggageObj ) ,
258
+ getThirdPartyBaggage ( baggage ) , // TODO: remove once we ignore 3rd party baggage
259
+ false , // set baggage immutable
260
+ ) ;
266
261
}
267
262
}
0 commit comments