Skip to content
Merged
10 changes: 5 additions & 5 deletions 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 All @@ -104,10 +108,6 @@ 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
11 changes: 7 additions & 4 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 All @@ -108,10 +115,6 @@ 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
26 changes: 25 additions & 1 deletion api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Tracer
*
* @since ABI_VERSION 2
*/
virtual bool Enabled() const noexcept = 0;
bool Enabled() const noexcept { return this->enabled_; }
#endif

#if OPENTELEMETRY_ABI_VERSION_NO == 1
Expand Down Expand Up @@ -209,6 +209,30 @@ 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 { this->enabled_ = enabled; }
#endif

private:
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
// variable to support implementation of Enabled method introduced in ABI V2.
bool enabled_ = true;
#endif
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
13 changes: 7 additions & 6 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 Expand Up @@ -285,12 +292,6 @@ 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
14 changes: 6 additions & 8 deletions 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 All @@ -91,10 +96,3 @@ 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: 0 additions & 6 deletions examples/plugin/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ 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
2 changes: 0 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ 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
13 changes: 5 additions & 8 deletions 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 Expand Up @@ -163,13 +167,6 @@ 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
Loading