asp.net-corekubernetesprometheusasp.net-core-3.1prometheus-net

Expose ASP.NET Core Prometheus metrics on another port


I'm setting up a Prometheus exporter for my ASP.NET Core 3.1 app.

I've imported

<PackageReference Include="prometheus-net.AspNetCore" Version="4.1.1" />

And this is what I have configured:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseRouting();
    app.UseHttpMetrics();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapMetrics();
    });
}

This will expose the metrics endpoint on the same port as the rest of the ASP.NET Core application, for example: my.api.com:80/metrics.

What do I need to do to expose the /metrics endpoint on another port? I would like to have my API running on port 80, and the /metrics endpoint on port 9102.

Can't really find any docs about that.

Edit

I'm deploying this into Kubernetes


Solution

  • I ended up doing like this:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        app.UseMetricServer(9102);
    
        app.UseRouting();
        app.UseHttpMetrics();
    
        ...
    
    }
    

    And then for my Kubernetes Deployment I had to add both port 80 and 9102 to containerPorts under ports.

    Additionally I had to set the ASPNETCORE_URLS environment variable to http://+:80;http://+:9102

    That way, /metrics is only exposed on port 9102. (However the rest of my API is exposed on both port 80 and 9102).