Skip to content

Commit f917c08

Browse files
authored
Merge branch 'main' into expo-histo-aggregation-4
2 parents 9adcfac + d976876 commit f917c08

File tree

12 files changed

+117
-6
lines changed

12 files changed

+117
-6
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
mv api-benchmark_result.json benchmarks
3636
mv sdk-benchmark_result.json benchmarks
3737
mv exporters-benchmark_result.json benchmarks
38-
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
38+
- uses: actions/upload-artifact@6027e3dd177782cd8ab9af838c04fd81a07f1d47 # main March 2025
3939
with:
4040
name: benchmark_results
4141
path: benchmarks
@@ -48,7 +48,7 @@ jobs:
4848
runs-on: ubuntu-latest
4949
steps:
5050
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
51-
- uses: actions/download-artifact@2a5974104b6d5dbdb2f9468a3e54da3bdd241578 # v4.2.1
51+
- uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # main March 2025
5252
with:
5353
name: benchmark_results
5454
path: benchmarks

CHANGELOG.md

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

33+
* [API] Add Enabled method to Tracer
34+
[#3357](https://github.com/open-telemetry/opentelemetry-cpp/pull/3357)
35+
3336
## [1.20 2025-04-01]
3437

3538
* [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.

0 commit comments

Comments
 (0)