Skip to content

Latest commit

 

History

History
105 lines (87 loc) · 4.12 KB

File metadata and controls

105 lines (87 loc) · 4.12 KB

Getting Started with OpenTelemetry .NET Traces in 5 Minutes - ASP.NET Core Application

First, download and install the .NET SDK on your computer.

Create a new web application:

dotnet new web -o aspnetcoreapp
cd aspnetcoreapp

Install the OpenTelemetry.Exporter.Console, OpenTelemetry.Extensions.Hosting, and OpenTelemetry.Instrumentation.AspNetCore packages:

dotnet add package OpenTelemetry.Exporter.Console
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore

Update the Program.cs file with the code from Program.cs.

Run the application again (using dotnet run) and then browse to the url shown in the console for your application (ex http://localhost:5154). You should see the trace output from the console.

Activity.TraceId:            c28f7b480d5c7dfc30cfbd80ad29028d
Activity.SpanId:             27e478bbf9fdec10
Activity.TraceFlags:         Recorded
Activity.ActivitySourceName: Microsoft.AspNetCore
Activity.DisplayName:        GET /
Activity.Kind:               Server
Activity.StartTime:          2024-07-04T13:03:37.3318740Z
Activity.Duration:           00:00:00.3693734
Activity.Tags:
    server.address: localhost
    server.port: 5154
    http.request.method: GET
    url.scheme: https
    url.path: /
    network.protocol.version: 2
    user_agent.original: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
    http.route: /
    http.response.status_code: 200
Resource associated with Activity:
    service.name: getting-started-aspnetcore
    service.instance.id: a388466b-4969-4bb0-ad96-8f39527fa66b
    telemetry.sdk.name: opentelemetry
    telemetry.sdk.language: dotnet
    telemetry.sdk.version: 1.9.0

Congratulations! You are now collecting traces using OpenTelemetry.

What does the above program do?

The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests and uses the OpenTelemetry.Exporter.Console package to write traces to the console. This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started:

builder.Services.AddOpenTelemetry()
    .ConfigureResource(resource => resource
        .AddService(serviceName: builder.Environment.ApplicationName))
    .WithTracing(tracing => tracing
        .AddAspNetCoreInstrumentation()
        .AddConsoleExporter());

Note

The AddOpenTelemetry extension is part of the OpenTelemetry.Extensions.Hosting package.

The index route ("/") is set up to write out the OpenTelemetry trace information on the response:

app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");

In OpenTelemetry .NET the Activity class represents the OpenTelemetry Specification Span. For more details about how the OpenTelemetry Specification is implemented in .NET see: Introduction to OpenTelemetry .NET Tracing API.

Learn more