Skip to content

Latest commit

 

History

History
118 lines (84 loc) · 3.85 KB

File metadata and controls

118 lines (84 loc) · 3.85 KB

Prometheus Exporter for OpenTelemetry .NET

NuGet NuGet

Prerequisite

Steps to enable OpenTelemetry.Exporter.Prometheus

Step 1: Install Package

dotnet add package OpenTelemetry.Exporter.Prometheus

Step 2: Configure OpenTelemetry MeterProvider

  • 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();

Step 3: Configure Prometheus Scraping Endpoint

  • 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 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.

Configuration

The PrometheusExporter can be configured using the PrometheusExporterOptions properties. Refer to TestPrometheusExporter.cs for example use.

StartHttpListener

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 the UseOpenTelemetryPrometheusScrapingEndpoint extension to register the scraping middleware instead of using the listener.

HttpListenerPrefixes

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)

ScrapeEndpointPath

Defines the path for the Prometheus scrape endpoint for either the HTTP listener or the middleware registered by 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 10000 (10 seconds). 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.

References