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
438 stars 268 forks source link

I Could not extract the traceparent using AspNet Instrumentation. #1632

Open LynnLi11 opened 4 months ago

LynnLi11 commented 4 months ago

Issue with OpenTelemetry.Instrumentation.AspNet

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

<Reference Include="OpenTelemetry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.1.3.2\lib\net462\OpenTelemetry.dll</HintPath>
    </Reference>
    <Reference Include="OpenTelemetry.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.Api.1.3.2\lib\net462\OpenTelemetry.Api.dll</HintPath>
    </Reference>
    <Reference Include="OpenTelemetry.Exporter.Console, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.Exporter.Console.1.3.2\lib\net462\OpenTelemetry.Exporter.Console.dll</HintPath>
    </Reference>
    <Reference Include="OpenTelemetry.Extensions.Hosting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.Extensions.Hosting.1.0.0-rc9\lib\netstandard2.0\OpenTelemetry.Extensions.Hosting.dll</HintPath>
    </Reference>
    <Reference Include="OpenTelemetry.Instrumentation.AspNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.Instrumentation.AspNet.1.0.0-rc9.1\lib\net461\OpenTelemetry.Instrumentation.AspNet.dll</HintPath>
    </Reference>
    <Reference Include="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bd6737fe5b67e3c, processorArchitecture=MSIL">
      <HintPath>..\packages\OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.1.0.0-rc9.1\lib\net461\OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.dll</HintPath>
    </Reference>

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

Is this a feature request or a bug?

What is the expected behavior?

ServiceProvider serviceProvider = s.BuildServiceProvider(); var hostedServices = serviceProvider.GetServices(); foreach (var hostedService in hostedServices) { _ = hostedService.StartAsync(CancellationToken.None); }

* I set the traceparent in request header and sent to my application
  * traceparent:00-6ea83d62e2dfba7068d3c046d04ae895-c52c52ddd45dfd29-01
* Activity started by AspNet Instrumentation should has parent and traceId should be set to `6ea83d62e2dfba7068d3c046d04ae895`

**What is the actual behavior?**
* But the activity created by AspNet Instrumentation does not has a parent:
![image](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/assets/110374457/9fca32c4-8072-4df5-8a30-8b43320caf4a)

** Possible reason:
> I read the code of AspNet Instrumentation, found that the propagator of HttpTelemetryModule would be set to the `DefaultTextMapPropagator`, but once not initializing OpenTelemetry with `Sdk.AddOpentelemetryTracing()`, the `DefaultTextMapPropagator` would be Noop instead of `CompositeTextMapPropagator` or `TraceContextPropagator`.

## Additional Context
I Set the 
```csharp
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
    new TraceContextPropagator(),
    new BaggagePropagator(),
}));

Then I get the expected behavior.

Is there any possibility that AspNet Intstrumentation would not rely on the default propagator? Or providing a way to reset the propagator specially for HttpTelemetryModule?

Kielek commented 4 months ago

@LynnLi11,

I think that you could try to use method described in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/Instrumentation.AspNet-1.8.0-beta.1/src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/README.md#textmappropagator

There is a static property you could set (when the OTel registration complete) by

TelemetryHttpModule.Options.TextMapPropagator = new CompositeTextMapPropagator(/* put something here */)

I didn't check with older version than 1.8.0-beta.1, but I suppose that it was not changed (but consider upgrading your packages).