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
421 stars 251 forks source link

Traces aren't correlated when using Microsoft.AspNetCore version 2.2.0 #1868

Closed juliuskoval closed 2 weeks ago

juliuskoval commented 2 weeks ago

Component

OpenTelemetry.Instrumentation.AspNetCore

Package Version

Package Name Version
Microsoft.AspNetCore 2.2.0
Microsoft.AspNetCore.Mvc 2.2.0
Microsoft.AspNetCore.Server.HttpSys 2.2.0
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.9.0-alpha1
OpenTelemetry.Instrumentation.AspNetCore 1.8.1

Runtime Version

net48

Description

If I run an app like below and call its API, the resulting span will be named Microsoft.AspNetCore.Hosting.HttpRequestIn and it will not be correlated with the trace from the app that called the API. The reason seems to be this piece of code from Microsoft.AspNetCore.Hosting.Internal.HostingApplicationDiagnostics:

if (_diagnosticListener.IsEnabled(ActivityStartKey)) { _diagnosticListener.StartActivity(activity, new { HttpContext = httpContext }); }

https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/4ee07f40efc5438816b718549fc99b6a0cdc3e1b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs#L95-L106

The expected payload will be the HttpContext property of the actual payload, so context will be null.

Steps to Reproduce

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Resources;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System;

class Program
{
    public static async Task Main(string[] args)
    {
        var prefix = "http://localhost:999";
        var host = WebHost
            .CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseHttpSys(x =>
            {
                x.UrlPrefixes.Add(prefix);
                x.Authentication.AllowAnonymous = true;
            })
            .Build();
        await host.StartAsync();

        await Task.Delay(1000000);
    }
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton(Sdk
            .CreateTracerProviderBuilder()
            .SetResourceBuilder(ResourceBuilder
                .CreateDefault()
                .AddService("TestService"))
            .AddAspNetCoreInstrumentation()
            .AddOtlpExporter()
            .Build());
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet("Get")]
    public void Get()
    {
        Console.WriteLine("Testing");
    }
}

Expected Result

The span will be correctly named and correlated with its parent span.

Actual Result

It won't be

Additional Context

No response

vishweshbankwar commented 2 weeks ago

@juliuskoval - This scenario is not supported. Apps targeting net6.0 or above are supported only.