8
8
BrowserTracing ,
9
9
BrowserTracingOptions ,
10
10
DEFAULT_MAX_TRANSACTION_DURATION_SECONDS ,
11
- getHeaderContext ,
11
+ extractTraceDataFromMetaTags ,
12
12
getMetaContent ,
13
13
} from '../../src/browser/browsertracing' ;
14
14
import { defaultRequestInstrumentationOptions } from '../../src/browser/request' ;
@@ -209,20 +209,23 @@ describe('BrowserTracing', () => {
209
209
} ) ;
210
210
} ) ;
211
211
212
- it ( 'sets transaction context from sentry-trace header' , ( ) => {
213
- const name = 'sentry-trace' ;
214
- const content = '126de09502ae4e0fb26c6967190756a4-b6e54397b12a2a0f-1' ;
215
- document . head . innerHTML = `<meta name="${ name } " content="${ content } ">` ;
212
+ it ( 'sets transaction context from <meta> tag data' , ( ) => {
213
+ document . head . innerHTML =
214
+ `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0"> ` +
215
+ `<meta name="tracestate" content="sentry=doGsaREgReaT,maisey=silly,charlie=goofy">` ;
216
+
216
217
const startIdleTransaction = jest . spyOn ( hubExtensions , 'startIdleTransaction' ) ;
217
218
218
219
createBrowserTracing ( true , { routingInstrumentation : customRoutingInstrumentation } ) ;
219
220
220
221
expect ( startIdleTransaction ) . toHaveBeenCalledWith (
222
+ // because `startIdleTransaction` uses positional arguments, we have to include placeholders for all of them
221
223
expect . any ( Object ) ,
222
224
expect . objectContaining ( {
223
- traceId : '126de09502ae4e0fb26c6967190756a4' ,
224
- parentSpanId : 'b6e54397b12a2a0f' ,
225
- parentSampled : true ,
225
+ traceId : '12312012123120121231201212312012' ,
226
+ parentSpanId : '1121201211212012' ,
227
+ parentSampled : false ,
228
+ metadata : { tracestate : { sentry : 'sentry=doGsaREgReaT' , thirdparty : 'maisey=silly,charlie=goofy' } } ,
226
229
} ) ,
227
230
expect . any ( Number ) ,
228
231
expect . any ( Boolean ) ,
@@ -354,7 +357,7 @@ describe('BrowserTracing', () => {
354
357
} ) ;
355
358
} ) ;
356
359
357
- describe ( 'sentry-trace <meta> element ' , ( ) => {
360
+ describe ( 'sentry-trace/tracestate <meta> tags ' , ( ) => {
358
361
describe ( 'getMetaContent' , ( ) => {
359
362
it ( 'finds the specified tag and extracts the value' , ( ) => {
360
363
const name = 'sentry-trace' ;
@@ -384,39 +387,37 @@ describe('BrowserTracing', () => {
384
387
} ) ;
385
388
} ) ;
386
389
387
- describe ( 'getHeaderContext' , ( ) => {
388
- it ( 'correctly parses a valid sentry-trace meta header' , ( ) => {
389
- document . head . innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">` ;
390
-
391
- const headerContext = getHeaderContext ( ) ;
392
-
393
- expect ( headerContext ) . toBeDefined ( ) ;
394
- expect ( headerContext ! . traceId ) . toEqual ( '12312012123120121231201212312012' ) ;
395
- expect ( headerContext ! . parentSpanId ) . toEqual ( '1121201211212012' ) ;
396
- expect ( headerContext ! . parentSampled ) . toEqual ( false ) ;
397
- } ) ;
398
-
399
- it ( 'returns undefined if the header is malformed' , ( ) => {
400
- document . head . innerHTML = `<meta name="sentry-trace" content="12312012-112120121-0">` ;
390
+ describe ( 'extractTraceDataFromMetaTags' , ( ) => {
391
+ it ( 'correctly captures both sentry-trace and tracestate data' , ( ) => {
392
+ document . head . innerHTML =
393
+ `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0"> ` +
394
+ `<meta name="tracestate" content="sentry=doGsaREgReaT,maisey=silly,charlie=goofy">` ;
401
395
402
- const headerContext = getHeaderContext ( ) ;
396
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
403
397
404
- expect ( headerContext ) . toBeUndefined ( ) ;
398
+ expect ( headerContext ) . toEqual ( {
399
+ traceId : '12312012123120121231201212312012' ,
400
+ parentSpanId : '1121201211212012' ,
401
+ parentSampled : false ,
402
+ metadata : { tracestate : { sentry : 'sentry=doGsaREgReaT' , thirdparty : 'maisey=silly,charlie=goofy' } } ,
403
+ } ) ;
405
404
} ) ;
406
405
407
- it ( " returns undefined if the header isn't there" , ( ) => {
408
- document . head . innerHTML = `<meta name="dogs " content="12312012123120121231201212312012-1121201211212012-0 ">` ;
406
+ it ( ' returns undefined if neither tag is there' , ( ) => {
407
+ document . head . innerHTML = `<meta name="cory " content="loyal"> <meta name="bodhi" content="floppy ">` ;
409
408
410
- const headerContext = getHeaderContext ( ) ;
409
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
411
410
412
411
expect ( headerContext ) . toBeUndefined ( ) ;
413
412
} ) ;
414
413
} ) ;
415
414
416
- describe ( 'using the data' , ( ) => {
415
+ describe ( 'using <meta> tag data' , ( ) => {
417
416
it ( 'uses the data for pageload transactions' , ( ) => {
418
417
// make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
419
- document . head . innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">` ;
418
+ document . head . innerHTML =
419
+ `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0"> ` +
420
+ `<meta name="tracestate" content="sentry=doGsaREgReaT,maisey=silly,charlie=goofy">` ;
420
421
421
422
// pageload transactions are created as part of the BrowserTracing integration's initialization
422
423
createBrowserTracing ( true ) ;
@@ -427,11 +428,20 @@ describe('BrowserTracing', () => {
427
428
expect ( transaction . traceId ) . toEqual ( '12312012123120121231201212312012' ) ;
428
429
expect ( transaction . parentSpanId ) . toEqual ( '1121201211212012' ) ;
429
430
expect ( transaction . sampled ) . toBe ( false ) ;
431
+ expect ( transaction . metadata ?. tracestate ) . toEqual ( {
432
+ sentry : 'sentry=doGsaREgReaT' ,
433
+ thirdparty : 'maisey=silly,charlie=goofy' ,
434
+ } ) ;
430
435
} ) ;
431
436
437
+ // navigation transactions happen on the same page as generated a pageload transaction (with <metat> tags still
438
+ // possibly present) but they start their own trace and therefore shouldn't contain the pageload transaction's
439
+ // trace data
432
440
it ( 'ignores the data for navigation transactions' , ( ) => {
433
441
mockChangeHistory = ( ) => undefined ;
434
- document . head . innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">` ;
442
+ document . head . innerHTML =
443
+ `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0"> ` +
444
+ `<meta name="tracestate" content="sentry=doGsaREgReaT,maisey=silly,charlie=goofy">` ;
435
445
436
446
createBrowserTracing ( true ) ;
437
447
@@ -442,6 +452,9 @@ describe('BrowserTracing', () => {
442
452
expect ( transaction . op ) . toBe ( 'navigation' ) ;
443
453
expect ( transaction . traceId ) . not . toEqual ( '12312012123120121231201212312012' ) ;
444
454
expect ( transaction . parentSpanId ) . toBeUndefined ( ) ;
455
+ expect ( transaction . sampled ) . toBe ( true ) ;
456
+ expect ( transaction . metadata ?. tracestate ?. sentry ) . not . toEqual ( 'sentry=doGsaREgReaT' ) ;
457
+ expect ( transaction . metadata ?. tracestate ?. thirdparty ) . toBeUndefined ( ) ;
445
458
} ) ;
446
459
} ) ;
447
460
} ) ;
0 commit comments