@@ -107,6 +107,48 @@ class MockSampler final : public Sampler
107107 nostd::string_view GetDescription () const noexcept override { return " MockSampler" ; }
108108};
109109
110+ /* *
111+ * A mock sampler with ShouldSample returning
112+ * a decision based on the span name.
113+ */
114+ class MockDecisionSampler final : public Sampler
115+ {
116+ public:
117+ SamplingResult ShouldSample (
118+ const SpanContext & /* parent_context*/ ,
119+ trace_api::TraceId /* trace_id*/ ,
120+ nostd::string_view name,
121+ trace_api::SpanKind /* span_kind*/ ,
122+ const opentelemetry::common::KeyValueIterable & /* attributes*/ ,
123+ const opentelemetry::trace::SpanContextKeyValueIterable & /* links*/ ) noexcept override
124+ {
125+ std::string span_name{name};
126+
127+ if (span_name.find (" DROP-" ) != std::string::npos)
128+ {
129+ return {Decision::DROP,
130+ nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
131+ nullptr ),
132+ nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr )};
133+ }
134+
135+ if (span_name.find (" RECORD_ONLY-" ) != std::string::npos)
136+ {
137+ return {Decision::RECORD_ONLY,
138+ nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
139+ nullptr ),
140+ nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr )};
141+ }
142+
143+ return {Decision::RECORD_AND_SAMPLE,
144+ nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
145+ nullptr ),
146+ nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr )};
147+ }
148+
149+ nostd::string_view GetDescription () const noexcept override { return " MockDecisionSampler" ; }
150+ };
151+
110152/* *
111153 * A Mock Custom ID Generator
112154 */
@@ -1331,3 +1373,32 @@ TEST(Tracer, SpanCleanupWithScope)
13311373 }
13321374 EXPECT_EQ (4 , span_data->GetSpans ().size ());
13331375}
1376+
1377+ TEST (Tracer, SpanSamplerDecision)
1378+ {
1379+ InMemorySpanExporter *exporter = new InMemorySpanExporter ();
1380+ std::shared_ptr<InMemorySpanData> span_data = exporter->GetData ();
1381+ auto tracer = initTracer (std::unique_ptr<SpanExporter>{exporter}, new MockDecisionSampler ());
1382+ {
1383+ auto span0 = tracer->StartSpan (" Span0" );
1384+ auto span1 = tracer->StartSpan (" span1" );
1385+ auto context1 = span1->GetContext ();
1386+ EXPECT_TRUE (context1.IsValid ());
1387+ EXPECT_TRUE (context1.IsSampled ());
1388+ {
1389+ trace_api::Scope scope1 (span1);
1390+ auto span2 = tracer->StartSpan (" RECORD_ONLY-span2" );
1391+ auto context2 = span2->GetContext ();
1392+ EXPECT_TRUE (context2.IsValid ());
1393+ EXPECT_FALSE (context2.IsSampled ());
1394+ {
1395+ trace_api::Scope scope2 (span2);
1396+ auto span3 = tracer->StartSpan (" DROP-span3" );
1397+ auto context3 = span3->GetContext ();
1398+ EXPECT_TRUE (context3.IsValid ());
1399+ EXPECT_FALSE (context3.IsSampled ());
1400+ }
1401+ }
1402+ }
1403+ EXPECT_EQ (3 , span_data->GetSpans ().size ());
1404+ }
0 commit comments