Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ internal void TestDefaultHistogramAggregationUsingConfiguration(string configVal
[InlineData("invalid", null)]
internal void TestDefaultHistogramAggregationUsingEnvVar(string configValue, MetricReaderHistogramAggregation? expectedAggregation)
{
using var scope = new EnvironmentVariableScope(OtlpSpecConfigDefinitions.MetricsDefaultHistogramAggregationEnvVarName, configValue);
using var scope = EnvironmentVariableScope.Create(OtlpSpecConfigDefinitions.MetricsDefaultHistogramAggregationEnvVarName, configValue);

var testExecuted = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void WhenOpenTelemetrySdkIsDisabledExceptionNotThrown()
// Arrange
var meterName = "TestMeter";

using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", "true"))
using (EnvironmentVariableScope.Create("OTEL_SDK_DISABLED", "true"))
{
var services = new ServiceCollection();

Expand Down
38 changes: 31 additions & 7 deletions test/OpenTelemetry.Tests/EnvironmentVariableScope.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry;

internal sealed class EnvironmentVariableScope : IDisposable
{
private readonly string name;
private readonly string? previous;
private readonly Dictionary<string, string?> originalEnvironment = [];
Comment thread
martincostello marked this conversation as resolved.
private bool disposed;

public EnvironmentVariableScope(string name, string? value)
private EnvironmentVariableScope(params ReadOnlySpan<(string Name, string? Value)> environment)
{
this.name = name;
this.previous = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, value);
foreach (var (name, value) in environment)
{
this.originalEnvironment[name] = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, value);
}
Comment thread
martincostello marked this conversation as resolved.
}

public void Dispose() => Environment.SetEnvironmentVariable(this.name, this.previous);
public static IDisposable Create(string name, string? value)
=> Create((name, value));

public static IDisposable Create(params ReadOnlySpan<(string Name, string? Value)> environment)
=> new EnvironmentVariableScope(environment);

public static IDisposable Create(IDictionary<string, string?> environment)
=> new EnvironmentVariableScope([.. environment.Select((p) => (p.Key, p.Value))]);

public void Dispose()
{
if (!this.disposed)
{
foreach (var pair in this.originalEnvironment)
{
Environment.SetEnvironmentVariable(pair.Key, pair.Value);
}

this.disposed = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class LoggerProviderBuilderBaseTests
[InlineData(null, typeof(LoggerProviderSdk))]
public void LoggerProviderIsExpectedType(string? value, Type expected)
{
using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value))
using (EnvironmentVariableScope.Create("OTEL_SDK_DISABLED", value))
{
var builder = new LoggerProviderBuilderBase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class MeterProviderBuilderBaseTests
[InlineData(null, typeof(MeterProviderSdk))]
public void LoggerProviderIsExpectedType(string? value, Type expected)
{
using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value))
using (EnvironmentVariableScope.Create("OTEL_SDK_DISABLED", value))
{
var builder = new MeterProviderBuilderBase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class TracerProviderBuilderBaseTests
[InlineData(null, typeof(TracerProviderSdk))]
public void TracerProviderIsExpectedType(string? value, Type expected)
{
using (new EnvironmentVariableScope("OTEL_SDK_DISABLED", value))
using (EnvironmentVariableScope.Create("OTEL_SDK_DISABLED", value))
{
var builder = new TestTracerProviderBuilder();

Expand Down Expand Up @@ -73,13 +73,9 @@ public void AddInstrumentationValidatesInputTest()
private sealed class TestTracerProviderBuilder : TracerProviderBuilderBase
{
public void AddInstrumentationViaProtectedMethod(Func<object?> factory)
{
this.AddInstrumentation("MyName", "MyVersion", factory);
}
=> this.AddInstrumentation("MyName", "MyVersion", factory);

public void AddInstrumentationViaProtectedMethod(string? name, string? version, Func<object?>? factory)
{
this.AddInstrumentation(name!, version!, factory!);
}
=> this.AddInstrumentation(name!, version!, factory!);
}
}
Loading