15
15
*/
16
16
17
17
import * as assert from 'assert' ;
18
+ import * as api from '@opentelemetry/api' ;
18
19
import {
19
20
ProbabilitySampler ,
20
21
ALWAYS_SAMPLER ,
@@ -24,60 +25,82 @@ import {
24
25
describe ( 'ProbabilitySampler' , ( ) => {
25
26
it ( 'should return a always sampler for 1' , ( ) => {
26
27
const sampler = new ProbabilitySampler ( 1 ) ;
27
- assert . strictEqual ( sampler . shouldSample ( ) , true ) ;
28
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
29
+ decision : api . SamplingDecision . RECORD_AND_SAMPLED ,
30
+ } ) ;
28
31
} ) ;
29
32
30
33
it ( 'should return a always sampler for >1' , ( ) => {
31
34
const sampler = new ProbabilitySampler ( 100 ) ;
32
- assert . strictEqual ( sampler . shouldSample ( ) , true ) ;
35
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
36
+ decision : api . SamplingDecision . RECORD_AND_SAMPLED ,
37
+ } ) ;
33
38
assert . strictEqual ( sampler . toString ( ) , 'ProbabilitySampler{1}' ) ;
34
39
} ) ;
35
40
36
41
it ( 'should return a never sampler for 0' , ( ) => {
37
42
const sampler = new ProbabilitySampler ( 0 ) ;
38
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
43
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
44
+ decision : api . SamplingDecision . NOT_RECORD ,
45
+ } ) ;
39
46
} ) ;
40
47
41
48
it ( 'should return a never sampler for <0' , ( ) => {
42
49
const sampler = new ProbabilitySampler ( - 1 ) ;
43
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
50
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
51
+ decision : api . SamplingDecision . NOT_RECORD ,
52
+ } ) ;
44
53
} ) ;
45
54
46
55
it ( 'should sample according to the probability' , ( ) => {
47
56
Math . random = ( ) => 1 / 10 ;
48
57
const sampler = new ProbabilitySampler ( 0.2 ) ;
49
- assert . strictEqual ( sampler . shouldSample ( ) , true ) ;
58
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
59
+ decision : api . SamplingDecision . RECORD_AND_SAMPLED ,
60
+ } ) ;
50
61
assert . strictEqual ( sampler . toString ( ) , 'ProbabilitySampler{0.2}' ) ;
51
62
52
63
Math . random = ( ) => 5 / 10 ;
53
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
64
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
65
+ decision : api . SamplingDecision . NOT_RECORD ,
66
+ } ) ;
54
67
} ) ;
55
68
56
- it ( 'should return true for ALWAYS_SAMPLER' , ( ) => {
57
- assert . strictEqual ( ALWAYS_SAMPLER . shouldSample ( ) , true ) ;
69
+ it ( 'should return api.SamplingDecision.RECORD_AND_SAMPLED for ALWAYS_SAMPLER' , ( ) => {
70
+ assert . deepStrictEqual ( ALWAYS_SAMPLER . shouldSample ( ) , {
71
+ decision : api . SamplingDecision . RECORD_AND_SAMPLED ,
72
+ } ) ;
58
73
assert . strictEqual ( ALWAYS_SAMPLER . toString ( ) , 'ProbabilitySampler{1}' ) ;
59
74
} ) ;
60
75
61
- it ( 'should return false for NEVER_SAMPLER' , ( ) => {
62
- assert . strictEqual ( NEVER_SAMPLER . shouldSample ( ) , false ) ;
76
+ it ( 'should return decision: api.SamplingDecision.NOT_RECORD for NEVER_SAMPLER' , ( ) => {
77
+ assert . deepStrictEqual ( NEVER_SAMPLER . shouldSample ( ) , {
78
+ decision : api . SamplingDecision . NOT_RECORD ,
79
+ } ) ;
63
80
assert . strictEqual ( NEVER_SAMPLER . toString ( ) , 'ProbabilitySampler{0}' ) ;
64
81
} ) ;
65
82
66
83
it ( 'should handle NaN' , ( ) => {
67
84
const sampler = new ProbabilitySampler ( NaN ) ;
68
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
85
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
86
+ decision : api . SamplingDecision . NOT_RECORD ,
87
+ } ) ;
69
88
assert . strictEqual ( sampler . toString ( ) , 'ProbabilitySampler{0}' ) ;
70
89
} ) ;
71
90
72
91
it ( 'should handle -NaN' , ( ) => {
73
92
const sampler = new ProbabilitySampler ( - NaN ) ;
74
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
93
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
94
+ decision : api . SamplingDecision . NOT_RECORD ,
95
+ } ) ;
75
96
assert . strictEqual ( sampler . toString ( ) , 'ProbabilitySampler{0}' ) ;
76
97
} ) ;
77
98
78
99
it ( 'should handle undefined' , ( ) => {
79
100
const sampler = new ProbabilitySampler ( undefined ) ;
80
- assert . strictEqual ( sampler . shouldSample ( ) , false ) ;
101
+ assert . deepStrictEqual ( sampler . shouldSample ( ) , {
102
+ decision : api . SamplingDecision . NOT_RECORD ,
103
+ } ) ;
81
104
assert . strictEqual ( sampler . toString ( ) , 'ProbabilitySampler{0}' ) ;
82
105
} ) ;
83
106
} ) ;
0 commit comments