An OpenTelemetry Prometheus exporter for configuring an ASP.NET Core application with an endpoint for Prometheus to scrape.
dotnet add package --prerelease OpenTelemetry.Exporter.Prometheus.AspNetCore
-
When using OpenTelemetry.Extensions.Hosting package on .NET 6.0+:
services.AddOpenTelemetryMetrics(builder => { builder.AddPrometheusExporter(); });
-
Or configure directly:
Call the
MeterProviderBuilder.AddPrometheusExporter
extension to register the Prometheus exporter.var meterProvider = Sdk.CreateMeterProviderBuilder() .AddPrometheusExporter() .Build(); builder.Services.AddSingleton(meterProvider);
-
Register Prometheus scraping middleware using the
UseOpenTelemetryPrometheusScrapingEndpoint
extension:public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOpenTelemetryPrometheusScrapingEndpoint(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
Overloads of the
UseOpenTelemetryPrometheusScrapingEndpoint
extension are provided to change the path or for more advanced configuration a predicate function can be used:public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOpenTelemetryPrometheusScrapingEndpoint( context => context.Request.Path == "/internal/metrics" && context.Connection.LocalPort == 5067); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
The PrometheusExporter
can be configured using the PrometheusExporterOptions
properties.
Defines the path for the Prometheus scrape endpoint for the middleware
registered by
UseOpenTelemetryPrometheusScrapingEndpoint
. Default value: "/metrics"
.
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.
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.