Skip to main content

rs_zero/observability/
rest.rs

1use std::time::Instant;
2
3use axum::{
4    Router,
5    body::Body,
6    extract::{MatchedPath, State},
7    http::Request,
8    middleware::Next,
9    response::IntoResponse,
10    routing::get,
11};
12
13use crate::observability::{CorrelationContext, HttpMetricLabels, MetricsRegistry};
14use tracing::Instrument;
15
16/// Records request metrics for an axum route.
17pub async fn record_metrics_middleware(
18    State(registry): State<MetricsRegistry>,
19    request: Request<Body>,
20    next: Next,
21) -> impl IntoResponse {
22    let method = request.method().to_string();
23    let route = request
24        .extensions()
25        .get::<MatchedPath>()
26        .map(|path| path.as_str().to_string())
27        .unwrap_or_else(|| "unknown".to_string());
28    let correlation = CorrelationContext::from_http_headers(
29        None,
30        method.clone(),
31        Some(&route),
32        request.headers(),
33    );
34    let request_id = correlation.request_id().unwrap_or("");
35    let incoming_traceparent = correlation.traceparent();
36    let started = Instant::now();
37    let span = tracing::info_span!(
38        "rs_zero.http.request",
39        http.method = %method,
40        http.route = %route,
41        service = "unknown",
42        transport = "http",
43        route = %route,
44        method = %method,
45        request_id = %request_id,
46        traceparent = incoming_traceparent.unwrap_or(""),
47        status = tracing::field::Empty,
48        trace_id = tracing::field::Empty,
49        span_id = tracing::field::Empty
50    );
51    registry.increment_http_in_flight();
52    #[cfg(feature = "otlp")]
53    crate::observability::set_span_parent_from_headers(&span, request.headers());
54    let response = next.run(request).instrument(span.clone()).await;
55    registry.decrement_http_in_flight();
56    let status = response.status().as_u16();
57    span.record("status", tracing::field::display(status));
58    let correlation = correlation.with_status(status.to_string());
59    if let Some(trace_id) = correlation.trace_id() {
60        span.record("trace_id", tracing::field::display(trace_id));
61    }
62    if let Some(span_id) = correlation.span_id() {
63        span.record("span_id", tracing::field::display(span_id));
64    }
65    registry.record_http_request(
66        HttpMetricLabels::new(method, route, status),
67        started.elapsed(),
68    );
69    response
70}
71
72/// Creates a router exposing Prometheus metrics at `/metrics`.
73pub fn metrics_router(registry: MetricsRegistry) -> Router {
74    Router::new().route(
75        "/metrics",
76        get(move || {
77            let registry = registry.clone();
78            async move { registry.render_prometheus() }
79        }),
80    )
81}