-
Notifications
You must be signed in to change notification settings - Fork 848
Add support for OTEL_SDK_DISABLED environment variable #6568
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
base: main
Are you sure you want to change the base?
Add support for OTEL_SDK_DISABLED environment variable #6568
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #6568 +/- ##
==========================================
- Coverage 86.75% 86.72% -0.04%
==========================================
Files 258 258
Lines 11910 11958 +48
==========================================
+ Hits 10332 10370 +38
- Misses 1578 1588 +10
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…ahhaering/opentelemetry-dotnet into support-sdk-disabled-envVar
test/OpenTelemetry.Tests/Logs/LoggerProviderBuilderBaseTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Martin Costello <martin@martincostello.com>
public LoggerProviderBuilderBaseTests() | ||
{ | ||
Environment.SetEnvironmentVariable(SdkConfigDefinitions.SdkDisableEnvVarName, null); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable(SdkConfigDefinitions.SdkDisableEnvVarName, null); | ||
GC.SuppressFinalize(this); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better way to do this, IMHO, would be to:
- Not set the value in the constructor
- Let the test set the value it's interested in
- Set it back to the original value when being disposed
You could use a similar pattern to this to implement it in a cross-cutting way, then the usage would be something like:
using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value))
{
var builder = new LoggerProviderBuilderBase();
using var provider = builder.Build();
Assert.IsType(expected, provider);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion, I've done that.
Is there any way I can retry the workflow without a commit? Sometimes the build fails even though I have no errors (like right now).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not without write access to the repo, no.
test/OpenTelemetry.Tests/Logs/LoggerProviderBuilderBaseTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Martin Costello <martin@martincostello.com>
@CodeBlanch Would you mind taking a look at this please? |
if (bool.TryParse(envVarValue, out bool result) && result) | ||
{ | ||
serviceProvider.Dispose(); | ||
return new NoopLoggerProvider(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 comments:
-
Would probably be good to add a log here. Don't want support tickets raised for missing telemetry when users have shot themselves in the foot 😄
-
This won't work for hosting scenarios. See line 80. This code is only invoked for manual/detached bootstrap. Something else will need to be done for hosting style. Ideally we would have a single solution for both but I haven't looked at where that might go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint. Now it should work for both scenarios. Could you please check again?
This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day. |
There was a problem hiding this 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 according to the OpenTelemetry specification. When this environment variable is set to true
, the SDK returns no-op provider implementations instead of functional ones, effectively disabling telemetry collection.
Key Changes:
- Added
OTEL_SDK_DISABLED
environment variable support across all three telemetry signals (tracing, metrics, logging) - Introduced
SdkConfigDefinitions
class to centralize the environment variable name - Created
EnvironmentVariableScope
test helper for managing environment variables in unit tests
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
SdkConfigDefinitions.cs |
Defines the OTEL_SDK_DISABLED constant for centralized configuration |
TracerProviderBuilderBase.cs |
Checks OTEL_SDK_DISABLED and returns NoopTracerProvider when disabled |
MeterProviderBuilderBase.cs |
Checks OTEL_SDK_DISABLED and returns NoopMeterProvider when disabled |
LoggerProviderBuilderBase.cs |
Checks OTEL_SDK_DISABLED and returns NoopLoggerProvider when disabled |
EnvironmentVariableScope.cs |
Test utility class for temporarily setting environment variables |
TracerProviderBuilderBaseTests.cs |
Unit tests verifying SDK disable functionality for tracer provider |
MeterProviderBuilderBaseTests.cs |
Unit tests verifying SDK disable functionality for meter provider |
LoggerProviderBuilderBaseTests.cs |
Unit tests verifying SDK disable functionality for logger provider |
CHANGELOG.md |
Documents the new feature |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Fixes #4155
Changes
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes