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 @@ -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
2 changes: 1 addition & 1 deletion api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ point.
// Atomic wrappers based on compiler intrinsics for memory read/write.
// The tailing number is read/write length in bits.
//
// N.B. Compiler instrinsic is used because the usage of C++ standard library is restricted in the
// N.B. Compiler intrinsic is used because the usage of C++ standard library is restricted in the
// OpenTelemetry C++ API.
//
#if defined(__GNUC__)
Expand Down
6 changes: 5 additions & 1 deletion api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
Tracer(std::shared_ptr<DynamicLibraryHandle> library_handle,
std::unique_ptr<TracerHandle> &&tracer_handle) noexcept
: library_handle_{std::move(library_handle)}, tracer_handle_{std::move(tracer_handle)}
{}
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(true);
#endif
}

// trace::Tracer
nostd::shared_ptr<trace::Span> StartSpan(
Expand Down
7 changes: 7 additions & 0 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
{
public:
// Tracer
NoopTracer()
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(false);
#endif
}

nostd::shared_ptr<Span> StartSpan(nostd::string_view /*name*/,
const common::KeyValueIterable & /*attributes*/,
const SpanContextKeyValueIterable & /*links*/,
Expand Down
42 changes: 42 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
*/
bool Enabled() const noexcept { return OPENTELEMETRY_ATOMIC_READ_8(&this->enabled_) != 0; }
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1

/*
Expand Down Expand Up @@ -197,6 +209,36 @@ class Tracer
virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0;

#endif /* OPENTELEMETRY_ABI_VERSION_NO */

protected:
#if OPENTELEMETRY_ABI_VERSION_NO >= 2

/**
* Updates the enabled state of the tracer. Calling this method will affect the result of the
* subsequent calls to {@code opentelemetry::v2::trace::Tracer::Enabled()}.
*
* This method should be used by SDK implementations to indicate the tracer's updated state
* whenever a tracer transitions from enabled to disabled state and vice versa.
*
* @param enabled The new state of the tracer. False would indicate that the tracer is no longer
* enabled and will not produce as
*
* @since ABI_VERSION 2
*/
void UpdateEnabled(const bool enabled) noexcept
{
OPENTELEMETRY_ATOMIC_WRITE_8(&this->enabled_, enabled);
}
#endif

private:
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
// Variable to support implementation of Enabled method introduced in ABI V2.
// Mutable allows enabled_ to be used as 'bool *' (instead of 'const bool *'), with the
// OPENTELEMETRY_ATOMIC_READ_8 macro's internal casts when used from a const function.
// std::atomic can not be used here because it is not ABI compatible for OpenTelemetry C++ API.
mutable bool enabled_ = true;
#endif
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
7 changes: 7 additions & 0 deletions api/test/singleton/singleton_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ void reset_counts()
class MyTracer : public trace::Tracer
{
public:
MyTracer()
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(true);
#endif
}

nostd::shared_ptr<trace::Span> StartSpan(
nostd::string_view name,
const common::KeyValueIterable & /* attributes */,
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: 6 additions & 1 deletion examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ class Span final : public trace::Span
};
} // namespace

Tracer::Tracer(nostd::string_view /*output*/) {}
Tracer::Tracer(nostd::string_view /*output*/)
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(true);
#endif
}

nostd::shared_ptr<trace::Span> Tracer::StartSpan(nostd::string_view name,
const common::KeyValueIterable &attributes,
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
6 changes: 5 additions & 1 deletion sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ Tracer::Tracer(std::shared_ptr<TracerContext> context,
: instrumentation_scope_{std::move(instrumentation_scope)},
context_{std::move(context)},
tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_))
{}
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(tracer_config_.IsEnabled());
#endif
}

nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
nostd::string_view name,
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