Skip to content

feat: add support for OTEL_SDK_DISABLED environment variable #3088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamsauravsharma
Copy link

@iamsauravsharma iamsauravsharma commented Jul 28, 2025

Fixes #1936

Changes

Add support for OTEL_SDK_DISABLED environment variable using no-op SDK implementation as explained Opentelemetry SDK environment variable docs. When set to true, the SDK will provide no-op implementations for all API components (Tracer, Logger, and Meter).

Implemented no-op provider classes that mimic the shutdown no-op providers for logger and tracer with only difference is shutdown variable which is set to false for disabled no op. Where as for metrics existing noop is re used

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@iamsauravsharma iamsauravsharma requested a review from a team as a code owner July 28, 2025 12:14
Copy link

linux-foundation-easycla bot commented Jul 28, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: iamsauravsharma / name: Saurav Sharma (59da472)

@iamsauravsharma iamsauravsharma force-pushed the add-env-otel-sdk-disabled branch from 00ed1c4 to cab02b4 Compare July 28, 2025 12:15
Copy link

codecov bot commented Jul 28, 2025

Codecov Report

❌ Patch coverage is 27.55906% with 92 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.8%. Comparing base (0462369) to head (59da472).

Files with missing lines Patch % Lines
opentelemetry-sdk/src/logs/logger_provider.rs 19.1% 38 Missing ⚠️
opentelemetry-sdk/src/trace/provider.rs 35.1% 35 Missing ⚠️
opentelemetry-sdk/src/metrics/meter_provider.rs 30.4% 16 Missing ⚠️
opentelemetry-sdk/src/logs/logger.rs 0.0% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #3088     +/-   ##
=======================================
- Coverage   80.1%   79.8%   -0.4%     
=======================================
  Files        126     126             
  Lines      21957   22078    +121     
=======================================
+ Hits       17603   17631     +28     
- Misses      4354    4447     +93     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@iamsauravsharma iamsauravsharma force-pushed the add-env-otel-sdk-disabled branch 2 times, most recently from 9fa6e81 to 3231e09 Compare July 28, 2025 14:17
@iamsauravsharma
Copy link
Author

iamsauravsharma commented Jul 28, 2025

Since overwriting OTEL_SDK_DISABLED environment variable would effects other test when running parallel test. 3 new test are ignored by default and scripts/test.sh is updated so 3 test run themselves in last step

@iamsauravsharma iamsauravsharma force-pushed the add-env-otel-sdk-disabled branch from 3231e09 to f587661 Compare July 28, 2025 14:48
@lalitb lalitb requested a review from Copilot July 28, 2025 15:49
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements support for the OTEL_SDK_DISABLED environment variable, which allows disabling the OpenTelemetry SDK at runtime. When set to "true", the SDK provides no-op implementations for all telemetry components (Tracer, Logger, and Meter) as specified in the OpenTelemetry specification.

Key changes:

  • Adds environment variable checking in all provider types to return no-op implementations when disabled
  • Implements separate disabled no-op providers distinct from shutdown no-op providers
  • Adds comprehensive test coverage for the new functionality

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
scripts/test.sh Adds test command for the new environment variable functionality
opentelemetry-sdk/src/trace/provider.rs Implements disabled tracer provider and environment variable checking
opentelemetry-sdk/src/metrics/meter_provider.rs Adds environment variable checking to meter provider
opentelemetry-sdk/src/logs/logger_provider.rs Implements disabled logger provider and environment variable checking
opentelemetry-sdk/CHANGELOG.md Documents the new feature addition
Comments suppressed due to low confidence (2)

opentelemetry-sdk/src/trace/provider.rs:782

  • [nitpick] Variable name 'noop_tracer' is misleading since this tests both disabled and enabled scenarios. Consider renaming to 'tracer' or 'test_tracer' for clarity.
            let noop_tracer = tracer_provider.tracer("noop");

opentelemetry-sdk/src/trace/provider.rs:804

  • [nitpick] Variable name 'noop_tracer' is misleading in this context where OTEL_SDK_DISABLED is set to 'false', meaning the tracer should be functional. Consider renaming to 'tracer' or 'functional_tracer'.
            let noop_tracer = tracer_provider.tracer("noop");

scripts/test.sh Outdated
@@ -20,6 +20,9 @@ cargo test --manifest-path=opentelemetry/Cargo.toml --no-default-features --lib
echo "Running tests for opentelemetry-prometheus with --all-features"
(cd opentelemetry-prometheus && cargo test --all-features --lib)

# Run test which set environment variable ``
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has an incomplete sentence and empty quotes. Should be: "Run test which sets environment variable"

Suggested change
# Run test which set environment variable ``
# Run test which sets the environment variable `otel_sdk_disabled_env`

Copilot uses AI. Check for mistakes.

return SdkTracer::new(scope, noop_tracer_provider().clone());
return SdkTracer::new(scope, shutdown_tracer_provider().clone());
}
if std::env::var("OTEL_SDK_DISABLED").is_ok_and(|var| var.to_lowercase() == "true") {
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variable is checked on every tracer creation. Consider caching this value or checking it once during provider initialization to avoid repeated environment variable lookups.

Copilot uses AI. Check for mistakes.

@@ -189,7 +189,9 @@ impl MeterProvider for SdkMeterProvider {
}

fn meter_with_scope(&self, scope: InstrumentationScope) -> Meter {
if self.inner.shutdown_invoked.load(Ordering::Relaxed) {
if self.inner.shutdown_invoked.load(Ordering::Relaxed)
|| std::env::var("OTEL_SDK_DISABLED").is_ok_and(|var| var.to_lowercase() == "true")
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variable is checked on every meter creation. Consider caching this value or checking it once during provider initialization to avoid repeated environment variable lookups.

Copilot uses AI. Check for mistakes.

return SdkLogger::new(scope, shutdown_logger_provider().clone());
}
// If the provider is disabled, new logger will refer a disabled no-op logger provider.
if std::env::var("OTEL_SDK_DISABLED").is_ok_and(|var| var.to_lowercase() == "true") {
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variable is checked on every logger creation. Consider caching this value or checking it once during provider initialization to avoid repeated environment variable lookups.

Suggested change
if std::env::var("OTEL_SDK_DISABLED").is_ok_and(|var| var.to_lowercase() == "true") {
if self.inner.is_disabled {

Copilot uses AI. Check for mistakes.

// a no op logger provider used as placeholder when sdk is disabled with
// help of environment variable `OTEL_SDK_DISABLED`
// TODO - replace it with LazyLock once it is stable
static DISABLED_LOGGER_PROVIDER: OnceLock<SdkLoggerProvider> = OnceLock::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reuse the existing NOOP_LOGGER_PROVIDER for both shutdown and disable ?

Copy link
Author

@iamsauravsharma iamsauravsharma Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting is_shutdown as true also for disabled one have no effect currently but to me true value looks wrong since logger provider is not in shutdown state but disabled by user so i have created new logger provider

return SdkTracer::new(scope, noop_tracer_provider().clone());
return SdkTracer::new(scope, shutdown_tracer_provider().clone());
}
if std::env::var("OTEL_SDK_DISABLED").is_ok_and(|var| var.to_lowercase() == "true") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the intended use case of "OTEL_SDK_DISABLED" env variable? Are we expected to re-read the env variable each time a tracer/logger/meter is requested? Or we read the env variable once at startup and not again?

It looks like the current PR is re-reading it each time. This can cause inconsistencies- tracers from before will continue to work vs new ones won't.. That is not the intended behavior in my opinion.

My suggestion:
Read the env variable once, at provider construction time, and store the value inside.
Any time tracer/logger/meter is requested, return NoOp if disabled. The log message should indicate NoOp is being returned due to the env-variable setting.

Happy to discuss other ideas.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented similar to python implementation open-telemetry/opentelemetry-python#3648 after reading discussion at open-telemetry/opentelemetry-specification#4332

Copy link
Member

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamsauravsharma iamsauravsharma force-pushed the add-env-otel-sdk-disabled branch 2 times, most recently from c990dd7 to 43f4bb0 Compare July 29, 2025 02:20
Signed-off-by: Saurav Sharma <appdroiddeveloper@gmail.com>
@iamsauravsharma iamsauravsharma force-pushed the add-env-otel-sdk-disabled branch from 43f4bb0 to 59da472 Compare August 2, 2025 09:28
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.

[Feature]: add support for environment variable OTEL_SDK_DISABLED
3 participants