Skip to content

Commit 2c1b1b5

Browse files
committed
test traceIdToUint64
Signed-off-by: therealak12 <[email protected]>
1 parent 115a6ef commit 2c1b1b5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/extensions/tracers/opentelemetry/samplers/trace_id_ratio_based/trace_id_ratio_based_sampler_test.cc

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ namespace OpenTelemetry {
2020

2121
const auto percentage_denominator = envoy::type::v3::FractionalPercent::MILLION;
2222

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+
2350
// As per the docs: https://opentelemetry.io/docs/specs/otel/trace/sdk/#traceidratiobased
2451
// > A TraceIDRatioBased sampler with a given sampling rate MUST also sample
2552
// all traces that any TraceIDRatioBased sampler with a lower sampling rate

0 commit comments

Comments
 (0)