Monitoring Rust Applications: Metrics and Alerts
Rust is a systems programming language that provides high performance and memory safety. However, even the most reliable application requires constant observation in production. Monitoring allows you to detect memory leaks, latency growth, performance degradation, or emergency situations in time. In this article, we will look at how to organize full-fledged monitoring of Rust applications using metrics and alerts.
Why monitor Rust applications?
Rust applications are often used in high-load systems, web servers, databases, network services, and blockchain projects. Without monitoring, you risk:
- Missing the moment when the application starts consuming too much memory
- Not noticing an increase in response time (latency)
- Losing data due to unplanned restarts
- Not learning about performance degradation until users complain
Monitoring gives you transparency: you see what is happening inside the application in real time and can react to problems before they become critical.
Key metrics for Rust applications
For effective monitoring, you need to collect several types of metrics. Let's look at the key categories.
System metrics
These metrics show how the application uses server resources:
- CPU usage — processor load
- Memory usage — amount of memory used (RSS, heap)
- File descriptors — number of open file descriptors
- Thread count — number of active threads
Business metrics
They reflect the application's performance from the user's perspective:
- Request rate — number of requests per second
- Latency — request processing time (p50, p95, p99)
- Error rate — percentage of errors (4xx, 5xx)
- Active connections — number of active connections
Rust-specific metrics
For Rust applications, it is useful to track:
- Panics — number of panics in the application
- Garbage collection — Rust has no GC, but you can track allocations
- Task pool size — size of the async task pool (Tokio)
Tools for monitoring Rust
The most popular stack for monitoring Rust applications is Prometheus (metric collection) + Grafana (visualization) + Alertmanager (alerts). Let's look at the integration using an example.
Libraries for Rust
prometheus— official Prometheus client for Rustmetrics— framework for collecting metrics with support for various exportersopentelemetry— OpenTelemetry for Rust (supports metrics, tracing, logs)actix-web-prom— Prometheus integration with Actix-web
Example: collecting metrics using Prometheus and Actix-web
Let's look at a simple example of a web server on Actix-web that collects metrics and serves them in Prometheus format.
use actix_web::{web, App, HttpServer, HttpResponse, middleware};use prometheus::{Encoder, TextEncoder, Counter, Registry, opts};use std::sync::Arc;
pub struct Metrics { pub registry: Registry, pub request_counter: Counter,}
impl Metrics { pub fn new() -> Self { let registry = Registry::new(); let counter_opts = opts!("http_requests_total", "Total number of HTTP requests"); let request_counter = Counter::with_opts(counter_opts).unwrap(); registry.register(Box::new(request_counter.clone())).unwra