Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When you run an application, you want to know how well the app is performing and to detect potential problems before they become larger. You can do this by emitting telemetry data such as logs or metrics from your app, then monitoring and analyzing that data.
Observability in the context of a distributed system is the ability to monitor and analyze telemetry about the state of each component, to be able to observe changes in performance, and to diagnose why those changes occur. Unlike debugging, which is invasive and can affect the operation of the application, observability is intended to be transparent to the primary operation and have a small enough performance impact that it can be used continuously.
Observability is commonly done using a combination of:
Together, logs, metrics, and distributed tracing are sometimes known as the three pillars of observability.
Each pillar might include telemetry data from:
HttpClient
.There are a few different ways to achieve observability in .NET applications:
OpenTelemetry (OTel) is a cross-platform, open standard for collecting and emitting telemetry data. OpenTelemetry includes:
Using OTel enables the use of a wide variety of APM systems including open-source systems such as Prometheus and Grafana, Azure Monitor - Microsoft's APM product in Azure, or from the many APM vendors that partner with OpenTelemetry.
There are OpenTelemetry implementations for most languages and platforms, including .NET.
The .NET OpenTelemetry implementation is a little different from other platforms, as .NET provides logging, metrics, and activity APIs in the framework. That means OTel doesn't need to provide APIs for library authors to use. The .NET OTel implementation uses these platform APIs for instrumentation:
Where OTel comes into play is that it collects telemetry from those APIs and other sources (via instrumentation libraries) and then exports them to an application performance monitoring (APM) system for storage and analysis. The benefit that OTel brings as an industry standard is a common mechanism for collection, common schemas and semantics for telemetry data, and an API for how APMs can integrate with OTel. Using OTel means that applications don't need to use APM-specific APIs or data structures; they work against the OTel standard. APMs can either implement an APM specific exporter component or use OTLP, which is a new wire standard for exporting telemetry data to the APM systems.
OpenTelemetry in .NET is implemented as a series of NuGet packages that form a couple of categories:
The following table describes the main packages.
Package Name | Description |
---|---|
OpenTelemetry | Main library that provides the core OTEL functionality |
OpenTelemetry.Instrumentation.AspNetCore | Instrumentation for ASP.NET Core and Kestrel |
OpenTelemetry.Instrumentation.GrpcNetClient | Instrumentation for gRPC Client for tracking outbound gRPC calls |
OpenTelemetry.Instrumentation.Http | Instrumentation for HttpClient and HttpWebRequest to track outbound HTTP calls |
OpenTelemetry.Instrumentation.SqlClient | Instrumentation for SqlClient used to trace database operations |
OpenTelemetry.Exporter.Console | Exporter for the console, commonly used to diagnose what telemetry is being exported |
OpenTelemetry.Exporter.OpenTelemetryProtocol | Exporter using the OTLP protocol |
OpenTelemetry.Exporter.Prometheus.AspNetCore | Exporter for Prometheus implemented using an ASP.NET Core endpoint |
OpenTelemetry.Exporter.Zipkin | Exporter for Zipkin tracing |
This topic is continued with a couple of example walkthroughs for using OpenTelemetry in .NET:
.NET Aspire is a set of extensions to .NET to make it easy to create and work with distributed applications. One of the benefits of using .NET Aspire is that telemetry is built in, using the OpenTelemetry libraries for .NET. The default project templates for .NET Aspire contain a ServiceDefaults
project, part of which is to setup and configure OTel. The Service Defaults project is referenced and initialized by each service in a .NET Aspire solution.
The Service Defaults project template includes the OTel SDK, ASP.NET, HttpClient and Runtime Instrumentation packages, and those are configured in the Extensions.cs
file. For exporting telemetry .NET Aspire includes the OTLP exporter by default so that it can provide telemetry visualization using the Aspire Dashboard.
The Aspire Dashboard is designed to bring telemetry observation to the local debug cycle, which enables developers to not only ensure that the applications are producing telemetry, but also use that telemetry to diagnose those applications locally. Being able to observe the calls between services is proving to be just as useful at debug time as in production. The .NET Aspire dashboard is launched automatically when you F5 the AppHost
Project from Visual Studio or dotnet run
the AppHost
project.
For more details on .NET Aspire see:
Probably the easiest way to configure OTel for ASP.NET projects is to use the Aspire Service Defaults project, even if not using the rest of .NET Aspire such as the AppHost for orchestration. The Service Defaults project is available as a project template via Visual Studio or dotnet new
. It configures OTel and sets up the OTLP exporter. You can then use the OTel environment variables to configure the OTLP endpoint to send telemetry to, and provide the resource properties for the application.
The steps to use ServiceDefaults outside .NET Aspire are:
dotnet new aspire-servicedefaults --output ServiceDefaults
var builder = WebApplication.CreateBuilder(args);
builder.ConfigureOpenTelemetry();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
Service Defaults can setup the following additional functionality if required via AddServiceDefaults()
or the specific functions:
/health
and /alive
endpoints.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Implement observability in a .NET cloud-native application with OpenTelemetry - Training
Learn about observability and how to implement it in a cloud-native application. Use OpenTelemetry packages to output logs, metrics, and tracing data and analyze the data in Application Insights and third-party applications.