Skip to content

Latest commit

 

History

History
105 lines (87 loc) · 4.14 KB

File metadata and controls

105 lines (87 loc) · 4.14 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:            c1572aa14ee9c0ac037dbdc3e91e5dd7
Activity.SpanId:             45406137f33cc279
Activity.TraceFlags:         Recorded
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore
Activity.DisplayName:        /
Activity.Kind:               Server
Activity.StartTime:          2023-01-13T19:38:11.5417593Z
Activity.Duration:           00:00:00.0167407
Activity.Tags:
    net.host.name: localhost
    net.host.port: 5154
    http.method: GET
    http.scheme: http
    http.target: /
    http.url: http://localhost:5154/
    http.flavor: 1.1
    http.user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.76
    http.status_code: 200
Resource associated with Activity:
    service.name: getting-started-aspnetcore
    service.instance.id: 32c9371c-ed9d-474c-a698-b169e87a5577
    telemetry.sdk.name: opentelemetry
    telemetry.sdk.language: dotnet
    telemetry.sdk.version: 1.5.1

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