44
55namespace OpenTelemetry \Tests \SDK \Unit \Trace \Sampler ;
66
7+ use function bin2hex ;
78use InvalidArgumentException ;
89use OpenTelemetry \API \Trace as API ;
910use OpenTelemetry \Context \Context ;
1011use OpenTelemetry \SDK \Common \Attribute \Attributes ;
1112use OpenTelemetry \SDK \Trace \Sampler \TraceIdRatioBasedSampler ;
1213use OpenTelemetry \SDK \Trace \SamplingResult ;
14+ use function pack ;
1315use PHPUnit \Framework \Attributes \CoversClass ;
1416use PHPUnit \Framework \Attributes \DataProvider ;
1517use PHPUnit \Framework \TestCase ;
18+ use function rtrim ;
19+ use function substr ;
1620
1721#[CoversClass(TraceIdRatioBasedSampler::class)]
1822class TraceIdRatioBasedSamplerTest extends TestCase
1923{
20- public function test_should_sample (): void
24+ #[DataProvider('shouldSampleProvider ' )]
25+ public function test_should_sample (string $ traceId , float $ probability , int $ result ): void
2126 {
22- $ sampler = new TraceIdRatioBasedSampler (1.0 );
27+ $ sampler = new TraceIdRatioBasedSampler ($ probability );
2328 $ decision = $ sampler ->shouldSample (
2429 Context::getRoot (),
25- ' 4bf92f3577b34da6a3ce929d0e0e4736 ' ,
30+ $ traceId ,
2631 'test.opentelemetry.io ' ,
2732 API \SpanKind::KIND_INTERNAL ,
2833 Attributes::create ([]),
2934 [],
3035 );
31- $ this ->assertEquals (SamplingResult::RECORD_AND_SAMPLE , $ decision ->getDecision ());
36+ $ this ->assertEquals ($ result , $ decision ->getDecision ());
37+ }
38+
39+ public static function shouldSampleProvider (): iterable
40+ {
41+ yield 'otep-0235 ' => ['123456789123456789d29d6a7215ced0 ' , 0.25 , SamplingResult::RECORD_AND_SAMPLE ];
42+
43+ yield 'tv=0 ' => ['4bf92f3577b34da6a3ce929d0e0e4736 ' , 1.0 , SamplingResult::RECORD_AND_SAMPLE ];
44+ yield 'tv=8 ' => ['4bf92f3577b34da6a3ce929d0e0e4736 ' , 0.5 , SamplingResult::RECORD_AND_SAMPLE ];
45+ yield 'tv=cccd ' => ['4bf92f3577b34da6a3ce929d0e0e4736 ' , 1 / 5 , SamplingResult::RECORD_AND_SAMPLE ];
46+ yield 'tv=d ' => ['4bf92f3577b34da6a3ce929d0e0e4736 ' , 3 / 16 , SamplingResult::DROP ];
47+
48+ yield ['4bf92f3577b34da6a380000000000000 ' , 0.5 , SamplingResult::RECORD_AND_SAMPLE ];
49+ yield ['4bf92f3577b34da6a37fffffffffffff ' , 0.5 , SamplingResult::DROP ];
50+ yield ['4bf92f3577b34da6a3f5560000000000 ' , 1 / 24 , SamplingResult::RECORD_AND_SAMPLE ];
51+ yield ['4bf92f3577b34da6a3f554ffffffffff ' , 1 / 24 , SamplingResult::DROP ];
52+ yield ['4bf92f3577b34da6a3fffffffffffff0 ' , 2 ** -52 , SamplingResult::RECORD_AND_SAMPLE ];
53+ yield ['4bf92f3577b34da6a3ffffffffffffef ' , 2 ** -52 , SamplingResult::DROP ];
54+ yield ['4bf92f3577b34da6a3ffffffffffffff ' , 2 ** -56 , SamplingResult::RECORD_AND_SAMPLE ];
55+ yield ['4bf92f3577b34da6a3fffffffffffffe ' , 2 ** -56 , SamplingResult::DROP ];
56+ yield ['4bf92f3577b34da6a3ffffffffffffff ' , 2 ** -57 , SamplingResult::DROP ];
57+ }
58+
59+ #[DataProvider('computeTValueProvider ' )]
60+ public function test_compute_t_value (string $ expected , float $ probability , int $ precision ): void
61+ {
62+ $ tv = TraceIdRatioBasedSampler::computeTValue ($ probability , $ precision , 4 );
63+ $ this ->assertSame ($ expected , rtrim (bin2hex (substr (pack ('J ' , $ tv ), 1 )), '0 ' ) ?: '0 ' );
64+ }
65+
66+ public static function computeTValueProvider (): iterable
67+ {
68+ // see https://github.com/open-telemetry/opentelemetry-specification/pull/4166
69+ yield from [['0 ' , 1 , 3 ], ['0 ' , 1 , 4 ], ['0 ' , 1 , 5 ]];
70+ yield from [['8 ' , 1 /2 , 3 ], ['8 ' , 1 /2 , 4 ], ['8 ' , 1 /2 , 5 ]];
71+ yield from [['aab ' , 1 /3 , 3 ], ['aaab ' , 1 /3 , 4 ], ['aaaab ' , 1 /3 , 5 ]];
72+ yield from [['c ' , 1 /4 , 3 ], ['c ' , 1 /4 , 4 ], ['c ' , 1 /4 , 5 ]];
73+ yield from [['ccd ' , 1 /5 , 3 ], ['cccd ' , 1 /5 , 4 ], ['ccccd ' , 1 /5 , 5 ]];
74+ yield from [['e ' , 1 /8 , 3 ], ['e ' , 1 /8 , 4 ], ['e ' , 1 /8 , 5 ]];
75+ yield from [['e66 ' , 1 /10 , 3 ], ['e666 ' , 1 /10 , 4 ], ['e6666 ' , 1 /10 , 5 ]];
76+ yield from [['f ' , 1 /16 , 3 ], ['f ' , 1 /16 , 4 ], ['f ' , 1 /16 , 5 ]];
77+ yield from [['fd71 ' , 1 /100 , 3 ], ['fd70a ' , 1 /100 , 4 ], ['fd70a4 ' , 1 /100 , 5 ]];
78+ yield from [['ffbe7 ' , 1 /1000 , 3 ], ['ffbe77 ' , 1 /1000 , 4 ], ['ffbe76d ' , 1 /1000 , 5 ]];
79+ yield from [['fff972 ' , 1 /10000 , 3 ], ['fff9724 ' , 1 /10000 , 4 ], ['fff97247 ' , 1 /10000 , 5 ]];
80+ yield from [['ffff584 ' , 1 /100000 , 3 ], ['ffff583a ' , 1 /100000 , 4 ], ['ffff583a5 ' , 1 /100000 , 5 ]];
81+ yield from [['ffffef4 ' , 1 /1000000 , 3 ], ['ffffef39 ' , 1 /1000000 , 4 ], ['ffffef391 ' , 1 /1000000 , 5 ]];
3282 }
3383
3484 #[DataProvider('invalidProbabilityProvider ' )]
@@ -43,6 +93,7 @@ public static function invalidProbabilityProvider(): array
4393 return [
4494 'negative ' => [-0.05 ],
4595 'greater than one ' => [1.5 ],
96+ 'NaN ' => [NAN ],
4697 ];
4798 }
4899
0 commit comments