opentelemetry-dotnet-contrib icon indicating copy to clipboard operation
opentelemetry-dotnet-contrib copied to clipboard

Adding only HttpClient instrumentation to ASP.NET Core app does not work

Open alanwest opened this issue 4 years ago • 10 comments

Bug Report

If you only add HttpClient instrumentation to an ASP.NET Core app, no spans from HttpClient are collected.

The issue is not specific to HttpClient instrumentation. I have also reproduced this when adding only Grpc.Net.Client instrumentation.

Reproduce

dotnet new webapi
dotnet add package --version 0.5.0-beta.2 OpenTelemetry.Instrumentation.Http
dotnet add package --version 0.5.0-beta.2 OpenTelemetry.Exporter.Console
dotnet add package --version 0.5.0-beta.2 OpenTelemetry.Instrumentation.AspNetCore

# Replace contents of Startup.cs with code below

dotnet run

# Navigate to https://localhost:5001/WeatherForecast
# This example uses the Console exporter. Observe no spans generated.
# Follow the instructions in the code comments to resolve the issue.
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOpenTelemetryTracerProvider(builder => {
            builder
                // Uncomment the following line enabling AspNetCore instrumentation
                // This will result in seeing a span for both AspNetCore requests and the HttpClient invocation
                // .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddConsoleExporter();
        });

        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            await next.Invoke();
            using var client = new HttpClient();
            await client.GetStringAsync("https://opentelemetry.io");
        });

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Update

See my analysis below for the reason why this occurs. Some thoughts come to my mind on how best to address this issue.

  1. We could document this behavior and note that ASP.NET Core instrumentation is required.
  2. We could also document any potential workarounds. For instance, using a different sampler rather than the default ParentBasedSampler.
  3. Though, if we want to pursue making this work as is, then I think we need to devise a way to ignore the Activity created by ASP.NET Core in sampling decisions when OpenTelemetry has not been configured to use ASP.NET Core instrumentation.

alanwest avatar Sep 01 '20 21:09 alanwest