OpenTelemetry for .NET and ASP.NET Core

In .NET, tracing is built into the runtime: a span is a System.Diagnostics.Activity. The OpenTelemetry SDK listens to those activities and to ILogger, and one exporter sends all three signals to Maple.

GET api/orders/{id} 5 spans · 74ms
DurationStatus
api/orders/{id}orders-api
74.0msOk
SELECT app.Orders
12.0msOk
HTTP POST
46.0msOk
chargespayments-api
41.0msOk
INSERT app.Charges
15.0msOk

Example: an ASP.NET Core request calling another service

Quick start

Set up .NET

1

Add the packages

bash
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Instrumentation.Http
2

Register OpenTelemetry on the host

csharp
// Program.cs
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing => tracing.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation())
    .WithMetrics(metrics => metrics.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation())
    .WithLogging()
    .UseOtlpExporter();

var app = builder.Build();
app.MapGet("/", () => "Hello!");
app.Run();
3

Point the exporter at Maple

bash
export OTEL_SERVICE_NAME="my-dotnet-app"
export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.maple.dev"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_INGEST_KEY"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment.name=production"

dotnet run

This is the short path. The full .NET guide covers the rest.

Organization in the EU region? Send to https://ingest.eu.maple.dev instead of https://ingest.maple.dev. An ingest key only works in its own region.

What you get

What a .NET app sends to Maple

ASP.NET Core requests

Each request is one root server span named after its method and route, plus request duration metrics.

HttpClient calls

Client spans carry traceparent to the service you call. When that service is instrumented, the call is an edge on the service map.

EF Core and SqlClient

Add OpenTelemetry.Instrumentation.EntityFrameworkCore or SqlClient. Query spans carry the statement and the database system.

ILogger records

WithLogging() exports ILogger<T> records. A record written inside an activity carries its trace and span id.

Redis and gRPC clients

StackExchange.Redis and GrpcNetClient each have an instrumentation package, registered the same way.

Runtime metrics

OpenTelemetry.Instrumentation.Runtime adds .NET runtime metrics, exported every 60 seconds.

What is different in .NET

Activities are spans

Custom spans come from an ActivitySource. They are only exported when the source name is registered with AddSource; an unregistered source records nothing.

gRPC is the exporter default

Set OTEL_EXPORTER_OTLP_PROTOCOL to http/protobuf, as above, or the exporter speaks gRPC. With HttpProtobuf, an endpoint set in code needs the full /v1/traces path.

Console apps and workers

Without a host, build the providers with Sdk.CreateTracerProviderBuilder() and dispose them before exit, so the last batch is sent.

Integrations

Other integrations

See your first trace today.

Add the SDK, point your OTLP exporter at Maple, and traces arrive.

maple.dev: observability on OpenTelemetry