Skip to content
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Increment the:
* [CMAKE] Bump cmake minimum required version to 3.14
[#3349](https://github.com/open-telemetry/opentelemetry-cpp/pull/3349)

* [API] Add Enabled method to Tracer
[#3357](https://github.com/open-telemetry/opentelemetry-cpp/pull/3357)

## [1.20 2025-04-01]

* [BUILD] Update opentelemetry-proto version
Expand Down
4 changes: 4 additions & 0 deletions api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
return nostd::shared_ptr<trace::Span>{new (std::nothrow) Span{this->shared_from_this(), span}};
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
bool Enabled() const noexcept override { return true; }
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override
Expand Down
4 changes: 4 additions & 0 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
return noop_span;
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
bool Enabled() const noexcept override { return false; }
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
Expand Down
12 changes: 12 additions & 0 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ class Tracer
}
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
/**
* Reports if the tracer is enabled or not. A disabled tracer will not create spans.
*
* The instrumentation authors should call this method before creating a spans to
* potentially avoid performing computationally expensive operations for disabled tracers.
*
* @since ABI_VERSION 2
*/
virtual bool Enabled() const noexcept = 0;
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

/*
Expand Down
6 changes: 6 additions & 0 deletions api/test/singleton/singleton_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ class MyTracer : public trace::Tracer
return result;
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2

bool Enabled() const noexcept override { return true; }

#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

void ForceFlushWithMicroseconds(uint64_t /* timeout */) noexcept override {}
Expand Down
2 changes: 2 additions & 0 deletions api/test/trace/noop_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ TEST(NoopTest, UseNoopTracersAbiv2)
s1->AddLink(target, {{"noop1", 1}});

s1->AddLinks({{trace_api::SpanContext(false, false), {{"noop2", 2}}}});

EXPECT_FALSE(tracer->Enabled());
}
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */

Expand Down
7 changes: 7 additions & 0 deletions examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,10 @@ nostd::shared_ptr<trace::Span> Tracer::StartSpan(nostd::string_view name,
return nostd::shared_ptr<trace::Span>{
new (std::nothrow) Span{this->shared_from_this(), name, attributes, links, options}};
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
bool Tracer::Enabled() const noexcept
{
return true;
}
#endif
6 changes: 6 additions & 0 deletions examples/plugin/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Tracer final : public opentelemetry::trace::Tracer,
const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/,
const opentelemetry::trace::StartSpanOptions & /*options */) noexcept override;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2

bool Enabled() const noexcept override;

#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
Expand Down
14 changes: 14 additions & 0 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,20 @@ class Tracer : public opentelemetry::trace::Tracer,
return result;
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
/**
* Reports if the tracer is enabled or not. A disabled tracer will not create spans.
* Note: The etw_tracer currently does not accept a TracerConfig and can therefore not be disabled
* based on the instrumentation scope.
*
* The instrumentation authors should call this method before creating a spans to
* potentially avoid performing computationally expensive operations for disabled tracers.
*
* @since ABI_VERSION 2
*/
virtual bool Enabled() const noexcept { return true; }
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1
/**
* @brief Force flush data to Tracer, spending up to given amount of microseconds to flush.
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
}

void CloseWithMicroseconds(uint64_t timeout) noexcept;

bool Enabled() const noexcept override;
#else
/* Exposed in the API in ABI version 1, but does not belong to the API */
void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override;
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
}
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
bool Tracer::Enabled() const noexcept
{
return tracer_config_.IsEnabled();
}
#endif

void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
{
if (context_)
Expand Down
23 changes: 23 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ TEST(Tracer, StartSpanWithDisabledConfig)
std::make_shared<opentelemetry::trace::NoopTracer>();
auto noop_span = noop_tracer->StartSpan("noop");
EXPECT_TRUE(span.get() == noop_span.get());

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
EXPECT_FALSE(noop_tracer->Enabled());
EXPECT_FALSE(tracer->Enabled());
#endif
}

TEST(Tracer, StartSpanWithEnabledConfig)
Expand All @@ -524,6 +529,11 @@ TEST(Tracer, StartSpanWithEnabledConfig)
std::make_shared<opentelemetry::trace::NoopTracer>();
auto noop_span = noop_tracer->StartSpan("noop");
EXPECT_FALSE(span.get() == noop_span.get());

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
EXPECT_FALSE(noop_tracer->Enabled());
EXPECT_TRUE(tracer->Enabled());
#endif
}

TEST(Tracer, StartSpanWithCustomConfig)
Expand Down Expand Up @@ -567,6 +577,14 @@ TEST(Tracer, StartSpanWithCustomConfig)
new RandomIdGenerator(), custom_configurator, std::move(bar_scope));
auto span_bar_scope = tracer_bar_scope->StartSpan("span 1");
EXPECT_FALSE(span_bar_scope == noop_span);

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
EXPECT_FALSE(noop_tracer->Enabled());
EXPECT_FALSE(tracer_default_scope->Enabled());
EXPECT_FALSE(tracer_foo_scope->Enabled());
EXPECT_TRUE(tracer_foo_scope_with_version->Enabled());
EXPECT_TRUE(tracer_bar_scope->Enabled());
#endif
}

TEST(Tracer, StartSpanWithCustomConfigDifferingConditionOrder)
Expand Down Expand Up @@ -608,6 +626,11 @@ TEST(Tracer, StartSpanWithCustomConfigDifferingConditionOrder)
// evaluating other condition
const auto span_foo_scope_with_version_2 = tracer_foo_scope_with_version_2->StartSpan("span 1");
EXPECT_TRUE(span_foo_scope_with_version_2 == noop_span);

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
EXPECT_TRUE(tracer_foo_scope_with_version_1->Enabled());
EXPECT_FALSE(tracer_foo_scope_with_version_2->Enabled());
#endif
}

TEST(Tracer, SpanSetLinks)
Expand Down
Loading