Skip to content

Commit d6301b7

Browse files
authored
[SDK] Reset TraceFlags::IsSampled bit on sampler Decision::DROP (open-telemetry#3745)
1 parent 907277c commit d6301b7

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Increment the:
4545
* [CI] Free disk space
4646
[#3749](https://github.com/open-telemetry/opentelemetry-cpp/pull/3749)
4747

48+
* [SDK] Reset TraceFlags::IsSampled bit on sampler Decision::DROP
49+
[#3745](https://github.com/open-telemetry/opentelemetry-cpp/pull/3745)
50+
4851
New Features:
4952

5053
* [CONFIGURATION] Implement declarative configuration (config.yaml)

api/include/opentelemetry/trace/trace_flags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TraceFlags final
2929

3030
/**
3131
* Valid flags in W3C Trace Context version 2.
32-
* See https://www.w3.org/TR/trace-context-1/#trace-flags
32+
* See https://www.w3.org/TR/trace-context-2/#trace-flags
3333
*/
3434
static constexpr uint8_t kAllW3CTraceContext2Flags = kIsSampled | kIsRandom;
3535

sdk/src/trace/tracer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
116116
{
117117
flags |= opentelemetry::trace::TraceFlags::kIsSampled;
118118
}
119+
else
120+
{
121+
flags &= ~opentelemetry::trace::TraceFlags::kIsSampled;
122+
}
119123

120124
#if 1
121125
/* https://github.com/open-telemetry/opentelemetry-specification as of v1.29.0 */

sdk/test/trace/tracer_test.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)