api_tools/server/axum/handlers/
prometheus.rs

1//! Prometheus metrics handler for Axum
2
3use crate::server::axum::response::ApiError;
4use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle};
5
6/// Buckets for HTTP request duration in seconds
7const SECONDS_DURATION_BUCKETS: &[f64; 11] = &[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0];
8
9/// Prometheus metrics handler for Axum
10pub struct PrometheusHandler {}
11
12impl PrometheusHandler {
13    /// Return a new `PrometheusHandle`
14    pub fn get_handle() -> Result<PrometheusHandle, ApiError> {
15        PrometheusBuilder::new()
16            .set_buckets_for_metric(
17                Matcher::Full("http_requests_duration_seconds".to_string()),
18                SECONDS_DURATION_BUCKETS,
19            )
20            .map_err(|err| ApiError::InternalServerError(err.to_string()))?
21            .install_recorder()
22            .map_err(|err| ApiError::InternalServerError(err.to_string()))
23    }
24}