Skip to content

Latest commit

 

History

History
120 lines (88 loc) · 4.53 KB

File metadata and controls

120 lines (88 loc) · 4.53 KB

Prometheus Exporter AspNetCore for OpenTelemetry .NET

NuGet NuGet

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.

Note

This exporter does not support Exemplars. For using Exemplars, use the OTLP Exporter and use a component like OTel Collector to expose metrics (with exemplars) to Prometheus. This tutorial shows one way how to do that.

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.AddPrometheusExporter extension to register the Prometheus exporter.

    var meterProvider = Sdk.CreateMeterProviderBuilder()
        .AddPrometheusExporter()
        .Build();
    builder.Services.AddSingleton(meterProvider);

Step 3: Configure Prometheus Scraping Endpoint

  • Register Prometheus scraping middleware using the UseOpenTelemetryPrometheusScrapingEndpoint extension method on IApplicationBuilder :

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

References