1
1
import { BrowserClient } from '@sentry/browser' ;
2
2
import { Hub , makeMain } from '@sentry/hub' ;
3
+ import { BaggageObj } from '@sentry/types' ;
3
4
import { getGlobalObject , InstrumentHandlerCallback , InstrumentHandlerType } from '@sentry/utils' ;
4
5
import { JSDOM } from 'jsdom' ;
5
6
6
7
import {
7
8
BrowserTracing ,
8
9
BrowserTracingOptions ,
9
- getHeaderContext ,
10
+ extractTraceDataFromMetaTags ,
10
11
getMetaContent ,
11
12
} from '../../src/browser/browsertracing' ;
12
13
import { MetricsInstrumentation } from '../../src/browser/metrics' ;
@@ -215,7 +216,8 @@ describe('BrowserTracing', () => {
215
216
it ( 'sets transaction context from sentry-trace header' , ( ) => {
216
217
const name = 'sentry-trace' ;
217
218
const content = '126de09502ae4e0fb26c6967190756a4-b6e54397b12a2a0f-1' ;
218
- document . head . innerHTML = `<meta name="${ name } " content="${ content } ">` ;
219
+ document . head . innerHTML =
220
+ `<meta name="${ name } " content="${ content } ">` + '<meta name="baggage" content="sentry-release=2.1.14,foo=bar">' ;
219
221
const startIdleTransaction = jest . spyOn ( hubExtensions , 'startIdleTransaction' ) ;
220
222
221
223
createBrowserTracing ( true , { routingInstrumentation : customInstrumentRouting } ) ;
@@ -226,6 +228,9 @@ describe('BrowserTracing', () => {
226
228
traceId : '126de09502ae4e0fb26c6967190756a4' ,
227
229
parentSpanId : 'b6e54397b12a2a0f' ,
228
230
parentSampled : true ,
231
+ metadata : {
232
+ baggage : [ { release : '2.1.14' } , 'foo=bar' ] ,
233
+ } ,
229
234
} ) ,
230
235
expect . any ( Number ) ,
231
236
expect . any ( Number ) ,
@@ -322,7 +327,7 @@ describe('BrowserTracing', () => {
322
327
} ) ;
323
328
} ) ;
324
329
325
- describe ( 'sentry-trace <meta> element ' , ( ) => {
330
+ describe ( 'sentry-trace and baggage <meta> elements ' , ( ) => {
326
331
describe ( 'getMetaContent' , ( ) => {
327
332
it ( 'finds the specified tag and extracts the value' , ( ) => {
328
333
const name = 'sentry-trace' ;
@@ -352,67 +357,107 @@ describe('BrowserTracing', () => {
352
357
} ) ;
353
358
} ) ;
354
359
355
- describe ( 'getHeaderContext ' , ( ) => {
360
+ describe ( 'extractTraceDataFromMetaTags() ' , ( ) => {
356
361
it ( 'correctly parses a valid sentry-trace meta header' , ( ) => {
357
362
document . head . innerHTML =
358
363
'<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' ;
359
364
360
- const headerContext = getHeaderContext ( ) ;
365
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
361
366
362
367
expect ( headerContext ) . toBeDefined ( ) ;
363
368
expect ( headerContext ! . traceId ) . toEqual ( '12312012123120121231201212312012' ) ;
364
369
expect ( headerContext ! . parentSpanId ) . toEqual ( '1121201211212012' ) ;
365
370
expect ( headerContext ! . parentSampled ) . toEqual ( false ) ;
366
371
} ) ;
367
372
368
- it ( 'returns undefined if the header is malformed' , ( ) => {
373
+ it ( 'correctly parses a valid baggage meta header' , ( ) => {
374
+ document . head . innerHTML = '<meta name="baggage" content="sentry-release=2.1.12,foo=bar">' ;
375
+
376
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
377
+
378
+ expect ( headerContext ) . toBeDefined ( ) ;
379
+ expect ( headerContext ?. metadata ?. baggage ) . toBeDefined ( ) ;
380
+ const baggage = headerContext ?. metadata ?. baggage ! ;
381
+ expect ( baggage [ 0 ] ) . toBeDefined ( ) ;
382
+ expect ( baggage [ 0 ] ) . toEqual ( {
383
+ release : '2.1.12' ,
384
+ } as BaggageObj ) ;
385
+ expect ( baggage [ 1 ] ) . toBeDefined ( ) ;
386
+ expect ( baggage [ 1 ] ) . toEqual ( 'foo=bar' ) ;
387
+ } ) ;
388
+
389
+ it ( 'returns undefined if the sentry-trace header is malformed' , ( ) => {
369
390
document . head . innerHTML = '<meta name="sentry-trace" content="12312012-112120121-0">' ;
370
391
371
- const headerContext = getHeaderContext ( ) ;
392
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
372
393
373
394
expect ( headerContext ) . toBeUndefined ( ) ;
374
395
} ) ;
375
396
397
+ it ( 'does not crash if the baggage header is malformed' , ( ) => {
398
+ document . head . innerHTML = '<meta name="baggage" content="sentry-relase:2.1.13;foo-bar">' ;
399
+
400
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
401
+
402
+ // TODO currently this creates invalid baggage. This must be adressed in a follow-up PR
403
+ expect ( headerContext ) . toBeDefined ( ) ;
404
+ expect ( headerContext ?. metadata ?. baggage ) . toBeDefined ( ) ;
405
+ const baggage = headerContext ?. metadata ?. baggage ! ;
406
+ expect ( baggage [ 0 ] ) . toBeDefined ( ) ;
407
+ //expect(baggage[0]).toEqual);
408
+ expect ( baggage [ 1 ] ) . toBeDefined ( ) ;
409
+ } ) ;
410
+
376
411
it ( "returns undefined if the header isn't there" , ( ) => {
377
412
document . head . innerHTML = '<meta name="dogs" content="12312012123120121231201212312012-1121201211212012-0">' ;
378
413
379
- const headerContext = getHeaderContext ( ) ;
414
+ const headerContext = extractTraceDataFromMetaTags ( ) ;
380
415
381
416
expect ( headerContext ) . toBeUndefined ( ) ;
382
417
} ) ;
383
418
} ) ;
384
419
385
420
describe ( 'using the data' , ( ) => {
386
- it ( 'uses the data for pageload transactions' , ( ) => {
421
+ it ( 'uses the tracing data for pageload transactions' , ( ) => {
387
422
// make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
388
423
document . head . innerHTML =
389
- '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' ;
424
+ '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' +
425
+ '<meta name="baggage" content="sentry-release=2.1.14,foo=bar">' ;
390
426
391
427
// pageload transactions are created as part of the BrowserTracing integration's initialization
392
428
createBrowserTracing ( true ) ;
393
429
const transaction = getActiveTransaction ( hub ) as IdleTransaction ;
430
+ const baggage = transaction . getBaggage ( ) ! ;
394
431
395
432
expect ( transaction ) . toBeDefined ( ) ;
396
433
expect ( transaction . op ) . toBe ( 'pageload' ) ;
397
434
expect ( transaction . traceId ) . toEqual ( '12312012123120121231201212312012' ) ;
398
435
expect ( transaction . parentSpanId ) . toEqual ( '1121201211212012' ) ;
399
436
expect ( transaction . sampled ) . toBe ( false ) ;
437
+ expect ( baggage ) . toBeDefined ( ) ;
438
+ expect ( baggage [ 0 ] ) . toBeDefined ( ) ;
439
+ expect ( baggage [ 0 ] ) . toEqual ( { release : '2.1.14' } ) ;
440
+ expect ( baggage [ 1 ] ) . toBeDefined ( ) ;
441
+ expect ( baggage [ 1 ] ) . toEqual ( 'foo=bar' ) ;
400
442
} ) ;
401
443
402
444
it ( 'ignores the data for navigation transactions' , ( ) => {
403
445
mockChangeHistory = ( ) => undefined ;
404
446
document . head . innerHTML =
405
- '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' ;
447
+ '<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' +
448
+ '<meta name="baggage" content="sentry-release=2.1.14,foo=bar">' ;
406
449
407
450
createBrowserTracing ( true ) ;
408
451
409
452
mockChangeHistory ( { to : 'here' , from : 'there' } ) ;
410
453
const transaction = getActiveTransaction ( hub ) as IdleTransaction ;
454
+ const baggage = transaction . getBaggage ( ) ! ;
411
455
412
456
expect ( transaction ) . toBeDefined ( ) ;
413
457
expect ( transaction . op ) . toBe ( 'navigation' ) ;
414
458
expect ( transaction . traceId ) . not . toEqual ( '12312012123120121231201212312012' ) ;
415
459
expect ( transaction . parentSpanId ) . toBeUndefined ( ) ;
460
+ expect ( baggage ) . toBeUndefined ( ) ;
416
461
} ) ;
417
462
} ) ;
418
463
} ) ;
0 commit comments