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.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(); }); }
-
On .NET Framework an HTTP listener is automatically started which will respond to scraping requests. See the Configuration 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.
The PrometheusExporter
can be configured using the PrometheusExporterOptions
properties. Refer to
TestPrometheusExporter.cs
for example use.
Set to true
to start an HTTP listener which will respond to Prometheus scrape
requests using the HttpListenerPrefixes and
ScrapeEndpointPath 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.
Defines the prefixes which will be used by the listener when StartHttpListener
is true
. The default value is ["http://localhost:9464/"]
. You may specify
multiple endpoints.
For details see: HttpListenerPrefixCollection.Add(String)
Defines the path for the Prometheus scrape endpoint for
either the HTTP listener or 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 10000
(10 seconds). 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.