Expand description
Prometheus’ metrics layer
This module provides:
-
PrometheusLayer— a towerLayerthat records per-request HTTP metrics (http_requests_total,http_requests_duration_seconds). The middleware overhead is in the microsecond range. -
spawn_system_metrics_collector— a helper that spawns a background Tokio task to collect host-level metrics (CPU, memory, swap, disk usage). System metrics are intentionally not collected from the request path: they do not change at request granularity, and collecting them inline would add hundreds of milliseconds to every response (seesysinfo::MINIMUM_CPU_UPDATE_INTERVAL).
§Example
ⓘ
use std::path::PathBuf;
use std::time::Duration;
use api_tools::server::axum::layers::prometheus::{
PrometheusLayer, spawn_system_metrics_collector,
};
let layer = PrometheusLayer { service_name: "myapp".into() };
// Once, at application startup:
let _collector = spawn_system_metrics_collector(
"myapp".into(),
vec![PathBuf::from("/")],
Duration::from_secs(10),
);Structs§
- Prometheus
Layer - Prometheus metrics layer for Axum.
- Prometheus
Middleware
Functions§
- spawn_
system_ metrics_ collector - Spawn a background task that periodically refreshes host metrics and publishes them as Prometheus gauges.