'How to collect requestIn activity trace information?

<PackageReference Include="OpenTelemetry" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Api" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9" />
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics;

var builder = WebApplication.CreateBuilder(args);

var serviceName = "Demo";
var serviceVersion = "1.0.0";
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddOpenTelemetryTracing(b =>
{
    b
    .SetSampler(new AlwaysOnSampler())
    .AddSource(serviceName)
    .SetResourceBuilder(
        ResourceBuilder.CreateDefault()
            .AddService(serviceName: serviceName, serviceVersion: serviceVersion))
    .AddConsoleExporter()
    ;
});
builder.Services.AddSingleton(TracerProvider.Default.GetTracer(serviceName));

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.MapGet("/hello", (Tracer tracer) =>
{
    var current = Activity.Current;
    using var span = tracer.StartActiveSpan("hello-span");

});
app.Run();

“hello-span” is output. I debug the variable current, and there is an operation name "Microsoft. Aspnetcore. Hosting. Httprequestin".

However, this activity is not output to the console. I want to output it. What should I do.

I've found a solution

<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9" />

This package creates a new activity based on the built-in requestin activity and provides a way to enrich this activity.

So I can add some tags and filters in the request process.

Filter the data according to the active tag, and then export it.



Solution 1:[1]

I have test the code you provided, and it works for me. Please check the result first, does it you want?

enter image description here

My test steps:

  1. Choose IIS Express first, then run it.

    enter image description here

  2. Open output window, and choose Project_Name-Asp.Net Core Web Server.

    enter image description here

  3. Then you can check the message here when access the url http://xxx:port/hello.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason