11import { getCurrentHub , Hub } from '@sentry/hub' ;
22import {
33 Baggage ,
4+ BaggageObj ,
45 Event ,
56 Measurements ,
67 Transaction as TransactionInterface ,
@@ -10,11 +11,11 @@ import {
1011import {
1112 createBaggage ,
1213 dropUndefinedKeys ,
14+ getSentryBaggageItems ,
15+ getThirdPartyBaggage ,
1316 isBaggageMutable ,
1417 isSentryBaggageEmpty ,
1518 logger ,
16- setBaggageImmutable ,
17- setBaggageValue ,
1819} from '@sentry/utils' ;
1920
2021import { Span as SpanClass , SpanRecorder } from './span' ;
@@ -228,40 +229,34 @@ export class Transaction extends SpanClass implements TransactionInterface {
228229 const hub : Hub = this . _hub || getCurrentHub ( ) ;
229230 const client = hub && hub . getClient ( ) ;
230231
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+ ) ;
266261 }
267262}
0 commit comments