DEV Community

Cover image for Use Prometheus-net
Antoine
Antoine

Posted on

Use Prometheus-net

Photo by Marius Masalar on Unsplash

In my last post, i have shown how to export performance counters. One option was to use prometheus-net project.

In order to use it, we need

  • publish metrics
  • a Prometheus server
  • a Grafana server

Publish metrics

In order to publish metrics for a .Net Core 2 AspNetCore project, we just have to add prometheus-net.AspNetCore package, and in the Configure method of the Startups.cs class, to add

            app.UseMetricServer();
            app.UseHttpMetrics();
Enter fullscreen mode Exit fullscreen mode

And, this just add an API served on "/metrics" endpoint on the default port.

Prometheus server

In order to get a Prometheus server, we can use Docker

docker run \
    -p 9090:9090 \
    -v /path/to/config:/etc/prometheus \
    prom/prometheus
Enter fullscreen mode Exit fullscreen mode

In order to scrape data from our server, we need to modify the Prometheus configuration to add the published metrics.

global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 15s
alerting:
  alertmanagers:
  - scheme: http
    timeout: 10s
    api_version: v1
    static_configs:
    - targets: []
scrape_configs:
- job_name: prometheus
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - localhost:9090
- job_name: backend
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - localhost:8080
Enter fullscreen mode Exit fullscreen mode

I have discovered this tool to use Service Discovery on Docker-Compose, but not yet used it.

Grafana server

In order to get a Grafana server, we can use Docker

docker run -d -p 3000:3000 grafana/grafana
Enter fullscreen mode Exit fullscreen mode

The default login / password is admin / admin.

We need to add the Prometheus as a data source and then the dashboard, by using the id 10427 in Grafana.

Grafana dashboard

Hope this helps !

Top comments (1)

Collapse
 
pbouillon profile image
Pierre Bouillon

That's a pretty good alternative to AppMetrics too ! I had a hard time trying to use it with ASP.NET Core 3.1, Prometheus saved me !