azure-sdk-for-net
azure-sdk-for-net copied to clipboard
[BUG] Instrumentation Options delegates get called multiple times
Library name and version
Azure.Monitor.OpenTelemetry.AspNetCore
Describe the bug
The AzureMonitor Distro (Azure.Monitor.OpenTelemetry.AspNetCore) automatically includes some Instrumentation libraries. If a user adds the same Instrumentation library again, we expect there should be no conflict.
Unfortunately, it seems that the delegate methods can get executed multiple times. This has only been confirmed with the AspNetCore Instrumentation's EnrichWithHttpRequest library, but I suspect it applies to each Instrumentation library.
Example
builder.Services.AddOpenTelemetry().UseAzureMonitor()
.WithTracing(builder =>
{
builder.AddAspNetCoreInstrumentation(options =>
{
options.EnrichWithHttpRequest = (activity, HttpResponse) =>
{
// this will be called twice
Console.WriteLine($"EnrichWithHttpRequest was called for activity: {activity.Id}");
};
});
});
Workaround
The workaround is to use the Configure Api to set an Instrumentation library's options.
This is already described in our Readme and public docs, but users are not discovering this pattern and doing the above instead.
builder.Services.AddOpenTelemetry().UseAzureMonitor()
builder.Services.Configure<AspNetCoreTraceInstrumentationOptions>(options =>
{
options.EnrichWithHttpRequest = (activity, HttpResponse) =>
{
// this will be called once
Console.WriteLine($"EnrichWithHttpRequest was called for activity: {activity.Id}");
};
});
Expected behavior
N/A
Actual behavior
N/A
Reproduction Steps
N/A
Environment
No response
I've opened an issue with the Instrumentation libraries as I believe this issue needs to be addressed there. https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/2247
Confirmed this is an issue in the Instrumentation libraries and not the Azure Monitor libraries. I'm closing this issue in favor of continuing the conversation in the OpenTelemetry repo