dotnet add package OpenTelemetry.Exporter.Prometheus
-
When using OpenTelemetry.Extensions.Hosting package on .NET Core 3.1+:
services.AddOpenTelemetryMetrics(builder => { builder.AddPrometheusExporter(); });
-
Or configure directly:
Call the
AddPrometheusExporter
MeterProviderBuilder
extension to register the Prometheus exporter.using var meterProvider = Sdk.CreateMeterProviderBuilder() .AddPrometheusExporter() .Build();
-
On .NET Core 3.1+ register Prometheus scraping middleware using the
UseOpenTelemetryPrometheusScrapingEndpoint
extension:public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseOpenTelemetryPrometheusScrapingEndpoint(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
-
On .NET Framework an http listener is automatically started which will respond to scraping requests. See the Options Properties section for details on the settings available. This may also be turned on in .NET Core (it is OFF by default) when the ASP.NET Core pipeline is not available for middleware registration.
You can configure the PrometheusExporter
through PrometheusExporterOptions
.
The PrometheusExporter
can be configured using the PrometheusExporterOptions
properties:
-
StartHttpListener
: Set totrue
to start an http listener which will respond to Prometheus scrape requests using theHttpListenerPrefixes
andScrapeEndpointPath
options.Defaults:
-
On .NET Framework this is
true
by default. -
On .NET Core 3.1+ this is
false
by default. Users running ASP.NET Core should use theUseOpenTelemetryPrometheusScrapingEndpoint
extension to register the scraping middleware instead of using the listener.
-
-
HttpListenerPrefixes
: Defines the prefixes which will be used by the listener whenStartHttpListener
istrue
. The default value is["http://*:80/"]
. You may specify multiple endpoints.For details see: HttpListenerPrefixCollection.Add(String)
-
ScrapeEndpointPath
: Defines the path for the Prometheus scrape endpoint for either the http listener or the middleware registered byUseOpenTelemetryPrometheusScrapingEndpoint
. 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 is10000
(10 seconds). Set to0
to disable response caching.
See
TestPrometheusExporter.cs
for example use.