OpenTelemetry Metrics
OpenTelemetry is an observability framework that provides APIs, libraries, agents, and instrumentation to collect distributed traces and metrics from an application. It's an open-source, CNCF project that aims to standardize the generation and collection of telemetry data (metrics, logs, and traces) for cloud-native software.
In this article, you will learn how to export custom OpenTelemetry metrics from your ASP.NET Core applications to Azure Monitor.
Here are the steps to follow along.
- Create a new Application Insights resource in Azure. You can use the detailed instructions in this Microsoft Learn guide to create the resource using the Azure Portal or Azure CLI. Please copy the connection string for the resource as you will need it later.

- Create a new ASP.NET Core Minimal API project using your preferred IDE or the .NET CLI. Please follow the instructions in this Microsoft guide to create the project. Use an officially supported version of .NET Core to avoid any issues.
- Install the latest version of the following NuGet packages in your project.
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
- Replace the contents of the Program.cs file with the following code.
using System.Diagnostics.Metrics; using Azure.Monitor.OpenTelemetry.AspNetCore; using Azure.Monitor.OpenTelemetry.Exporter; using OpenTelemetry.Metrics; var builder = WebApplication.CreateBuilder(args); // Configure the OpenTelemetry meter provider to add a meter named "OTel.AzureMonitor.Demo". builder.Services.ConfigureOpenTelemetryMeterProvider((sp, builder) => builder.AddMeter("OTel.AzureMonitor.Demo")); // Add the Azure Monitor telemetry service to the application. // This service will collect and send telemetry data to Azure Monitor. builder.Services.AddOpenTelemetry().UseAzureMonitor(); // Build the ASP.NET Core web application. var app = builder.Build(); // Create a new meter named "OTel.AzureMonitor.Demo". var meter = new Meter("OTel.AzureMonitor.Demo"); // Create a new counter metric named "MyDiceCounter". var myDiceCounter = meter.CreateCounter<long>("MyDiceCounter"); app.MapGet("/metrics-demo", () => { var gameId = Guid.NewGuid().ToString(); for (var i = 0; i < 5; i++) { var diceValue = new Random().Next(1, 6); myDiceCounter.Add(1, new("faceValue", diceValue), new("gameId", gameId)); } return "Counter incremented"; }); // Run the application. app.Run();



Join the conversation! Your thoughts help the community grow.