@@ -163,6 +163,18 @@ class Tracer
163163 }
164164 }
165165
166+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
167+ /* *
168+ * Reports if the tracer is enabled or not. A disabled tracer will not create spans.
169+ *
170+ * The instrumentation authors should call this method before creating a spans to
171+ * potentially avoid performing computationally expensive operations for disabled tracers.
172+ *
173+ * @since ABI_VERSION 2
174+ */
175+ bool Enabled () const noexcept { return OPENTELEMETRY_ATOMIC_READ_8 (&this ->enabled_ ) != 0 ; }
176+ #endif
177+
166178#if OPENTELEMETRY_ABI_VERSION_NO == 1
167179
168180 /*
@@ -197,6 +209,36 @@ class Tracer
197209 virtual void CloseWithMicroseconds (uint64_t timeout) noexcept = 0;
198210
199211#endif /* OPENTELEMETRY_ABI_VERSION_NO */
212+
213+ protected:
214+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
215+
216+ /* *
217+ * Updates the enabled state of the tracer. Calling this method will affect the result of the
218+ * subsequent calls to {@code opentelemetry::v2::trace::Tracer::Enabled()}.
219+ *
220+ * This method should be used by SDK implementations to indicate the tracer's updated state
221+ * whenever a tracer transitions from enabled to disabled state and vice versa.
222+ *
223+ * @param enabled The new state of the tracer. False would indicate that the tracer is no longer
224+ * enabled and will not produce as
225+ *
226+ * @since ABI_VERSION 2
227+ */
228+ void UpdateEnabled (const bool enabled) noexcept
229+ {
230+ OPENTELEMETRY_ATOMIC_WRITE_8 (&this ->enabled_ , enabled);
231+ }
232+ #endif
233+
234+ private:
235+ #if OPENTELEMETRY_ABI_VERSION_NO >= 2
236+ // Variable to support implementation of Enabled method introduced in ABI V2.
237+ // Mutable allows enabled_ to be used as 'bool *' (instead of 'const bool *'), with the
238+ // OPENTELEMETRY_ATOMIC_READ_8 macro's internal casts when used from a const function.
239+ // std::atomic can not be used here because it is not ABI compatible for OpenTelemetry C++ API.
240+ mutable bool enabled_ = true ;
241+ #endif
200242};
201243} // namespace trace
202244OPENTELEMETRY_END_NAMESPACE
0 commit comments