'Use Prometheus in .net core application

I am doing POC using Prometheus in .net core app. I did not found sufficient information on Prometheus website to get started , I have following question if someone can answer that would be helpful

  • a) Do I need to write my own .net core client in order to use prometheus in app?

  • b) What is best approach to use prometheus for metric recording such as should I use in every client or add prometheus logging logic in services method so that metrics is logged for each request and response in pipeline ?

  • c) where to configure prometheus server in .net core app ?



Solution 1:[1]

Monitor .Net core web API using prometheus

Install the below packages

<PackageReference Include="App.Metrics.AspNetCore" Version="3.2.0" />
<PackageReference Include="App.Metrics.AspNetCore.Endpoints" Version="3.2.0" />
<PackageReference Include="App.Metrics.AspNetCore.Tracking" Version="3.2.0" />
<PackageReference Include="App.Metrics.Formatters.Prometheus" Version="3.2.0" />

Add the below code in Program.cs class to support prometheus

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
      .UseMetricsWebTracking()
      .UseMetrics(option =>
      {
          option.EndpointOptions = endpointOptions =>
          {
              endpointOptions.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
              endpointOptions.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
              endpointOptions.EnvironmentInfoEndpointEnabled = false;
          };
      })
      .UseStartup<Startup>();

Make sure these below endpoints are working after making all the above changes

  http://<ip:port>/metrics
  http://<ip:port>/metrics-text

Add a new job in prometheus.yml

  - job_name: 'SampleWebAPi'
      metrics_path: /metrics-text
      static_configs:
      - targets: ['<ip:port>']

Restart prometheus & then check targets page

http://localhost:9090/targets

Github Code

Solution 2:[2]

.net client for prometheus.io now supports.NET Core 2.0:

https://github.com/prometheus-net/prometheus-net

Client is suggested here: https://prometheus.io/docs/instrumenting/clientlibs/

Solution 3:[3]

We are using our own open source library because no other existing solution worked correctly at the time we started using Prometheus.

https://github.com/nexogen-international/Nexogen.Libraries.Metrics

This library can add a lot of metrics out-of-the box. I recommend measuring everything you can as you can never know what will be a bottleneck in production. At least measure everything that can be a key performance indicator, like number of visitors or the time it takes to do some long operation.

The library can be configured in your ASP.NET Core Startup. I recommend injecting your metrics through an interface containing your relevant metrics. If you have a non ASP.NET app, then you can simply set it up in your main class.

Solution 4:[4]

Use the this nuget library if you are using dotnet core. This will give lot of metrics out of the box.

In Configure method of startup.cs. add app.UseHttpMetrics() after app.UseRouting. Futhermore add endpoints.MapMetrics(); as end point which expose end point to scrap prometheus metrics

Sample project here: Repo: https://github.com/CodeSam621/YT_DotNetCorePrometheusGrafana

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

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

https://www.youtube.com/watch?v=cvt1Vrs3ajU

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Narottam Goyal
Solution 2
Solution 3 ahoka
Solution 4 Sam