Skip to content

IHttpActivityFeature.Activity and Activity.Current don't return the same activity #4466

@ghost

Description

Description

Considering the following example:

using System.Diagnostics;
using Microsoft.AspNetCore.Http.Features;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(builder =>
    {
        builder
            .SetSampler(new AlwaysOnSampler()) // note here I use the AlwaysOn sampler to always record and sample activities
            .AddAspNetCoreInstrumentation()
            .AddConsoleExporter();
    });

var app = builder.Build();

app.MapGet("/values", (HttpContext context) =>
{
    var activity = context.Features.GetRequiredFeature<IHttpActivityFeature>().Activity;
    activity.SetTag("test", "123");

    Activity.Current?.SetTag("test2", "123");

    return Results.Ok();
});

app.Run();

When the application receives a traceparent header with a trace-flags 00 (not sampled),activity.Id and Activity.Current.Id don't have the same id but both activities have the same name: Microsoft.AspNetCore.Hosting.HttpRequestIn.
In addition, only tags added through Activity.Current.SetTag are taken into account.

image

This seems to be a bug or an unexpected behaviour with the OpenTelemetry SDK as without the SDK both activities are the same and both tags are taken into account.

What is the expected behavior?

Both activities (the one from IHttpActivityFeature.Activity and the one from Activity.Current) should be the same.

What is the actual behavior?

Both activities are not the same.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions