Search code examples
c#asp.netasp.net-mvcprometheus-net

How to report Prometheus-net metrics in ASP.NET 4.7.1


How can I use prometheus-net in a regular ASP.NET 4.7.1 application ? In .Net Core is quite easy, but I cannot find a nice way of reporting the metrics to Grafana in 4.7.1

The ideal would be to have path /metrics where the metrics are reported.

I tried to create a rough test controller to run the MetricServer but I get an error.

// horrible test code
[RoutePrefix("metrics")]
public class MetricsController : ApiController
{
    static readonly MetricServer _server = new MetricServer(7777);

    static MetricsController()
    {
        _server.Start();
    }

    [Route("")]
    public async Task<IHttpActionResult> Get()
    {
        using (var client = new HttpClient())
        {
            var metrics = await client.GetAsync("http://localhost:7777/metrics");
            return Content(HttpStatusCode.OK, await metrics.Content.ReadAsStringAsync(),null, metrics.Content.Headers.ContentType.ToString());
        }
    }
}

System.Net.HttpListenerException: 'Access is denied'


Solution

  • MetricServer.Start() may throw an access denied exception on Windows if your user does not have the right to open a web server on the specified port. You can use the netsh command to grant yourself the required permissions:

    netsh http add urlacl url=http://+:7777/metrics user=DOMAIN\user