@@ -33,6 +33,8 @@ describe('Hub', () => {
33
33
it ( 'should find a transaction which has been set on the scope if sampled = true' , ( ) => {
34
34
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
35
35
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
36
+ transaction . sampled = true ;
37
+
36
38
hub . configureScope ( scope => {
37
39
scope . setSpan ( transaction ) ;
38
40
} ) ;
@@ -42,7 +44,9 @@ describe('Hub', () => {
42
44
43
45
it ( 'should find a transaction which has been set on the scope if sampled = false' , ( ) => {
44
46
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
45
- const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
47
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
48
+ transaction . sampled = false ;
49
+
46
50
hub . configureScope ( scope => {
47
51
scope . setSpan ( transaction ) ;
48
52
} ) ;
@@ -59,33 +63,6 @@ describe('Hub', () => {
59
63
} ) ;
60
64
61
65
describe ( 'transaction sampling' , ( ) => {
62
- describe ( 'tracesSampleRate and tracesSampler options' , ( ) => {
63
- it ( "should call tracesSampler if it's defined" , ( ) => {
64
- const tracesSampler = jest . fn ( ) ;
65
- const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
66
- hub . startTransaction ( { name : 'dogpark' } ) ;
67
-
68
- expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
69
- } ) ;
70
-
71
- it ( 'should prefer tracesSampler to tracesSampleRate' , ( ) => {
72
- const tracesSampler = jest . fn ( ) ;
73
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 , tracesSampler } ) ) ;
74
- hub . startTransaction ( { name : 'dogpark' } ) ;
75
-
76
- expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
77
- } ) ;
78
-
79
- it ( 'tolerates tracesSampler returning a boolean' , ( ) => {
80
- const tracesSampler = jest . fn ( ) . mockReturnValue ( true ) ;
81
- const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
82
- const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
83
-
84
- expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
85
- expect ( transaction . sampled ) . toBe ( true ) ;
86
- } ) ;
87
- } ) ;
88
-
89
66
describe ( 'default sample context' , ( ) => {
90
67
it ( 'should extract request data for default sampling context when in node' , ( ) => {
91
68
// make sure we look like we're in node
@@ -201,25 +178,78 @@ describe('Hub', () => {
201
178
expect ( transaction . sampled ) . toBe ( false ) ;
202
179
} ) ;
203
180
204
- it ( 'should not try to override sampling decision provided in transaction context' , ( ) => {
205
- // setting tracesSampleRate to 1 means that without the override, the sampling decision should be true
181
+ it ( 'should set sampled = false if tracesSampleRate is 0' , ( ) => {
182
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0 } ) ) ;
183
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
184
+
185
+ expect ( transaction . sampled ) . toBe ( false ) ;
186
+ } ) ;
187
+
188
+ it ( 'should set sampled = true if tracesSampleRate is 1' , ( ) => {
206
189
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
207
- const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
190
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
208
191
192
+ expect ( transaction . sampled ) . toBe ( true ) ;
193
+ } ) ;
194
+
195
+ it ( "should call tracesSampler if it's defined" , ( ) => {
196
+ const tracesSampler = jest . fn ( ) ;
197
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
198
+ hub . startTransaction ( { name : 'dogpark' } ) ;
199
+
200
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
201
+ } ) ;
202
+
203
+ it ( 'should set sampled = false if tracesSampler returns 0' , ( ) => {
204
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 0 ) ;
205
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
206
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
207
+
208
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
209
209
expect ( transaction . sampled ) . toBe ( false ) ;
210
210
} ) ;
211
211
212
- it ( 'should not sample transactions when tracesSampleRate is 0' , ( ) => {
213
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0 } ) ) ;
212
+ it ( 'should set sampled = true if tracesSampler returns 1' , ( ) => {
213
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 1 ) ;
214
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
214
215
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
215
216
217
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
218
+ expect ( transaction . sampled ) . toBe ( true ) ;
219
+ } ) ;
220
+
221
+ it ( 'should not try to override positive sampling decision provided in transaction context' , ( ) => {
222
+ // so that the decision otherwise would be false
223
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 0 ) ;
224
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
225
+ const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : true } ) ;
226
+
227
+ expect ( transaction . sampled ) . toBe ( true ) ;
228
+ } ) ;
229
+
230
+ it ( 'should not try to override negative sampling decision provided in transaction context' , ( ) => {
231
+ // so that the decision otherwise would be true
232
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 1 ) ;
233
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
234
+ const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
235
+
216
236
expect ( transaction . sampled ) . toBe ( false ) ;
217
237
} ) ;
218
238
219
- it ( 'should sample transactions when tracesSampleRate is 1' , ( ) => {
220
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
239
+ it ( 'should prefer tracesSampler to tracesSampleRate' , ( ) => {
240
+ const tracesSampler = jest . fn ( ) ;
241
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 , tracesSampler } ) ) ;
242
+ hub . startTransaction ( { name : 'dogpark' } ) ;
243
+
244
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
245
+ } ) ;
246
+
247
+ it ( 'should tolerate tracesSampler returning a boolean' , ( ) => {
248
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( true ) ;
249
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
221
250
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
222
251
252
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
223
253
expect ( transaction . sampled ) . toBe ( true ) ;
224
254
} ) ;
225
255
} ) ;
@@ -386,11 +416,21 @@ describe('Hub', () => {
386
416
expect ( extractTraceparentData ( headers [ 'sentry-trace' ] ) ! . parentSampled ) . toBe ( false ) ;
387
417
} ) ;
388
418
389
- it ( 'should propagate sampling decision to child transactions in fetch header' , ( ) => {
419
+ it ( 'should propagate positive sampling decision to child transactions in fetch header' , ( ) => {
420
+ // TODO (kmclb)
421
+ } ) ;
422
+
423
+ it ( 'should propagate negative sampling decision to child transactions in fetch header' , ( ) => {
390
424
// TODO (kmclb)
391
425
} ) ;
392
426
393
- it ( "should inherit parent's sampling decision when creating a new transaction if tracesSampler is undefined" , ( ) => {
427
+ it ( "should inherit parent's positive sampling decision if tracesSampler is undefined" , ( ) => {
428
+ // This is untestable, because there's no way to guarantee that a non-inherited decision is false (thus proving
429
+ // the parent's positive decision takes precedence) without using tracesSampler, since setting tracesSampleRate
430
+ // to 0 here will disable tracing all together. See below for the opposite test, though.
431
+ } ) ;
432
+
433
+ it ( "should inherit parent's negative sampling decision if tracesSampler is undefined" , ( ) => {
394
434
// tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the
395
435
// opposite to prove that inheritance takes precedence over tracesSampleRate
396
436
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
@@ -405,7 +445,7 @@ describe('Hub', () => {
405
445
expect ( transaction . sampled ) . toBe ( parentSamplingDecsion ) ;
406
446
} ) ;
407
447
408
- it ( "should ignore parent's sampling decision when tracesSampler is defined" , ( ) => {
448
+ it ( "should ignore parent's positive sampling decision when tracesSampler is defined" , ( ) => {
409
449
// this tracesSampler causes every transaction to end up with sampled = true, so make parent's decision the
410
450
// opposite to prove that tracesSampler takes precedence over inheritance
411
451
const tracesSampler = ( ) => true ;
@@ -421,6 +461,23 @@ describe('Hub', () => {
421
461
422
462
expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ;
423
463
} ) ;
464
+
465
+ it ( "should ignore parent's negative sampling decision when tracesSampler is defined" , ( ) => {
466
+ // this tracesSampler causes every transaction to end up with sampled = false, so make parent's decision the
467
+ // opposite to prove that tracesSampler takes precedence over inheritance
468
+ const tracesSampler = ( ) => false ;
469
+ const parentSamplingDecsion = true ;
470
+
471
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
472
+
473
+ const transaction = hub . startTransaction ( {
474
+ name : 'dogpark' ,
475
+ parentSpanId : '12312012' ,
476
+ parentSampled : parentSamplingDecsion ,
477
+ } ) ;
478
+
479
+ expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ;
480
+ } ) ;
424
481
} ) ;
425
482
} ) ;
426
483
} ) ;
0 commit comments