-
Notifications
You must be signed in to change notification settings - Fork 847
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.Logs; | ||
|
||
|
@@ -91,6 +93,14 @@ internal LoggerProvider Build() | |
bool validateScopes = false; | ||
#endif | ||
var serviceProvider = services.BuildServiceProvider(validateScopes); | ||
var configuration = serviceProvider.GetRequiredService<IConfiguration>(); | ||
configuration.TryGetStringValue(SdkConfigDefinitions.SdkDisableEnvVarName, out var envVarValue); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 2 comments:
|
||
} | ||
|
||
return new LoggerProviderSdk(serviceProvider, ownsServiceProvider: true); | ||
} | ||
|
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 |
---|---|---|
@@ -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.
nit: Call
TryGetBoolValue