Skip to content
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

[Otlp] Make sure Otlp trace and metric exporters have dedicated options instances #4200

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -73,16 +73,25 @@ public static MeterProviderBuilder AddOtlpExporter(

return builder.AddReader(sp =>
{
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
OtlpExporterOptions exporterOptions;

if (name == null && configureExporter != null)
if (name == null)
{
// If we are NOT using named options, we execute the
// configuration delegate inline. The reason for this is
// If we are NOT using named options we create a new
// instance always. The reason for this is
// OtlpExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
configureExporter(exporterOptions);
exporterOptions = sp.GetRequiredService<IOptionsFactory<OtlpExporterOptions>>().Create(finalOptionsName);

// Configuration delegate is executed inline on the fresh instance.
configureExporter?.Invoke(exporterOptions);
}
else
{
// When using named options we can properly utilize Options
// API to create or reuse an instance.
exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
}

return BuildOtlpExporterMetricReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,25 @@ public static TracerProviderBuilder AddOtlpExporter(

return builder.AddProcessor(sp =>
{
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
OtlpExporterOptions exporterOptions;

if (name == null && configure != null)
if (name == null)
{
// If we are NOT using named options, we execute the
// configuration delegate inline. The reason for this is
// If we are NOT using named options we create a new
// instance always. The reason for this is
// OtlpExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
configure(exporterOptions);
exporterOptions = sp.GetRequiredService<IOptionsFactory<OtlpExporterOptions>>().Create(finalOptionsName);

// Configuration delegate is executed inline on the fresh instance.
configure?.Invoke(exporterOptions);
}
else
{
// When using named options we can properly utilize Options
// API to create or reuse an instance.
exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
}

// Note: Not using finalOptionsName here for SdkLimitOptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ public void Null_BatchExportProcessorOptions_SupportedTest()
[Fact]
public void NonnamedOptionsMutateSharedInstanceTest()
{
var testOptionsInstance = new OtlpExporterOptions();

OtlpExporterOptions tracerOptions = null;
OtlpExporterOptions meterOptions = null;

Expand All @@ -649,11 +651,15 @@ public void NonnamedOptionsMutateSharedInstanceTest()
services.AddOpenTelemetry()
.WithTracing(builder => builder.AddOtlpExporter(o =>
{
Assert.Equal(testOptionsInstance.Endpoint, o.Endpoint);

tracerOptions = o;
o.Endpoint = new("http://localhost/traces");
}))
.WithMetrics(builder => builder.AddOtlpExporter(o =>
{
Assert.Equal(testOptionsInstance.Endpoint, o.Endpoint);

meterOptions = o;
o.Endpoint = new("http://localhost/metrics");
}));
Expand All @@ -676,12 +682,7 @@ public void NonnamedOptionsMutateSharedInstanceTest()
Assert.NotNull(meterOptions);
Assert.Equal("http://localhost/metrics", meterOptions.Endpoint.OriginalString);

// Note: tracerOptions & meterOptions are actually the same instance
// in memory and that instance was actually mutated after
// OtlpTraceExporter was created but this is OK because it doesn't
// use the options after ctor.
Assert.True(ReferenceEquals(tracerOptions, meterOptions));
Assert.Equal("http://localhost/metrics", tracerOptions.Endpoint.OriginalString);
Assert.False(ReferenceEquals(tracerOptions, meterOptions));
}

[Fact]
Expand Down