Prometheus Exporter AspNetCore for OpenTelemetry .NET
May 6, 2026 ยท View on GitHub
An OpenTelemetry Prometheus exporter for configuring an ASP.NET Core application with an endpoint for Prometheus to scrape.
Warning
This component is still under development due to a dependency on the experimental Prometheus and OpenMetrics Compatibility specification and can undergo breaking changes before stable release. Production environments should consider using OpenTelemetry.Exporter.OpenTelemetryProtocol. Refer to the Getting Started with Prometheus and Grafana tutorial for more information.
Important
The Prometheus scraping endpoint is not secured by default, so it is important to consider the security implications of exposing this endpoint in your application.
Refer to the Prometheus Security model and ASP.NET Core security documentation for more information and guidance on securing the Prometheus scraping endpoint to ensure only authorized users can access the information exposed by it.
Prerequisite
Steps to enable OpenTelemetry.Exporter.Prometheus.AspNetCore
Step 1: Install Package
dotnet add package --prerelease OpenTelemetry.Exporter.Prometheus.AspNetCore
Step 2: Configure OpenTelemetry MeterProvider
-
When using OpenTelemetry.Extensions.Hosting package on .NET 6.0+:
services.AddOpenTelemetry() .WithMetrics(builder => builder.AddPrometheusExporter()); -
Or configure directly:
Call the
MeterProviderBuilder.AddPrometheusExporterextension to register the Prometheus exporter.var meterProvider = Sdk.CreateMeterProviderBuilder() .AddPrometheusExporter() .Build(); builder.Services.AddSingleton(meterProvider);
Step 3: Configure Prometheus Scraping Endpoint
You can use register the Prometheus scraping middleware using the
MapPrometheusScrapingEndpoint extension method on
IEndpointRouteBuilder interface with
Minimal APIs.
For example:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPrometheusScrapingEndpoint();
You can use the IEndpointConventionBuilder returned by the extension
method to compose with other functionality, such as to exclude HTTP metrics
from the scraping endpoint itself. For example:
app.MapPrometheusScrapingEndpoint()
.DisableHttpMetrics();
If you are using the older Generic Host API
you can register the Prometheus scraping middleware with the
UseOpenTelemetryPrometheusScrapingEndpoint extension method on
IApplicationBuilder instead:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseOpenTelemetryPrometheusScrapingEndpoint();
Overloads of the UseOpenTelemetryPrometheusScrapingEndpoint extension are
provided to change the path or for more advanced configuration a predicate
function can be used:
app.UseOpenTelemetryPrometheusScrapingEndpoint(
context => context.Request.Path == "/internal/metrics" &&
context.Connection.LocalPort == 5067);
This can be used in combination with configuring multiple ports on the ASP.NET application to expose the scraping endpoint on a different port.
Configuration
The PrometheusExporter can be configured using the PrometheusAspNetCoreOptions
properties.
ScrapeEndpointPath
Defines the path for the Prometheus scrape endpoint for the middleware
registered by MapPrometheusScrapingEndpoint and
UseOpenTelemetryPrometheusScrapingEndpoint. Default value: "/metrics".
ScrapeResponseCacheDurationMilliseconds
Configures scrape endpoint response caching. Multiple scrape requests within the
cache duration time period will receive the same previously generated response.
The default value is 300. Set to 0 to disable response caching.
Troubleshooting
This component uses an EventSource with the name "OpenTelemetry-Exporter-Prometheus" for its internal logging. Please refer to SDK troubleshooting for instructions on seeing these internal logs.