Skip to content

[SDK] Handle instrumentation object construction failures safely - #4469

Draft
22elix3r wants to merge 1 commit into
open-telemetry:mainfrom
22elix3r:fix/noexcept-provider-construction-4361
Draft

[SDK] Handle instrumentation object construction failures safely#4469
22elix3r wants to merge 1 commit into
open-telemetry:mainfrom
22elix3r:fix/noexcept-provider-construction-4361

Conversation

@22elix3r

Copy link
Copy Markdown

Fixes #4361

Changes

Provider construction happens during SDK initialization. Runtime GetTracer / GetLogger / GetMeter calls must not throw. This change follows the post-SIG direction from @dbarker:

  • Remove noexcept from the SDK constructors for TracerProvider, LoggerProvider, MeterProvider, Tracer, Logger, and Meter. Allocation or initialization failures during provider construction can now propagate so the caller (often the configuration library) can handle them.
  • Keep GetTracer, GetLogger, and GetMeter noexcept. If constructing a previously unseen tracer/logger/meter fails, the provider catches the exception (when exceptions are enabled), logs via OTEL_INTERNAL_LOG_ERROR without letting logging escape, and returns a valid noop API object.
  • The noop fallback is allocated during provider construction (new NoopTracer / NoopLogger / NoopMeter stored as a nostd::shared_ptr). Runtime failure handling does not allocate a fresh noop object.
  • Failed construction is not inserted into the provider cache. A later Get* call can retry and return a normal SDK object once the failure is gone. Existing cached objects continue to be returned unchanged.
  • The complete fallible path is protected: instrumentation-scope construction, configurator evaluation, object allocation, shared_ptr control-block allocation, cache insertion, and metrics AddMeter.
  • Public API virtual method exception contracts are unchanged. ABI v1 and ABI v2 Get* signatures are unchanged. Try/catch is compiled only when OPENTELEMETRY_HAVE_EXCEPTIONS is set.

Tests added

  • Constructor noexcept static_asserts for each signal.
  • Deterministic fault injection through a ScopeConfigurator matcher that throws for a named scope.
  • After failure, Get* returns a valid noop-compatible object, using it produces no telemetry, the cache is not poisoned, cached objects are unchanged, and a later call recovers when the injected failure is removed.
  • Those tests are skipped when exceptions are disabled.

Validation

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug \
  -DOTELCPP_WITH_EXAMPLES=OFF -DOTELCPP_BUILD_TESTING=ON \
  -DOTELCPP_WITH_OTLP_GRPC=OFF -DOTELCPP_WITH_OTLP_HTTP=OFF \
  -DOTELCPP_WITH_OTLP_FILE=OFF -DCMAKE_CXX_STANDARD=17

cmake --build build --target tracer_provider_test logger_provider_sdk_test meter_provider_sdk_test
ctest --test-dir build --output-on-failure \
  -R 'trace.(TracerProvider|TracerConfig)|logs.(LoggerProviderSDK|LoggerConfig)|metrics.(MeterProvider|MeterConfig)'
# 63/63 passed, including Get*ReturnsNoopOnConstructionFailure

ctest --test-dir build --output-on-failure -R 'logs.LoggerSDK'
# 12/12 passed

./build/sdk/test/metrics/meter_test
# 20/20 passed

# Exception-disabled compile of changed SDK sources:
g++ -std=c++17 -fno-exceptions -c -DOPENTELEMETRY_HAVE_EXCEPTIONS=0 ...
# tracer_provider.cc, tracer.cc, logger_provider.cc, logger.cc,
# meter_provider.cc, meter.cc: success

# ABI v2 compile of the three provider .cc files:
g++ -std=c++17 -c -DOPENTELEMETRY_ABI_VERSION_NO=2 ...
# tracer_provider.cc, logger_provider.cc, meter_provider.cc: success

git diff --check
# clean

tools/format.sh was not run: clang-format is not installed in this environment.

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed (SDK constructor exception specs only; API Get* contracts unchanged)

Remove noexcept from SDK TracerProvider, LoggerProvider, MeterProvider,
Tracer, Logger, and Meter constructors so initialization-time allocation
failures can propagate. Keep GetTracer/GetLogger/GetMeter noexcept and
return a pre-allocated noop object if constructing a new instrumentation
object fails, without caching the failed attempt.

Fixes open-telemetry#4361

Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.13636% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.59%. Comparing base (68a575f) to head (1271d6f).

Files with missing lines Patch % Lines
sdk/src/logs/logger_provider.cc 70.00% 9 Missing ⚠️
sdk/src/metrics/meter_provider.cc 76.93% 6 Missing ⚠️
sdk/src/trace/tracer_provider.cc 79.32% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4469      +/-   ##
==========================================
- Coverage   82.62%   82.59%   -0.03%     
==========================================
  Files         512      512              
  Lines       20139    20194      +55     
==========================================
+ Hits        16638    16677      +39     
- Misses       3501     3517      +16     
Files with missing lines Coverage Δ
sdk/include/opentelemetry/sdk/metrics/meter.h 57.15% <ø> (ø)
sdk/include/opentelemetry/sdk/trace/tracer.h 100.00% <ø> (ø)
sdk/src/logs/logger.cc 94.12% <100.00%> (ø)
sdk/src/metrics/meter.cc 81.36% <100.00%> (ø)
sdk/src/trace/tracer.cc 80.22% <100.00%> (ø)
sdk/src/metrics/meter_provider.cc 85.51% <76.93%> (-6.80%) ⬇️
sdk/src/trace/tracer_provider.cc 83.96% <79.32%> (-4.75%) ⬇️
sdk/src/logs/logger_provider.cc 84.00% <70.00%> (-7.07%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code flagged noexcept should not raise exceptions

1 participant