@@ -63,6 +63,69 @@ TEST(TraceIdRatioBasedSamplerTest, TestTraceIdRatioSamplesInclusively) {
63
63
}
64
64
}
65
65
66
+ // Test special ratios including 0, 1, and numbers out of [0, 1]
67
+ TEST (TraceIdRatioBasedSamplerTest, TestSpecialRatios) {
68
+ NiceMock<Server::Configuration::MockTracerFactoryContext> context;
69
+ envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig config;
70
+ std::srand (std::time (nullptr ));
71
+
72
+ // ratio = 0, should never sample
73
+ config.set_ratio (0 );
74
+ auto sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
75
+
76
+ for (int i = 0 ; i < 10 ; ++i) {
77
+ Random::RandomGeneratorImpl random_generator;
78
+ auto trace_id = absl::StrCat (Hex::uint64ToHex (random_generator.random ()),
79
+ Hex::uint64ToHex (random_generator.random ()));
80
+ auto sampling_result =
81
+ sampler->shouldSample (absl::nullopt, trace_id, " operation_name" ,
82
+ ::opentelemetry::proto::trace::v1::Span::SPAN_KIND_SERVER, {}, {});
83
+ EXPECT_EQ (sampling_result.decision , Decision::Drop);
84
+ }
85
+
86
+ // ratio < 0, should never sample
87
+ config.set_ratio (-5 );
88
+ sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
89
+
90
+ for (int i = 0 ; i < 10 ; ++i) {
91
+ Random::RandomGeneratorImpl random_generator;
92
+ auto trace_id = absl::StrCat (Hex::uint64ToHex (random_generator.random ()),
93
+ Hex::uint64ToHex (random_generator.random ()));
94
+ auto sampling_result =
95
+ sampler->shouldSample (absl::nullopt, trace_id, " operation_name" ,
96
+ ::opentelemetry::proto::trace::v1::Span::SPAN_KIND_SERVER, {}, {});
97
+ EXPECT_EQ (sampling_result.decision , Decision::Drop);
98
+ }
99
+
100
+ // ratio = 1, should always sample
101
+ config.set_ratio (1 );
102
+ sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
103
+
104
+ for (int i = 0 ; i < 10 ; ++i) {
105
+ Random::RandomGeneratorImpl random_generator;
106
+ auto trace_id = absl::StrCat (Hex::uint64ToHex (random_generator.random ()),
107
+ Hex::uint64ToHex (random_generator.random ()));
108
+ auto sampling_result =
109
+ sampler->shouldSample (absl::nullopt, trace_id, " operation_name" ,
110
+ ::opentelemetry::proto::trace::v1::Span::SPAN_KIND_SERVER, {}, {});
111
+ EXPECT_EQ (sampling_result.decision , Decision::RecordAndSample);
112
+ }
113
+
114
+ // ratio < 0, should never sample
115
+ config.set_ratio (7 );
116
+ sampler = std::make_shared<TraceIdRatioBasedSampler>(config, context);
117
+
118
+ for (int i = 0 ; i < 10 ; ++i) {
119
+ Random::RandomGeneratorImpl random_generator;
120
+ auto trace_id = absl::StrCat (Hex::uint64ToHex (random_generator.random ()),
121
+ Hex::uint64ToHex (random_generator.random ()));
122
+ auto sampling_result =
123
+ sampler->shouldSample (absl::nullopt, trace_id, " operation_name" ,
124
+ ::opentelemetry::proto::trace::v1::Span::SPAN_KIND_SERVER, {}, {});
125
+ EXPECT_EQ (sampling_result.decision , Decision::RecordAndSample);
126
+ }
127
+ }
128
+
66
129
TEST (TraceIdRatioBasedSamplerTest, TestTraceIdRatioDescription) {
67
130
envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig config;
68
131
NiceMock<Server::Configuration::MockTracerFactoryContext> context;
0 commit comments