@@ -20,6 +20,33 @@ namespace OpenTelemetry {
20
20
21
21
const auto percentage_denominator = envoy::type::v3::FractionalPercent::MILLION;
22
22
23
+ TEST (TraceIdRatioBasedSamplerTest, TestTraceIdToUint64) {
24
+ NiceMock<Server::Configuration::MockTracerFactoryContext> context;
25
+ NiceMock<StreamInfo::MockStreamInfo> info;
26
+ envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig config;
27
+ config.mutable_sampling_percentage ()->set_denominator (percentage_denominator);
28
+ uint64_t numerator = std::rand () % ProtobufPercentHelper::fractionalPercentDenominatorToInt (
29
+ percentage_denominator);
30
+ config.mutable_sampling_percentage ()->set_numerator (numerator);
31
+ auto sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
32
+
33
+ // Test with an empty string.
34
+ std::string empty_trace_id = " " ;
35
+ EXPECT_EQ (sampler->traceIdToUint64 (empty_trace_id), 0 );
36
+
37
+ // Test with a string smaller than 16 characters.
38
+ std::string short_trace_id = " 5b8aa5a" ;
39
+ EXPECT_EQ (sampler->traceIdToUint64 (short_trace_id), 0 );
40
+
41
+ // Test with a string of exactly 16 characters.
42
+ std::string trace_id_16 = " 5b8aa5a2d2c872e8" ;
43
+ EXPECT_EQ (sampler->traceIdToUint64 (trace_id_16), 1312 );
44
+
45
+ // Test with a string larger than 16 characters.
46
+ std::string long_trace_id = " 5b8aa5a2d2c872e8321cf37308d69df2" ;
47
+ EXPECT_EQ (sampler->traceIdToUint64 (long_trace_id), 1312 );
48
+ }
49
+
23
50
// As per the docs: https://opentelemetry.io/docs/specs/otel/trace/sdk/#traceidratiobased
24
51
// > A TraceIDRatioBased sampler with a given sampling rate MUST also sample
25
52
// all traces that any TraceIDRatioBased sampler with a lower sampling rate
0 commit comments