Skip to content

Commit fb9c76e

Browse files
committed
add api method to check if tracer is enabled
1 parent 2c55339 commit fb9c76e

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

api/include/opentelemetry/trace/noop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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 {}

api/include/opentelemetry/trace/tracer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
/*

api/test/trace/tracer_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

sdk/include/opentelemetry/sdk/trace/tracer.h

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

sdk/src/trace/tracer.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
167183
void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
168184
{
169185
if (context_)

0 commit comments

Comments
 (0)