11/* eslint-disable max-lines */
22import 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' ;
104import 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' ;
126
137import { registerBackgroundTabDetection } from './backgroundtab' ;
148import {
@@ -297,24 +291,25 @@ export class BrowserTracing implements Integration {
297291 return undefined ;
298292 }
299293
294+ const hub = this . _getCurrentHub ( ) ;
295+
300296 const { beforeNavigate, idleTimeout, finalTimeout, heartbeatInterval } = this . options ;
301297
302298 const isPageloadTransaction = context . op === 'pageload' ;
303299
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+ ) ;
311306
312307 const expandedContext : TransactionContext = {
313308 ...context ,
314- ...traceParentData ,
309+ ...traceparentData ,
315310 metadata : {
316311 ...context . metadata ,
317- dynamicSamplingContext : traceParentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
312+ dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
318313 } ,
319314 trimEnd : true ,
320315 } ;
@@ -341,7 +336,6 @@ export class BrowserTracing implements Integration {
341336
342337 __DEBUG_BUILD__ && logger . log ( `[Tracing] Starting ${ finalContext . op } transaction on scope` ) ;
343338
344- const hub = this . _getCurrentHub ( ) ;
345339 const { location } = WINDOW ;
346340
347341 const idleTransaction = startIdleTransaction (
@@ -353,6 +347,24 @@ export class BrowserTracing implements Integration {
353347 { location } , // for use in the tracesSampler
354348 heartbeatInterval ,
355349 ) ;
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+
356368 idleTransaction . registerBeforeFinishCallback ( transaction => {
357369 this . _collectWebVitals ( ) ;
358370 addPerformanceEntries ( transaction ) ;
@@ -424,11 +436,11 @@ export class BrowserTracing implements Integration {
424436}
425437
426438/** Returns the value of a meta tag */
427- export function getMetaContent ( metaName : string ) : string | null {
439+ export function getMetaContent ( metaName : string ) : string | undefined {
428440 // Can't specify generic to `getDomElement` because tracing can be used
429441 // in a variety of environments, have to disable `no-unsafe-member-access`
430442 // as a result.
431443 const metaTag = getDomElement ( `meta[name=${ metaName } ]` ) ;
432444 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
433- return metaTag ? metaTag . getAttribute ( 'content' ) : null ;
445+ return metaTag ? metaTag . getAttribute ( 'content' ) : undefined ;
434446}
0 commit comments