File tree Expand file tree Collapse file tree 5 files changed +45
-1
lines changed
include/opentelemetry/trace
include/opentelemetry/sdk/trace Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,10 @@ class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
108108 return noop_span;
109109 }
110110
111+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
112+ bool IsEnabled () const noexcept override { return false ; }
113+ #endif
114+
111115#if OPENTELEMETRY_ABI_VERSION_NO == 1
112116
113117 void ForceFlushWithMicroseconds (uint64_t /* timeout*/ ) noexcept override {}
Original file line number Diff line number Diff line change @@ -163,6 +163,15 @@ class Tracer
163163 }
164164 }
165165
166+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
167+ /* *
168+ * Returns a boolean indicating if the tracer is enabled or not. Disabled tracers behave like
169+ * no-op tracers. By default, tracers are considered enabled.
170+ * @return true if the tracer is enabled, false otherwise.
171+ */
172+ virtual bool IsEnabled () const noexcept = 0;
173+ #endif
174+
166175#if OPENTELEMETRY_ABI_VERSION_NO == 1
167176
168177 /*
Original file line number Diff line number Diff line change @@ -40,3 +40,13 @@ TEST(TracerTest, GetCurrentSpan)
4040 current = tracer->GetCurrentSpan ();
4141 ASSERT_FALSE (current->GetContext ().IsValid ());
4242}
43+
44+ TEST (TracerTest, IsEnabled)
45+ {
46+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
47+ std::unique_ptr<trace_api::Tracer> tracer (new trace_api::NoopTracer ());
48+ ASSERT_FALSE (tracer->IsEnabled ());
49+ #else
50+ GTEST_SKIP () << " IsEnabled() not exposed in ABI v1" ;
51+ #endif
52+ }
Original file line number Diff line number Diff line change @@ -71,6 +71,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
7171 }
7272
7373 void CloseWithMicroseconds (uint64_t timeout) noexcept ;
74+
75+ bool IsEnabled () const noexcept override ;
7476#else
7577 /* Exposed in the API in ABI version 1, but does not belong to the API */
7678 void ForceFlushWithMicroseconds (uint64_t timeout) noexcept override ;
@@ -109,6 +111,9 @@ class Tracer final : public opentelemetry::trace::Tracer,
109111 std::shared_ptr<InstrumentationScope> instrumentation_scope_;
110112 std::shared_ptr<TracerContext> context_;
111113 static const std::shared_ptr<opentelemetry::trace::NoopTracer> kNoopTracer ;
114+
115+ // Provides IsEnabled implementation for Tracers, appropriate for current ABI version
116+ bool ABIAppropriateIsEnabled () const ;
112117};
113118} // namespace trace
114119} // namespace sdk
Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
5858 opentelemetry::trace::SpanContext parent_context = GetCurrentSpan ()->GetContext ();
5959 if (nostd::holds_alternative<opentelemetry::trace::SpanContext>(options.parent ))
6060 {
61- if (!tracer_config_-> IsEnabled ())
61+ if (!ABIAppropriateIsEnabled ())
6262 {
6363 return kNoopTracer ->StartSpan (name, attributes, links, options);
6464 }
@@ -164,6 +164,22 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
164164 }
165165}
166166
167+ bool Tracer::ABIAppropriateIsEnabled () const
168+ {
169+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
170+ return Tracer::IsEnabled ();
171+ #else
172+ return tracer_config_->IsEnabled ();
173+ #endif
174+ }
175+
176+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
177+ bool Tracer::IsEnabled () const noexcept
178+ {
179+ return tracer_config_->IsEnabled ();
180+ }
181+ #endif
182+
167183void Tracer::ForceFlushWithMicroseconds (uint64_t timeout) noexcept
168184{
169185 if (context_)
You can’t perform that action at this time.
0 commit comments