Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Increment the:
* [CI] Free disk space
[#3749](https://github.com/open-telemetry/opentelemetry-cpp/pull/3749)

* [SDK] Reset TraceFlags::IsSampled bit on sampler Decision::DROP
[#3745](https://github.com/open-telemetry/opentelemetry-cpp/pull/3745)

New Features:

* [CONFIGURATION] Implement declarative configuration (config.yaml)
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/trace_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TraceFlags final

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

Expand Down
4 changes: 4 additions & 0 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
{
flags |= opentelemetry::trace::TraceFlags::kIsSampled;
}
else
{
flags &= ~opentelemetry::trace::TraceFlags::kIsSampled;
}

#if 1
/* https://github.com/open-telemetry/opentelemetry-specification as of v1.29.0 */
Expand Down
71 changes: 71 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,48 @@ class MockSampler final : public Sampler
nostd::string_view GetDescription() const noexcept override { return "MockSampler"; }
};

/**
* A mock sampler with ShouldSample returning
* a decision based on the span name.
*/
class MockDecisionSampler final : public Sampler
{
public:
SamplingResult ShouldSample(
const SpanContext & /*parent_context*/,
trace_api::TraceId /*trace_id*/,
nostd::string_view name,
trace_api::SpanKind /*span_kind*/,
const opentelemetry::common::KeyValueIterable & /*attributes*/,
const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/) noexcept override
{
std::string span_name{name};

if (span_name.find("DROP-") != std::string::npos)
{
return {Decision::DROP,
nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
nullptr),
nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr)};
}

if (span_name.find("RECORD_ONLY-") != std::string::npos)
{
return {Decision::RECORD_ONLY,
nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
nullptr),
nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr)};
}

return {Decision::RECORD_AND_SAMPLE,
nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
nullptr),
nostd::shared_ptr<opentelemetry::trace::TraceState>(nullptr)};
}

nostd::string_view GetDescription() const noexcept override { return "MockDecisionSampler"; }
};

/**
* A Mock Custom ID Generator
*/
Expand Down Expand Up @@ -1331,3 +1373,32 @@ TEST(Tracer, SpanCleanupWithScope)
}
EXPECT_EQ(4, span_data->GetSpans().size());
}

TEST(Tracer, SpanSamplerDecision)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - prefer make_unique/make_shared in place of new.

std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new MockDecisionSampler());
{
auto span0 = tracer->StartSpan("Span0");
auto span1 = tracer->StartSpan("span1");
auto context1 = span1->GetContext();
EXPECT_TRUE(context1.IsValid());
EXPECT_TRUE(context1.IsSampled());
{
trace_api::Scope scope1(span1);
auto span2 = tracer->StartSpan("RECORD_ONLY-span2");
auto context2 = span2->GetContext();
EXPECT_TRUE(context2.IsValid());
EXPECT_FALSE(context2.IsSampled());
{
trace_api::Scope scope2(span2);
auto span3 = tracer->StartSpan("DROP-span3");
auto context3 = span3->GetContext();
EXPECT_TRUE(context3.IsValid());
EXPECT_FALSE(context3.IsSampled());
}
}
}
EXPECT_EQ(3, span_data->GetSpans().size());
}
Loading