1
1
/* eslint-disable max-lines */
2
2
import type { Hub , IdleTransaction } from '@sentry/core' ;
3
- import {
4
- addTracingExtensions ,
5
- extractTraceparentData ,
6
- getActiveTransaction ,
7
- startIdleTransaction ,
8
- TRACING_DEFAULTS ,
9
- } from '@sentry/core' ;
3
+ import { addTracingExtensions , getActiveTransaction , startIdleTransaction , TRACING_DEFAULTS } from '@sentry/core' ;
10
4
import type { EventProcessor , Integration , Transaction , TransactionContext , TransactionSource } from '@sentry/types' ;
11
- import { baggageHeaderToDynamicSamplingContext , getDomElement , logger } from '@sentry/utils' ;
5
+ import { getDomElement , logger , tracingContextFromHeaders } from '@sentry/utils' ;
12
6
13
7
import { registerBackgroundTabDetection } from './backgroundtab' ;
14
8
import {
@@ -297,24 +291,25 @@ export class BrowserTracing implements Integration {
297
291
return undefined ;
298
292
}
299
293
294
+ const hub = this . _getCurrentHub ( ) ;
295
+
300
296
const { beforeNavigate, idleTimeout, finalTimeout, heartbeatInterval } = this . options ;
301
297
302
298
const isPageloadTransaction = context . op === 'pageload' ;
303
299
304
- const sentryTraceMetaTagValue = isPageloadTransaction ? getMetaContent ( 'sentry-trace' ) : null ;
305
- const baggageMetaTagValue = isPageloadTransaction ? getMetaContent ( 'baggage' ) : null ;
306
-
307
- const traceParentData = sentryTraceMetaTagValue ? extractTraceparentData ( sentryTraceMetaTagValue ) : undefined ;
308
- const dynamicSamplingContext = baggageMetaTagValue
309
- ? baggageHeaderToDynamicSamplingContext ( baggageMetaTagValue )
310
- : undefined ;
300
+ const sentryTrace = isPageloadTransaction ? getMetaContent ( 'sentry-trace' ) : '' ;
301
+ const baggage = isPageloadTransaction ? getMetaContent ( 'baggage' ) : '' ;
302
+ const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
303
+ sentryTrace ,
304
+ baggage ,
305
+ ) ;
311
306
312
307
const expandedContext : TransactionContext = {
313
308
...context ,
314
- ...traceParentData ,
309
+ ...traceparentData ,
315
310
metadata : {
316
311
...context . metadata ,
317
- dynamicSamplingContext : traceParentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
312
+ dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
318
313
} ,
319
314
trimEnd : true ,
320
315
} ;
@@ -341,7 +336,6 @@ export class BrowserTracing implements Integration {
341
336
342
337
__DEBUG_BUILD__ && logger . log ( `[Tracing] Starting ${ finalContext . op } transaction on scope` ) ;
343
338
344
- const hub = this . _getCurrentHub ( ) ;
345
339
const { location } = WINDOW ;
346
340
347
341
const idleTransaction = startIdleTransaction (
@@ -353,6 +347,24 @@ export class BrowserTracing implements Integration {
353
347
{ location } , // for use in the tracesSampler
354
348
heartbeatInterval ,
355
349
) ;
350
+
351
+ const scope = hub . getScope ( ) ;
352
+
353
+ // If it's a pageload and there is a meta tag set
354
+ // use the traceparentData as the propagation context
355
+ if ( isPageloadTransaction && traceparentData ) {
356
+ scope . setPropagationContext ( propagationContext ) ;
357
+ } else {
358
+ // Navigation transactions should set a new propagation context based on the
359
+ // created idle transaction.
360
+ scope . setPropagationContext ( {
361
+ traceId : idleTransaction . traceId ,
362
+ spanId : idleTransaction . spanId ,
363
+ parentSpanId : idleTransaction . parentSpanId ,
364
+ sampled : ! ! idleTransaction . sampled ,
365
+ } ) ;
366
+ }
367
+
356
368
idleTransaction . registerBeforeFinishCallback ( transaction => {
357
369
this . _collectWebVitals ( ) ;
358
370
addPerformanceEntries ( transaction ) ;
@@ -424,11 +436,11 @@ export class BrowserTracing implements Integration {
424
436
}
425
437
426
438
/** Returns the value of a meta tag */
427
- export function getMetaContent ( metaName : string ) : string | null {
439
+ export function getMetaContent ( metaName : string ) : string | undefined {
428
440
// Can't specify generic to `getDomElement` because tracing can be used
429
441
// in a variety of environments, have to disable `no-unsafe-member-access`
430
442
// as a result.
431
443
const metaTag = getDomElement ( `meta[name=${ metaName } ]` ) ;
432
444
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
433
- return metaTag ? metaTag . getAttribute ( 'content' ) : null ;
445
+ return metaTag ? metaTag . getAttribute ( 'content' ) : undefined ;
434
446
}
0 commit comments