Skip to content

Commit 84993e1

Browse files
authored
Merge branch 'main' into fixes_warnings_when_include_ciso646
2 parents 0aef16b + d976876 commit 84993e1

File tree

11 files changed

+115
-4
lines changed

11 files changed

+115
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Increment the:
2121
* [CMAKE] Bump cmake minimum required version to 3.14
2222
[#3349](https://github.com/open-telemetry/opentelemetry-cpp/pull/3349)
2323

24+
* [API] Add Enabled method to Tracer
25+
[#3357](https://github.com/open-telemetry/opentelemetry-cpp/pull/3357)
26+
2427
## [1.20 2025-04-01]
2528

2629
* [BUILD] Update opentelemetry-proto version

api/include/opentelemetry/common/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ point.
341341
// Atomic wrappers based on compiler intrinsics for memory read/write.
342342
// The tailing number is read/write length in bits.
343343
//
344-
// N.B. Compiler instrinsic is used because the usage of C++ standard library is restricted in the
344+
// N.B. Compiler intrinsic is used because the usage of C++ standard library is restricted in the
345345
// OpenTelemetry C++ API.
346346
//
347347
#if defined(__GNUC__)

api/include/opentelemetry/plugin/tracer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
8787
Tracer(std::shared_ptr<DynamicLibraryHandle> library_handle,
8888
std::unique_ptr<TracerHandle> &&tracer_handle) noexcept
8989
: library_handle_{std::move(library_handle)}, tracer_handle_{std::move(tracer_handle)}
90-
{}
90+
{
91+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
92+
UpdateEnabled(true);
93+
#endif
94+
}
9195

9296
// trace::Tracer
9397
nostd::shared_ptr<trace::Span> StartSpan(

api/include/opentelemetry/trace/noop.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
9696
{
9797
public:
9898
// Tracer
99+
NoopTracer()
100+
{
101+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
102+
UpdateEnabled(false);
103+
#endif
104+
}
105+
99106
nostd::shared_ptr<Span> StartSpan(nostd::string_view /*name*/,
100107
const common::KeyValueIterable & /*attributes*/,
101108
const SpanContextKeyValueIterable & /*links*/,

api/include/opentelemetry/trace/tracer.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
202244
OPENTELEMETRY_END_NAMESPACE

api/test/singleton/singleton_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ void reset_counts()
169169
class MyTracer : public trace::Tracer
170170
{
171171
public:
172+
MyTracer()
173+
{
174+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
175+
UpdateEnabled(true);
176+
#endif
177+
}
178+
172179
nostd::shared_ptr<trace::Span> StartSpan(
173180
nostd::string_view name,
174181
const common::KeyValueIterable & /* attributes */,

api/test/trace/noop_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ TEST(NoopTest, UseNoopTracersAbiv2)
7070
s1->AddLink(target, {{"noop1", 1}});
7171

7272
s1->AddLinks({{trace_api::SpanContext(false, false), {{"noop2", 2}}}});
73+
74+
EXPECT_FALSE(tracer->Enabled());
7375
}
7476
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */
7577

examples/plugin/plugin/tracer.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ class Span final : public trace::Span
8181
};
8282
} // namespace
8383

84-
Tracer::Tracer(nostd::string_view /*output*/) {}
84+
Tracer::Tracer(nostd::string_view /*output*/)
85+
{
86+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
87+
UpdateEnabled(true);
88+
#endif
89+
}
8590

8691
nostd::shared_ptr<trace::Span> Tracer::StartSpan(nostd::string_view name,
8792
const common::KeyValueIterable &attributes,

exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ class Tracer : public opentelemetry::trace::Tracer,
590590
return result;
591591
}
592592

593+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
594+
/**
595+
* Reports if the tracer is enabled or not. A disabled tracer will not create spans.
596+
* Note: The etw_tracer currently does not accept a TracerConfig and can therefore not be disabled
597+
* based on the instrumentation scope.
598+
*
599+
* The instrumentation authors should call this method before creating a spans to
600+
* potentially avoid performing computationally expensive operations for disabled tracers.
601+
*
602+
* @since ABI_VERSION 2
603+
*/
604+
virtual bool Enabled() const noexcept { return true; }
605+
#endif
606+
593607
#if OPENTELEMETRY_ABI_VERSION_NO == 1
594608
/**
595609
* @brief Force flush data to Tracer, spending up to given amount of microseconds to flush.

sdk/src/trace/tracer.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ Tracer::Tracer(std::shared_ptr<TracerContext> context,
4646
: instrumentation_scope_{std::move(instrumentation_scope)},
4747
context_{std::move(context)},
4848
tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_))
49-
{}
49+
{
50+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
51+
UpdateEnabled(tracer_config_.IsEnabled());
52+
#endif
53+
}
5054

5155
nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
5256
nostd::string_view name,

0 commit comments

Comments
 (0)