open-telemetry / opentelemetry-dotnet-contrib

This repository contains set of components extending functionality of the OpenTelemetry .NET SDK. Instrumentation libraries, exporters, and other components can find their home here.
https://opentelemetry.io
Apache License 2.0
460 stars 274 forks source link

Unable to have multiple unit tests when AddWcfInstrumentation is called. #1592

Open antmeehan opened 7 months ago

antmeehan commented 7 months ago

Issue with OpenTelemetry.Instrumentation.Wcf

List of all OpenTelemetry NuGet packages and version that you are using (e.g. OpenTelemetry 1.3.2):

Runtime version (e.g. net462, net48, net6.0, net7.0 etc. You can find this information from the *.csproj file):

Is this a feature request or a bug?

What is the expected behavior?

I was hoping there was a way to support having multiple unit tests where the SUT calls AddWcfInstrumentation() (e.g. as part of a host configuration).

What is the actual behavior?

You get the following exception:

System.NotSupportedException : WCF instrumentation has already been registered and doesn't support multiple registrations.
   at OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddWcfInstrumentation(TracerProviderBuilder builder, Action`1 configure)
   at OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddWcfInstrumentation(TracerProviderBuilder builder)

Additional Context

I can see that internally the library makes this call to reset the static reference causing the exception fire:

https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/de135415f5de21c0a565f3fb3b0570484e7a2bae/test/OpenTelemetry.Instrumentation.Wcf.Tests/TelemetryBindingElementForHttpTests.cs#L227C13-L227C61

WcfInstrumentationActivitySource.Options = null;

But WcfInstrumentationActivitySource is an internal class, so we can't reference it from our tests.

antmeehan commented 7 months ago

It's probably worth adding that you might normally exclude the OpenTelemetry from being configured during your unit tests, so this isn't that high a priority.