@@ -660,30 +660,57 @@ describe('startSpan', () => {
660
660
} ) ;
661
661
} ) ;
662
662
663
- it ( 'samples with a tracesSampler ', ( ) => {
664
- const tracesSampler = vi . fn ( ( ) => {
663
+ describe ( 'uses tracesSampler if defined ', ( ) => {
664
+ const tracesSampler = vi . fn < ( ) => boolean | number > ( ( ) => {
665
665
return true ;
666
666
} ) ;
667
667
668
- const options = getDefaultTestClientOptions ( { tracesSampler } ) ;
669
- client = new TestClient ( options ) ;
670
- setCurrentClient ( client ) ;
671
- client . init ( ) ;
668
+ it . each ( [ true , 1 ] ) ( 'returns a positive sampling decision if tracesSampler returns %s' , tracesSamplerResult => {
669
+ tracesSampler . mockReturnValueOnce ( tracesSamplerResult ) ;
672
670
673
- startSpan ( { name : 'outer' , attributes : { test1 : 'aa' , test2 : 'aa' , test3 : 'bb' } } , outerSpan => {
674
- expect ( outerSpan ) . toBeDefined ( ) ;
671
+ const options = getDefaultTestClientOptions ( { tracesSampler } ) ;
672
+ client = new TestClient ( options ) ;
673
+ setCurrentClient ( client ) ;
674
+ client . init ( ) ;
675
+
676
+ startSpan ( { name : 'outer' , attributes : { test1 : 'aa' , test2 : 'aa' , test3 : 'bb' } } , outerSpan => {
677
+ expect ( outerSpan ) . toBeDefined ( ) ;
678
+ expect ( spanIsSampled ( outerSpan ) ) . toBe ( true ) ;
679
+ } ) ;
680
+
681
+ expect ( tracesSampler ) . toBeCalledTimes ( 1 ) ;
682
+ expect ( tracesSampler ) . toHaveBeenLastCalledWith ( {
683
+ parentSampled : undefined ,
684
+ name : 'outer' ,
685
+ attributes : {
686
+ test1 : 'aa' ,
687
+ test2 : 'aa' ,
688
+ test3 : 'bb' ,
689
+ } ,
690
+ inheritOrSampleWith : expect . any ( Function ) ,
691
+ } ) ;
675
692
} ) ;
676
693
677
- expect ( tracesSampler ) . toBeCalledTimes ( 1 ) ;
678
- expect ( tracesSampler ) . toHaveBeenLastCalledWith ( {
679
- parentSampled : undefined ,
680
- name : 'outer' ,
681
- attributes : {
682
- test1 : 'aa' ,
683
- test2 : 'aa' ,
684
- test3 : 'bb' ,
685
- } ,
686
- inheritOrSampleWith : expect . any ( Function ) ,
694
+ it . each ( [ false , 0 ] ) ( 'returns a negative sampling decision if tracesSampler returns %s' , tracesSamplerResult => {
695
+ tracesSampler . mockReturnValueOnce ( tracesSamplerResult ) ;
696
+
697
+ const options = getDefaultTestClientOptions ( { tracesSampler } ) ;
698
+ client = new TestClient ( options ) ;
699
+ setCurrentClient ( client ) ;
700
+ client . init ( ) ;
701
+
702
+ startSpan ( { name : 'outer' } , outerSpan => {
703
+ expect ( outerSpan ) . toBeDefined ( ) ;
704
+ expect ( spanIsSampled ( outerSpan ) ) . toBe ( false ) ;
705
+ } ) ;
706
+
707
+ expect ( tracesSampler ) . toBeCalledTimes ( 1 ) ;
708
+ expect ( tracesSampler ) . toHaveBeenLastCalledWith ( {
709
+ parentSampled : undefined ,
710
+ attributes : { } ,
711
+ name : 'outer' ,
712
+ inheritOrSampleWith : expect . any ( Function ) ,
713
+ } ) ;
687
714
} ) ;
688
715
} ) ;
689
716
0 commit comments