Prometheus Metrics in C# (.NET): Complete Integration Guide
Prometheus is an open-source monitoring system that collects and stores metrics as time series data. For C# (.NET) developers, integrating with Prometheus opens the ability to track application performance, business metrics, and system health in real time. In this article, we will cover how to connect Prometheus to a .NET application, which libraries to use, and how to properly configure metrics.
Why Do You Need Prometheus Metrics in .NET?
Monitoring with Prometheus allows you to:
- Track the number of requests, errors, and response time.
- Collect performance metrics (CPU, memory, GC).
- Create custom business metrics (active users, orders).
- Integrate with Grafana for dashboard visualization.
- Set up alerts based on threshold values.
The prometheus-net Library
The main library for working with Prometheus in .NET is prometheus-net. It supports ASP.NET Core, .NET Core, .NET Framework, and gRPC. Install via NuGet:
dotnet add package prometheus-netFor ASP.NET Core, additionally install:
dotnet add package prometheus-net.AspNetCoreMetric Types in Prometheus
The library supports all standard metric types:
- Counter — a counter that only increases (number of requests).
- Gauge — a value that can increase and decrease (current temperature).
- Histogram — a histogram for measuring value distribution (response time).
- Summary — quantiles for data aggregation (p95, p99).
Example: Basic Counter
Let's create a metric that counts the number of requests to an endpoint:
using Prometheus;
public class MetricsService{ private static readonly Counter RequestCount = Metrics .CreateCounter("app_requests_total", "Total number of requests", new CounterConfiguration { LabelNames = new[] { "method", "endpoint" } });
public void RecordRequest(string method, string endpoint) { RequestCount.WithLabels(method, endpoint).Inc(); }}In an ASP.NET Core controller, the call would look like this:
[ApiController][Route("api/order")]public class OrderController : ControllerBase{ private readonly MetricsService _metrics;
public OrderController(MetricsService metrics) => _metrics = metrics;
[HttpGet] public IActionResult GetOrders() { _metrics.RecordRequest("GET", "/api/order"); return Ok(new { orders = new[] { "order1", "order2" } }); }}Example: Gauge for Active Users
private static readonly Gauge ActiveUsers = Metrics .CreateGauge("app_active_users", "Number of active users");
public void UserLoggedIn() => ActiveUsers.Inc();public void UserLoggedOut() => ActiveUsers.Dec();Example: Histogram for Response Time
private static readonly Histogram RequestDuration = Metrics .CreateHistogram("app_request_duration_seconds", "Request duration in seconds", new HistogramConfiguration { Buckets = Histogram.ExponentialBuckets(0.001, 2, 10) // from 1ms to ~1s });
public void RecordRequestDuration(double seconds){ RequestDuration.Observe(seconds);}Exporting Metrics in ASP.NET Core
To allow Prometheus to collect metrics, you need to add the /metrics endpoint. In Program.cs:
using Prometheus;
var builder = WebApplication.CreateBuilder(args);builder.Services.AddCont