Modern distributed applications are composed of multiple services, APIs, databases, and external dependencies. When a request fails or performance degrades, identifying the root cause becomes challenging without proper observability.
Traditional logging alone is no longer sufficient. Developers need visibility into how requests flow across services, where latency occurs, and which components contribute to failures. This is where OpenTelemetry comes in.
OpenTelemetry is an open-source observability framework that standardizes the collection of logs, metrics, and distributed traces. It enables developers to monitor applications consistently while remaining vendor-neutral.
In this guide, you'll learn how to integrate OpenTelemetry into an ASP.NET Core application and adopt best practices for production environments.
What Is OpenTelemetry?
OpenTelemetry (OTel) provides a unified way to collect telemetry data from applications.
It focuses on three pillars of observability:
Logs – Record application events and errors.
Metrics – Measure application health and performance.
Traces – Track a request across multiple services.
Together, these signals help answer critical questions such as:
Why is an API slow?
Which service caused a failure?
How long does each database call take?
Where is the application's bottleneck?
Unlike vendor-specific SDKs, OpenTelemetry allows you to send telemetry to multiple monitoring platforms without changing application code.
Understanding the OpenTelemetry Architecture
A typical OpenTelemetry workflow looks like this:
ASP.NET Core Application
│
OpenTelemetry SDK
│
Telemetry Data
│
OpenTelemetry Collector (Optional)
│
Monitoring Platform
The SDK collects telemetry, while the optional OpenTelemetry Collector processes and exports data to systems like Azure Monitor, Jaeger, Prometheus, Grafana, or Elastic.
Installing OpenTelemetry
Install the required NuGet packages:
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Instrumentation.Http
dotnet add package OpenTelemetry.Instrumentation.SqlClient
dotnet add package OpenTelemetry.Exporter.Console
These packages enable automatic instrumentation for common ASP.NET Core components.
Configuring OpenTelemetry
Register OpenTelemetry during application startup.
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService("ProductApi"))
.WithTracing(tracing =>
{
tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.AddConsoleExporter();
});
var app = builder.Build();
app.MapControllers();
app.Run();
This configuration automatically captures incoming HTTP requests, outgoing HTTP calls, SQL queries, and exports trace information to the console.
Collecting Metrics
Metrics help monitor application performance over time.
Configure metrics collection:
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddConsoleExporter();
});
Common metrics include:
Request count
Request duration
CPU usage
Memory consumption
Active requests
These measurements help identify trends before they become production issues.
Join the conversation! Your thoughts help the community grow.