use crate::infrastructure::observability::metrics::MetricsRegistry;
use axum::{
body::HttpBody,
extract::{MatchedPath, Request, State},
middleware::Next,
response::Response,
};
use std::{sync::Arc, time::Instant};
pub async fn record(
State(metrics): State<Arc<MetricsRegistry>>,
request: Request,
next: Next,
) -> Response {
let method = request.method().as_str().to_owned();
let endpoint = request
.extensions()
.get::<MatchedPath>()
.map_or("unmatched", MatchedPath::as_str)
.to_owned();
metrics.http_requests_in_flight.inc();
let started = Instant::now();
let response = next.run(request).await;
metrics.http_requests_in_flight.dec();
let status = response.status().as_str().to_owned();
let labels = [method.as_str(), endpoint.as_str(), status.as_str()];
metrics.http_requests_total.with_label_values(&labels).inc();
metrics
.http_request_duration_seconds
.with_label_values(&[method.as_str(), endpoint.as_str()])
.observe(started.elapsed().as_secs_f64());
if let Some(bytes) = response.body().size_hint().exact() {
metrics
.http_response_bytes_total
.with_label_values(&labels)
.inc_by(bytes);
}
response
}