2
2
#include < string>
3
3
4
4
#include " envoy/extensions/tracers/opentelemetry/samplers/v3/trace_id_ratio_based_sampler.pb.h"
5
+ #include " envoy/type/v3/percent.pb.h"
5
6
6
7
#include " source/common/common/random_generator.h"
7
8
#include " source/extensions/tracers/opentelemetry/samplers/trace_id_ratio_based/trace_id_ratio_based_sampler.h"
@@ -17,6 +18,8 @@ namespace Extensions {
17
18
namespace Tracers {
18
19
namespace OpenTelemetry {
19
20
21
+ const auto percentage_denominator = envoy::type::v3::FractionalPercent::MILLION;
22
+
20
23
// As per the docs: https://opentelemetry.io/docs/specs/otel/trace/sdk/#traceidratiobased
21
24
// > A TraceIDRatioBased sampler with a given sampling rate MUST also sample
22
25
// all traces that any TraceIDRatioBased sampler with a lower sampling rate
@@ -27,19 +30,24 @@ TEST(TraceIdRatioBasedSamplerTest, TestTraceIdRatioSamplesInclusively) {
27
30
28
31
std::srand (std::time (nullptr ));
29
32
for (int i = 0 ; i < 100 ; ++i) {
30
- double ratio_low = static_cast <double >(std::rand ()) / RAND_MAX;
31
- double ratio_high = static_cast <double >(std::rand ()) / RAND_MAX;
32
- if (ratio_low > ratio_high) {
33
- double holder = ratio_low;
34
- ratio_low = ratio_high;
35
- ratio_high = holder;
33
+ uint64_t numerator_low = std::rand () % ProtobufPercentHelper::fractionalPercentDenominatorToInt (
34
+ percentage_denominator);
35
+ uint64_t numerator_high =
36
+ std::rand () %
37
+ ProtobufPercentHelper::fractionalPercentDenominatorToInt (percentage_denominator);
38
+ if (numerator_low > numerator_high) {
39
+ double holder = numerator_low;
40
+ numerator_low = numerator_high;
41
+ numerator_high = holder;
36
42
}
37
43
envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig
38
44
config_low;
39
45
envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig
40
46
config_high;
41
- config_low.set_ratio (ratio_low);
42
- config_high.set_ratio (ratio_high);
47
+ config_low.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
48
+ config_low.mutable_sampling_percentage ()->set_numerator (numerator_low);
49
+ config_high.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
50
+ config_high.mutable_sampling_percentage ()->set_numerator (numerator_high);
43
51
auto sampler_low = std::make_shared<TraceIdRatioBasedSampler>(config_low, context);
44
52
auto sampler_high = std::make_shared<TraceIdRatioBasedSampler>(config_high, context);
45
53
@@ -72,7 +80,8 @@ TEST(TraceIdRatioBasedSamplerTest, TestSpecialRatios) {
72
80
std::srand (std::time (nullptr ));
73
81
74
82
// ratio = 0, should never sample
75
- config.set_ratio (0 );
83
+ config.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
84
+ config.mutable_sampling_percentage ()->set_numerator (0 );
76
85
auto sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
77
86
78
87
for (int i = 0 ; i < 10 ; ++i) {
@@ -86,7 +95,7 @@ TEST(TraceIdRatioBasedSamplerTest, TestSpecialRatios) {
86
95
}
87
96
88
97
// ratio < 0, should never sample
89
- config.set_ratio (-5 );
98
+ config.mutable_sampling_percentage ()-> set_numerator (-5 );
90
99
sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
91
100
92
101
for (int i = 0 ; i < 10 ; ++i) {
@@ -100,7 +109,8 @@ TEST(TraceIdRatioBasedSamplerTest, TestSpecialRatios) {
100
109
}
101
110
102
111
// ratio = 1, should always sample
103
- config.set_ratio (1 );
112
+ config.mutable_sampling_percentage ()->set_numerator (
113
+ ProtobufPercentHelper::fractionalPercentDenominatorToInt (percentage_denominator));
104
114
sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
105
115
106
116
for (int i = 0 ; i < 10 ; ++i) {
@@ -113,8 +123,9 @@ TEST(TraceIdRatioBasedSamplerTest, TestSpecialRatios) {
113
123
EXPECT_EQ (sampling_result.decision , Decision::RecordAndSample);
114
124
}
115
125
116
- // ratio < 0, should never sample
117
- config.set_ratio (7 );
126
+ // ratio > 1, should always sample
127
+ config.mutable_sampling_percentage ()->set_numerator (
128
+ 7 * ProtobufPercentHelper::fractionalPercentDenominatorToInt (percentage_denominator));
118
129
sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
119
130
120
131
for (int i = 0 ; i < 10 ; ++i) {
@@ -132,17 +143,21 @@ TEST(TraceIdRatioBasedSamplerTest, TestTraceIdRatioDescription) {
132
143
envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig config;
133
144
NiceMock<Server::Configuration::MockTracerFactoryContext> context;
134
145
NiceMock<StreamInfo::MockStreamInfo> info;
135
- config.set_ratio (0.0157 );
146
+ config.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
147
+ config.mutable_sampling_percentage ()->set_numerator (157 );
136
148
auto sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
137
- EXPECT_STREQ (sampler->getDescription ().c_str (), " TraceIdRatioBasedSampler{0.015700 }" );
149
+ EXPECT_STREQ (sampler->getDescription ().c_str (), " TraceIdRatioBasedSampler{157/1000000 }" );
138
150
}
139
151
140
152
TEST (TraceIdRatioBasedSamplerTest, TestTraceIdRatioAttrs) {
141
153
envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig config;
142
154
NiceMock<Server::Configuration::MockTracerFactoryContext> context;
143
155
NiceMock<StreamInfo::MockStreamInfo> info;
144
156
std::srand (std::time (nullptr ));
145
- config.set_ratio (static_cast <double >(std::rand ()) / RAND_MAX);
157
+ uint64_t numerator = std::rand () % ProtobufPercentHelper::fractionalPercentDenominatorToInt (
158
+ percentage_denominator);
159
+ config.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
160
+ config.mutable_sampling_percentage ()->set_numerator (numerator);
146
161
auto sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
147
162
SpanContext parent_context (" 0" , " 12345" , " 45678" , true , " random_key=random_value" );
148
163
auto sampling_result = sampler->shouldSample (
0 commit comments