Skip to main content

Module prometheus

Module prometheus 

Source
Expand description

Prometheus’ metrics layer

This module provides:

  1. PrometheusLayer — a tower Layer that records per-request HTTP metrics (http_requests_total, http_requests_duration_seconds). The middleware overhead is in the microsecond range.

  2. 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 (see sysinfo::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§

PrometheusLayer
Prometheus metrics layer for Axum.
PrometheusMiddleware

Functions§

spawn_system_metrics_collector
Spawn a background task that periodically refreshes host metrics and publishes them as Prometheus gauges.