@@ -9,6 +9,7 @@ import { addExtensionMethods } from '../src/hubextensions';
9
9
10
10
addExtensionMethods ( ) ;
11
11
12
+ const mathRandom = jest . spyOn ( Math , 'random' ) ;
12
13
describe ( 'Hub' , ( ) => {
13
14
beforeEach ( ( ) => {
14
15
jest . spyOn ( logger , 'warn' ) ;
@@ -178,6 +179,24 @@ describe('Hub', () => {
178
179
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
179
180
} ) ;
180
181
182
+ it ( 'should set sampled = false if tracesSampler returns 0' , ( ) => {
183
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 0 ) ;
184
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
185
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
186
+
187
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
188
+ expect ( transaction . sampled ) . toBe ( false ) ;
189
+ } ) ;
190
+
191
+ it ( 'should set sampled = true if tracesSampler returns 1' , ( ) => {
192
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 1 ) ;
193
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
194
+ const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
195
+
196
+ expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
197
+ expect ( transaction . sampled ) . toBe ( true ) ;
198
+ } ) ;
199
+
181
200
it ( 'should prefer tracesSampler to tracesSampleRate' , ( ) => {
182
201
// make the two options do opposite things to prove precedence
183
202
const tracesSampler = jest . fn ( ) . mockReturnValue ( true ) ;
@@ -296,7 +315,24 @@ describe('Hub', () => {
296
315
// TODO fix this and write the test
297
316
} ) ;
298
317
299
- it ( "should inherit parent's sampling decision when creating a new transaction if tracesSampler is undefined" , ( ) => {
318
+ it ( "should inherit parent's positive sampling decision if tracesSampler is undefined" , ( ) => {
319
+ // we know that without inheritance we'll get sampled = false (since our "random" number won't be below the
320
+ // sample rate), so make parent's decision the opposite to prove that inheritance takes precedence over
321
+ // tracesSampleRate
322
+ mathRandom . mockReturnValueOnce ( 1 ) ;
323
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0.5 } ) ) ;
324
+ const parentSamplingDecsion = true ;
325
+
326
+ const transaction = hub . startTransaction ( {
327
+ name : 'dogpark' ,
328
+ parentSpanId : '12312012' ,
329
+ parentSampled : parentSamplingDecsion ,
330
+ } ) ;
331
+
332
+ expect ( transaction . sampled ) . toBe ( parentSamplingDecsion ) ;
333
+ } ) ;
334
+
335
+ it ( "should inherit parent's negative sampling decision if tracesSampler is undefined" , ( ) => {
300
336
// tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the
301
337
// opposite to prove that inheritance takes precedence over tracesSampleRate
302
338
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
@@ -311,7 +347,7 @@ describe('Hub', () => {
311
347
expect ( transaction . sampled ) . toBe ( parentSamplingDecsion ) ;
312
348
} ) ;
313
349
314
- it ( "should ignore parent's sampling decision when tracesSampler is defined" , ( ) => {
350
+ it ( "should ignore parent's positive sampling decision when tracesSampler is defined" , ( ) => {
315
351
// this tracesSampler causes every transaction to end up with sampled = true, so make parent's decision the
316
352
// opposite to prove that tracesSampler takes precedence over inheritance
317
353
const tracesSampler = ( ) => true ;
@@ -327,6 +363,23 @@ describe('Hub', () => {
327
363
328
364
expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ;
329
365
} ) ;
366
+
367
+ it ( "should ignore parent's negative sampling decision when tracesSampler is defined" , ( ) => {
368
+ // this tracesSampler causes every transaction to end up with sampled = false, so make parent's decision the
369
+ // opposite to prove that tracesSampler takes precedence over inheritance
370
+ const tracesSampler = ( ) => false ;
371
+ const parentSamplingDecsion = true ;
372
+
373
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
374
+
375
+ const transaction = hub . startTransaction ( {
376
+ name : 'dogpark' ,
377
+ parentSpanId : '12312012' ,
378
+ parentSampled : parentSamplingDecsion ,
379
+ } ) ;
380
+
381
+ expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ;
382
+ } ) ;
330
383
} ) ;
331
384
} ) ;
332
385
} ) ;
0 commit comments