Prometheus metrics in C# (.NET) — prometheus-net library
In modern development of web applications and microservices on the .NET platform, monitoring and observability have become mandatory elements of a production environment. The prometheus-net library is the official client library for .NET, which allows you to easily export application metrics in a format understandable to the Prometheus monitoring server.
With prometheus-net, you can collect performance data (response time, request count, errors, memory usage) and send it to Prometheus for subsequent visualization in Grafana or other tools. The library supports both older versions of .NET Framework (4.6.1+) and modern .NET Core / .NET 5/6/7/8.
Installation
You can install the library via NuGet Package Manager or using .NET CLI:
dotnet add package prometheus-netFor ASP.NET Core applications, the prometheus-net.AspNetCore package is additionally required:
dotnet add package prometheus-net.AspNetCoreMain metric types
Prometheus supports four main metric types. In prometheus-net, they are implemented as static factories in the Metrics class:
- Counter — a counter that can only increase (e.g., number of processed requests).
- Gauge — a metric that can both increase and decrease (e.g., number of active connections or current temperature).
- Histogram — a histogram that counts the number of observations in specified intervals (buckets). Used for measuring the distribution of response time or data size.
- Summary — a summary metric that calculates quantiles (p50, p95, p99) based on a stream of observations.
Counter usage example
Let's create a simple counter to count the number of processed HTTP requests:
using Prometheus;
// Create a counter with a name and descriptionprivate static readonly Counter RequestCount = Metrics .CreateCounter("myapp_requests_total", "Total number of HTTP requests.");
// In the request handler, increment the counterpublic IActionResult Index(){ RequestCount.Inc(); // increment by 1 // Or RequestCount.Inc(5); // increment by 5 return View();}Gauge usage example
Gauge is convenient for tracking current state, for example, the number of active users:
private static readonly Gauge ActiveUsers = Metrics .CreateGauge("myapp_active_users", "Number of active users.");
// When a user logs inActiveUsers.Inc();
// When a user logs outActiveUsers.Dec();Histogram usage example
A histogram is ideal for measuring request execution time:
private static readonly Histogram RequestDuration = Metrics .CreateHistogram("myapp_request_duration_seconds", "Histogram of request duration.", new HistogramConfiguration { // Configure bucket boundaries Buckets = new[] { 0.1, 0.25, 0.5, 1.0, 2.5 } });
// In middleware or controller actionusing (RequestDuration.NewTimer()){ // execute request await Next(context);}Integration with ASP.NET Core
To make your /metrics endpoint start serving data in Prometheus format, simply add middleware in Program.cs:
using Prometheus;
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();
// Add middleware for collecting s