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

+2-2
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

+3
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

+1-1
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

+5-1
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

+7
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

+42
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

+7
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

+2
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

+6-1
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

+14
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

+5-1
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,

sdk/test/trace/tracer_test.cc

+23
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ TEST(Tracer, StartSpanWithDisabledConfig)
508508
std::make_shared<opentelemetry::trace::NoopTracer>();
509509
auto noop_span = noop_tracer->StartSpan("noop");
510510
EXPECT_TRUE(span.get() == noop_span.get());
511+
512+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
513+
EXPECT_FALSE(noop_tracer->Enabled());
514+
EXPECT_FALSE(tracer->Enabled());
515+
#endif
511516
}
512517

513518
TEST(Tracer, StartSpanWithEnabledConfig)
@@ -524,6 +529,11 @@ TEST(Tracer, StartSpanWithEnabledConfig)
524529
std::make_shared<opentelemetry::trace::NoopTracer>();
525530
auto noop_span = noop_tracer->StartSpan("noop");
526531
EXPECT_FALSE(span.get() == noop_span.get());
532+
533+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
534+
EXPECT_FALSE(noop_tracer->Enabled());
535+
EXPECT_TRUE(tracer->Enabled());
536+
#endif
527537
}
528538

529539
TEST(Tracer, StartSpanWithCustomConfig)
@@ -567,6 +577,14 @@ TEST(Tracer, StartSpanWithCustomConfig)
567577
new RandomIdGenerator(), custom_configurator, std::move(bar_scope));
568578
auto span_bar_scope = tracer_bar_scope->StartSpan("span 1");
569579
EXPECT_FALSE(span_bar_scope == noop_span);
580+
581+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
582+
EXPECT_FALSE(noop_tracer->Enabled());
583+
EXPECT_FALSE(tracer_default_scope->Enabled());
584+
EXPECT_FALSE(tracer_foo_scope->Enabled());
585+
EXPECT_TRUE(tracer_foo_scope_with_version->Enabled());
586+
EXPECT_TRUE(tracer_bar_scope->Enabled());
587+
#endif
570588
}
571589

572590
TEST(Tracer, StartSpanWithCustomConfigDifferingConditionOrder)
@@ -608,6 +626,11 @@ TEST(Tracer, StartSpanWithCustomConfigDifferingConditionOrder)
608626
// evaluating other condition
609627
const auto span_foo_scope_with_version_2 = tracer_foo_scope_with_version_2->StartSpan("span 1");
610628
EXPECT_TRUE(span_foo_scope_with_version_2 == noop_span);
629+
630+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
631+
EXPECT_TRUE(tracer_foo_scope_with_version_1->Enabled());
632+
EXPECT_FALSE(tracer_foo_scope_with_version_2->Enabled());
633+
#endif
611634
}
612635

613636
TEST(Tracer, SpanSetLinks)

0 commit comments

Comments
 (0)