Skip to content

Commit 1d4a641

Browse files
author
Luca Forstner
committed
unit tests
1 parent 9b394c3 commit 1d4a641

File tree

18 files changed

+117
-47
lines changed

18 files changed

+117
-47
lines changed

packages/browser/test/tracing/browserTracingIntegration.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,15 +644,19 @@ describe('browserTracingIntegration', () => {
644644

645645
expect(oldCurrentScopePropCtx).toEqual({
646646
traceId: expect.stringMatching(/[a-f0-9]{32}/),
647+
sampleRand: expect.any(Number),
647648
});
648649
expect(oldIsolationScopePropCtx).toEqual({
649650
traceId: expect.stringMatching(/[a-f0-9]{32}/),
651+
sampleRand: expect.any(Number),
650652
});
651653
expect(newCurrentScopePropCtx).toEqual({
652654
traceId: expect.stringMatching(/[a-f0-9]{32}/),
655+
sampleRand: expect.any(Number),
653656
});
654657
expect(newIsolationScopePropCtx).toEqual({
655658
traceId: expect.stringMatching(/[a-f0-9]{32}/),
659+
sampleRand: expect.any(Number),
656660
});
657661

658662
expect(newIsolationScopePropCtx.traceId).not.toEqual(oldIsolationScopePropCtx.traceId);
@@ -676,6 +680,7 @@ describe('browserTracingIntegration', () => {
676680

677681
const propCtxBeforeEnd = getCurrentScope().getPropagationContext();
678682
expect(propCtxBeforeEnd).toStrictEqual({
683+
sampleRand: expect.any(Number),
679684
traceId: expect.stringMatching(/[a-f0-9]{32}/),
680685
});
681686

@@ -685,13 +690,15 @@ describe('browserTracingIntegration', () => {
685690
expect(propCtxAfterEnd).toStrictEqual({
686691
traceId: propCtxBeforeEnd.traceId,
687692
sampled: true,
693+
sampleRand: expect.any(Number),
688694
dsc: {
689695
environment: 'production',
690696
public_key: 'examplePublicKey',
691697
sample_rate: '1',
692698
sampled: 'true',
693699
transaction: 'mySpan',
694700
trace_id: propCtxBeforeEnd.traceId,
701+
sample_rand: expect.any(String),
695702
},
696703
});
697704
});
@@ -714,6 +721,7 @@ describe('browserTracingIntegration', () => {
714721
const propCtxBeforeEnd = getCurrentScope().getPropagationContext();
715722
expect(propCtxBeforeEnd).toStrictEqual({
716723
traceId: expect.stringMatching(/[a-f0-9]{32}/),
724+
sampleRand: expect.any(Number),
717725
});
718726

719727
navigationSpan!.end();
@@ -722,13 +730,15 @@ describe('browserTracingIntegration', () => {
722730
expect(propCtxAfterEnd).toStrictEqual({
723731
traceId: propCtxBeforeEnd.traceId,
724732
sampled: false,
733+
sampleRand: expect.any(Number),
725734
dsc: {
726735
environment: 'production',
727736
public_key: 'examplePublicKey',
728737
sample_rate: '0',
729738
sampled: 'false',
730739
transaction: 'mySpan',
731740
trace_id: propCtxBeforeEnd.traceId,
741+
sample_rand: expect.any(String),
732742
},
733743
});
734744
});
@@ -739,7 +749,7 @@ describe('browserTracingIntegration', () => {
739749
// make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
740750
document.head.innerHTML =
741751
'<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">' +
742-
'<meta name="baggage" content="sentry-release=2.1.14,foo=bar">';
752+
'<meta name="baggage" content="sentry-release=2.1.14,foo=bar,sentry-sample_rand=0.123">';
743753

744754
const client = new BrowserClient(
745755
getDefaultBrowserClientOptions({
@@ -765,11 +775,12 @@ describe('browserTracingIntegration', () => {
765775
expect(spanIsSampled(idleSpan)).toBe(false);
766776

767777
expect(dynamicSamplingContext).toBeDefined();
768-
expect(dynamicSamplingContext).toStrictEqual({ release: '2.1.14' });
778+
expect(dynamicSamplingContext).toStrictEqual({ release: '2.1.14', sample_rand: '0.123' });
769779

770780
// Propagation context keeps the meta tag trace data for later events on the same route to add them to the trace
771781
expect(propagationContext.traceId).toEqual('12312012123120121231201212312012');
772782
expect(propagationContext.parentSpanId).toEqual('1121201211212012');
783+
expect(propagationContext.sampleRand).toBe(0.123);
773784
});
774785

775786
it('puts frozen Dynamic Sampling Context on pageload span if sentry-trace data and only 3rd party baggage is present', () => {
@@ -849,6 +860,7 @@ describe('browserTracingIntegration', () => {
849860
public_key: 'examplePublicKey',
850861
sample_rate: '1',
851862
sampled: 'true',
863+
sample_rand: expect.any(String),
852864
trace_id: expect.not.stringContaining('12312012123120121231201212312012'),
853865
});
854866

@@ -861,7 +873,7 @@ describe('browserTracingIntegration', () => {
861873
// make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one
862874
document.head.innerHTML =
863875
'<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-1">' +
864-
'<meta name="baggage" content="sentry-release=2.1.14,foo=bar">';
876+
'<meta name="baggage" content="sentry-release=2.1.14,foo=bar,sentry-sample_rand=0.555">';
865877

866878
const client = new BrowserClient(
867879
getDefaultBrowserClientOptions({
@@ -881,7 +893,7 @@ describe('browserTracingIntegration', () => {
881893
},
882894
{
883895
sentryTrace: '12312012123120121231201212312011-1121201211212011-1',
884-
baggage: 'sentry-release=2.2.14,foo=bar',
896+
baggage: 'sentry-release=2.2.14,foo=bar,sentry-sample_rand=0.123',
885897
},
886898
);
887899

@@ -898,11 +910,12 @@ describe('browserTracingIntegration', () => {
898910
expect(spanIsSampled(idleSpan)).toBe(true);
899911

900912
expect(dynamicSamplingContext).toBeDefined();
901-
expect(dynamicSamplingContext).toStrictEqual({ release: '2.2.14' });
913+
expect(dynamicSamplingContext).toStrictEqual({ release: '2.2.14', sample_rand: '0.123' });
902914

903915
// Propagation context keeps the custom trace data for later events on the same route to add them to the trace
904916
expect(propagationContext.traceId).toEqual('12312012123120121231201212312011');
905917
expect(propagationContext.parentSpanId).toEqual('1121201211212011');
918+
expect(propagationContext.sampleRand).toEqual(0.123);
906919
});
907920
});
908921

packages/core/src/types-hoist/tracing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export interface PropagationContext {
1717
traceId: string;
1818

1919
/**
20-
* TODO
20+
* A random between 0 an 1 (including 0, excluding 1) used for sampling in the current execution context.
21+
* This should be newly generated when a new trace is started.
2122
*/
2223
sampleRand: number;
2324

packages/core/test/lib/feedback.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ describe('captureFeedback', () => {
262262
traceId,
263263
parentSpanId: spanId,
264264
dsc,
265+
sampleRand: 0.42,
265266
});
266267

267268
const eventId = captureFeedback({
@@ -351,6 +352,7 @@ describe('captureFeedback', () => {
351352
sampled: 'true',
352353
sample_rate: '1',
353354
transaction: 'test-span',
355+
sample_rand: expect.any(String),
354356
},
355357
},
356358
[

packages/core/test/lib/prepareEvent.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ describe('parseEventHintOrCaptureContext', () => {
238238
fingerprint: ['xx', 'yy'],
239239
propagationContext: {
240240
traceId: 'xxx',
241+
sampleRand: Math.random(),
241242
},
242243
};
243244

@@ -328,7 +329,7 @@ describe('prepareEvent', () => {
328329
tags: { tag1: 'aa', tag2: 'aa' },
329330
extra: { extra1: 'aa', extra2: 'aa' },
330331
contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } },
331-
propagationContext: { traceId: '1' },
332+
propagationContext: { traceId: '1', sampleRand: 0.42 },
332333
fingerprint: ['aa'],
333334
});
334335
scope.addBreadcrumb(breadcrumb1);

packages/core/test/lib/scope.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('Scope', () => {
3232
eventProcessors: [],
3333
propagationContext: {
3434
traceId: expect.any(String),
35+
sampleRand: expect.any(Number),
3536
},
3637
sdkProcessingMetadata: {},
3738
});
@@ -57,6 +58,7 @@ describe('Scope', () => {
5758
eventProcessors: [],
5859
propagationContext: {
5960
traceId: expect.any(String),
61+
sampleRand: expect.any(Number),
6062
},
6163
sdkProcessingMetadata: {},
6264
});
@@ -90,6 +92,7 @@ describe('Scope', () => {
9092
eventProcessors: [],
9193
propagationContext: {
9294
traceId: expect.any(String),
95+
sampleRand: expect.any(Number),
9396
},
9497
sdkProcessingMetadata: {},
9598
});
@@ -101,6 +104,7 @@ describe('Scope', () => {
101104

102105
expect(scope.getScopeData().propagationContext).toEqual({
103106
traceId: expect.any(String),
107+
sampleRand: expect.any(Number),
104108
sampled: undefined,
105109
dsc: undefined,
106110
parentSpanId: undefined,
@@ -228,12 +232,14 @@ describe('Scope', () => {
228232
const oldPropagationContext = scope.getPropagationContext();
229233
scope.setPropagationContext({
230234
traceId: '86f39e84263a4de99c326acab3bfe3bd',
235+
sampleRand: 0.42,
231236
sampled: true,
232237
});
233238
expect(scope.getPropagationContext()).not.toEqual(oldPropagationContext);
234239
expect(scope.getPropagationContext()).toEqual({
235240
traceId: '86f39e84263a4de99c326acab3bfe3bd',
236241
sampled: true,
242+
sampleRand: 0.42,
237243
});
238244
});
239245

@@ -293,6 +299,7 @@ describe('Scope', () => {
293299
expect(scope['_propagationContext']).toEqual({
294300
traceId: expect.any(String),
295301
sampled: undefined,
302+
sampleRand: expect.any(Number),
296303
});
297304
expect(scope['_propagationContext']).not.toEqual(oldPropagationContext);
298305
});
@@ -420,6 +427,7 @@ describe('Scope', () => {
420427
propagationContext: {
421428
traceId: '8949daf83f4a4a70bee4c1eb9ab242ed',
422429
sampled: true,
430+
sampleRand: 0.42,
423431
},
424432
};
425433

@@ -446,6 +454,7 @@ describe('Scope', () => {
446454
expect(updatedScope._propagationContext).toEqual({
447455
traceId: '8949daf83f4a4a70bee4c1eb9ab242ed',
448456
sampled: true,
457+
sampleRand: 0.42,
449458
});
450459
});
451460
});
@@ -493,7 +502,7 @@ describe('Scope', () => {
493502
tags: { tag1: 'aa', tag2: 'aa' },
494503
extra: { extra1: 'aa', extra2: 'aa' },
495504
contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } },
496-
propagationContext: { traceId: '1' },
505+
propagationContext: { traceId: '1', sampleRand: 0.42 },
497506
fingerprint: ['aa'],
498507
});
499508
scope.addBreadcrumb(breadcrumb1);

packages/core/test/lib/tracing/dynamicSamplingContext.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe('getDynamicSamplingContextFromSpan', () => {
7171
sample_rate: '0.56',
7272
trace_id: expect.stringMatching(/^[a-f0-9]{32}$/),
7373
transaction: 'tx',
74+
sample_rand: expect.any(String),
7475
});
7576
});
7677

@@ -88,6 +89,7 @@ describe('getDynamicSamplingContextFromSpan', () => {
8889
sample_rate: '1',
8990
trace_id: expect.stringMatching(/^[a-f0-9]{32}$/),
9091
transaction: 'tx',
92+
sample_rand: expect.any(String),
9193
});
9294
});
9395

@@ -110,6 +112,7 @@ describe('getDynamicSamplingContextFromSpan', () => {
110112
sample_rate: '0.56',
111113
trace_id: expect.stringMatching(/^[a-f0-9]{32}$/),
112114
transaction: 'tx',
115+
sample_rand: undefined, // this is a bit funky admittedly
113116
});
114117
});
115118

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ describe('startSpan', () => {
473473
sample_rate: '1',
474474
transaction: 'outer transaction',
475475
sampled: 'true',
476+
sample_rand: expect.any(String),
476477
},
477478
});
478479

@@ -498,6 +499,7 @@ describe('startSpan', () => {
498499
sample_rate: '1',
499500
transaction: 'outer transaction',
500501
sampled: 'true',
502+
sample_rand: expect.any(String),
501503
},
502504
});
503505
});
@@ -507,6 +509,7 @@ describe('startSpan', () => {
507509
withScope(scope => {
508510
scope.setPropagationContext({
509511
traceId: '99999999999999999999999999999999',
512+
sampleRand: Math.random(),
510513
dsc: {},
511514
parentSpanId: '4242424242424242',
512515
});
@@ -902,6 +905,7 @@ describe('startSpanManual', () => {
902905
sample_rate: '1',
903906
transaction: 'outer transaction',
904907
sampled: 'true',
908+
sample_rand: expect.any(String),
905909
},
906910
});
907911

@@ -927,6 +931,7 @@ describe('startSpanManual', () => {
927931
sample_rate: '1',
928932
transaction: 'outer transaction',
929933
sampled: 'true',
934+
sample_rand: expect.any(String),
930935
},
931936
});
932937
});
@@ -945,6 +950,7 @@ describe('startSpanManual', () => {
945950
withScope(scope => {
946951
scope.setPropagationContext({
947952
traceId: '99999999999999999999999999999991',
953+
sampleRand: Math.random(),
948954
dsc: {},
949955
parentSpanId: '4242424242424242',
950956
});
@@ -1230,6 +1236,7 @@ describe('startInactiveSpan', () => {
12301236
sample_rate: '1',
12311237
transaction: 'outer transaction',
12321238
sampled: 'true',
1239+
sample_rand: expect.any(String),
12331240
},
12341241
});
12351242

@@ -1255,6 +1262,7 @@ describe('startInactiveSpan', () => {
12551262
sample_rate: '1',
12561263
transaction: 'outer transaction',
12571264
sampled: 'true',
1265+
sample_rand: expect.any(String),
12581266
},
12591267
});
12601268
});
@@ -1269,6 +1277,7 @@ describe('startInactiveSpan', () => {
12691277
withScope(scope => {
12701278
scope.setPropagationContext({
12711279
traceId: '99999999999999999999999999999991',
1280+
sampleRand: Math.random(),
12721281
dsc: {},
12731282
parentSpanId: '4242424242424242',
12741283
});
@@ -1451,6 +1460,7 @@ describe('continueTrace', () => {
14511460
expect(scope.getPropagationContext()).toEqual({
14521461
sampled: undefined,
14531462
traceId: expect.any(String),
1463+
sampleRand: expect.any(Number),
14541464
});
14551465

14561466
expect(scope.getScopeData().sdkProcessingMetadata).toEqual({});
@@ -1472,6 +1482,7 @@ describe('continueTrace', () => {
14721482
sampled: false,
14731483
parentSpanId: '1121201211212012',
14741484
traceId: '12312012123120121231201212312012',
1485+
sampleRand: expect.any(Number),
14751486
});
14761487

14771488
expect(scope.getScopeData().sdkProcessingMetadata).toEqual({});
@@ -1492,10 +1503,12 @@ describe('continueTrace', () => {
14921503
dsc: {
14931504
environment: 'production',
14941505
version: '1.0',
1506+
sample_rand: expect.any(String),
14951507
},
14961508
sampled: true,
14971509
parentSpanId: '1121201211212012',
14981510
traceId: '12312012123120121231201212312012',
1511+
sampleRand: expect.any(Number),
14991512
});
15001513

15011514
expect(scope.getScopeData().sdkProcessingMetadata).toEqual({});
@@ -1516,10 +1529,12 @@ describe('continueTrace', () => {
15161529
dsc: {
15171530
environment: 'production',
15181531
version: '1.0',
1532+
sample_rand: expect.any(String),
15191533
},
15201534
sampled: true,
15211535
parentSpanId: '1121201211212012',
15221536
traceId: '12312012123120121231201212312012',
1537+
sampleRand: expect.any(Number),
15231538
});
15241539

15251540
expect(scope.getScopeData().sdkProcessingMetadata).toEqual({});

0 commit comments

Comments
 (0)