-
Notifications
You must be signed in to change notification settings - Fork 857
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?
Changes from all commits
3603f51
947fcb1
ba7d7ae
6244098
ae28768
583b34b
ca695bf
bbf01d4
d35a343
c51cd9e
0aa2dc3
ecacbf2
86b050b
d35c752
dee73a9
538ae6c
abc9d43
aa6027c
14114da
a911411
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace OpenTelemetry; | ||
|
|
||
| internal static class SdkConfigDefinitions | ||
| { | ||
| public const string SdkDisableEnvVarName = "OTEL_SDK_DISABLED"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using OpenTelemetry.Internal; | ||
| using static OpenTelemetry.OpenTelemetrySdk; | ||
|
|
||
| namespace OpenTelemetry.Trace; | ||
|
|
||
|
|
@@ -39,7 +41,17 @@ internal TracerProviderBuilderBase(IServiceCollection services) | |
|
|
||
| services | ||
| .AddOpenTelemetryTracerProviderBuilderServices() | ||
| .TryAddSingleton<TracerProvider>(sp => new TracerProviderSdk(sp, ownsServiceProvider: false)); | ||
| .TryAddSingleton<TracerProvider>(sp => | ||
| { | ||
| if (IsOtelSdkDisabled(sp.GetRequiredService<IConfiguration>())) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there may be a problem here, as it appears that service provider is not being disposed of.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rajkumar-rangaraj I’ve taken another look, but I think my comment still applies. In this hosting scenario, the |
||
| { | ||
| var noopTracerProvider = new NoopTracerProvider(); | ||
| noopTracerProvider.Dispose(); | ||
| return noopTracerProvider; | ||
| } | ||
|
|
||
| return new TracerProviderSdk(sp, ownsServiceProvider: false); | ||
| }); | ||
|
|
||
| this.innerBuilder = new TracerProviderServiceCollectionBuilder(services); | ||
|
|
||
|
|
@@ -146,7 +158,25 @@ protected TracerProvider Build() | |
| bool validateScopes = false; | ||
| #endif | ||
| var serviceProvider = services.BuildServiceProvider(validateScopes); | ||
| var configuration = serviceProvider.GetRequiredService<IConfiguration>(); | ||
|
|
||
| if (IsOtelSdkDisabled(configuration)) | ||
| { | ||
| serviceProvider.Dispose(); | ||
| return new NoopTracerProvider(); | ||
| } | ||
|
|
||
| return new TracerProviderSdk(serviceProvider, ownsServiceProvider: true); | ||
| } | ||
|
|
||
| private static bool IsOtelSdkDisabled(IConfiguration configuration) | ||
| { | ||
| bool isDisabled = configuration.TryGetBoolValue(OpenTelemetrySdkEventSource.Log, SdkConfigDefinitions.SdkDisableEnvVarName, out bool result) && result; | ||
| if (isDisabled) | ||
| { | ||
| OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Disabled because {SdkConfigDefinitions.SdkDisableEnvVarName} is true."); | ||
| } | ||
|
|
||
| return isDisabled; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| internal sealed class EnvironmentVariableScope : IDisposable | ||
| { | ||
| private readonly string name; | ||
| private readonly string? previous; | ||
|
|
||
| public EnvironmentVariableScope(string name, string? value) | ||
| { | ||
| this.name = name; | ||
| this.previous = Environment.GetEnvironmentVariable(name); | ||
| Environment.SetEnvironmentVariable(name, value); | ||
| } | ||
|
|
||
| public void Dispose() => Environment.SetEnvironmentVariable(this.name, this.previous); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using Xunit; | ||
| using static OpenTelemetry.OpenTelemetrySdk; | ||
|
|
||
| namespace OpenTelemetry.Logs.Tests; | ||
|
|
||
| public sealed class LoggerProviderBuilderBaseTests | ||
| { | ||
| [Theory] | ||
| [InlineData("true", typeof(NoopLoggerProvider))] | ||
| [InlineData("false", typeof(LoggerProviderSdk))] | ||
| [InlineData(null, typeof(LoggerProviderSdk))] | ||
| public void LoggerProviderIsExpectedType(string? value, Type expected) | ||
| { | ||
| using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value)) | ||
| { | ||
| var builder = new LoggerProviderBuilderBase(); | ||
|
|
||
| using var provider = builder.Build(); | ||
|
|
||
| Assert.IsType(expected, provider); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using Xunit; | ||
| using static OpenTelemetry.OpenTelemetrySdk; | ||
|
|
||
| namespace OpenTelemetry.Metrics.Tests; | ||
|
|
||
| public sealed class MeterProviderBuilderBaseTests | ||
| { | ||
| [Theory] | ||
| [InlineData("true", typeof(NoopMeterProvider))] | ||
| [InlineData("false", typeof(MeterProviderSdk))] | ||
| [InlineData(null, typeof(MeterProviderSdk))] | ||
| public void LoggerProviderIsExpectedType(string? value, Type expected) | ||
| { | ||
| using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value)) | ||
| { | ||
| var builder = new MeterProviderBuilderBase(); | ||
|
|
||
| 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.
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?