Skip to main content

autoendpoint/routes/
metrics.rs

1use actix_web::{HttpResponse, web::Data};
2use autopush_common::delivery_metrics::CONTENT_TYPE;
3
4use crate::{
5    error::{ApiErrorKind, ApiResult},
6    server::AppState,
7};
8
9/// Handle the `/metrics` request by returning a Prometheus compatible report.
10pub async fn metrics_route(app_state: Data<AppState>) -> ApiResult<HttpResponse> {
11    #[cfg(feature = "reliable_report")]
12    update_reliability_report(&app_state).await;
13
14    let report = app_state
15        .delivery_metrics
16        .encode()
17        .map_err(|e| ApiErrorKind::General(format!("Metrics report error: {e}")))?;
18    Ok(HttpResponse::Ok().content_type(CONTENT_TYPE).body(report))
19}
20
21/// Refresh the legacy reliability state gauges. Failures are logged rather than
22/// returned so the delivery histograms are still served if Redis is unavailable.
23#[cfg(feature = "reliable_report")]
24async fn update_reliability_report(app_state: &AppState) {
25    let reliability = &app_state.reliability;
26    if let Err(e) = reliability.gc().await {
27        warn!("🔍⚠️ Reliability gc error: {e}");
28    }
29    let Some(family) = &app_state.delivery_metrics.reliability_report else {
30        return;
31    };
32    match reliability.report().await {
33        Ok(values) => autopush_common::reliability::update_report(family, values),
34        Err(e) => {
35            // Serve no reliability gauges rather than the last known values
36            family.clear();
37            warn!("🔍⚠️ Reliability report error: {e}");
38        }
39    }
40}