diff --git a/gitmirror/crates/gitmirror-xrpc/src/metrics.rs b/gitmirror/crates/gitmirror-xrpc/src/metrics.rs index 716655747..f27fa7437 100644 --- a/gitmirror/crates/gitmirror-xrpc/src/metrics.rs +++ b/gitmirror/crates/gitmirror-xrpc/src/metrics.rs @@ -1,7 +1,7 @@ use axum::{ Router, extract::MatchedPath, - http::{Request, StatusCode}, + http::{Method, Request, StatusCode}, middleware::Next, response::{IntoResponse, Response}, routing::get, @@ -124,7 +124,7 @@ pub async fn metrics_handler() -> impl IntoResponse { } struct InFlightGuard { - method: String, + method: &'static str, path: String, } @@ -132,7 +132,7 @@ impl Drop for InFlightGuard { fn drop(&mut self) { gauge!( "gitmirror_xrpc_in_flight", - "method" => self.method.clone(), + "method" => self.method, "path" => self.path.clone() ) .decrement(1); @@ -141,7 +141,7 @@ impl Drop for InFlightGuard { pub async fn metrics_middleware(request: Request, next: Next) -> Response { let start = Instant::now(); - let method = request.method().to_string(); + let method = normalize_method(request.method()); let path = normalize_path(&request); let span = tracing::info_span!( @@ -157,12 +157,12 @@ pub async fn metrics_middleware(request: Request, next: Next) gauge!( "gitmirror_xrpc_in_flight", - "method" => method.clone(), + "method" => method, "path" => path.clone() ) .increment(1); let _in_flight = InFlightGuard { - method: method.clone(), + method, path: path.clone(), }; @@ -177,7 +177,7 @@ pub async fn metrics_middleware(request: Request, next: Next) counter!( "gitmirror_xrpc_requests_total", - "method" => method.clone(), + "method" => method, "path" => path.clone(), "status" => status.clone(), "error" => error @@ -199,6 +199,15 @@ pub async fn metrics_middleware(request: Request, next: Next) response } +fn normalize_method(method: &Method) -> &'static str { + match *method { + Method::GET => "GET", + Method::HEAD => "HEAD", + Method::POST => "POST", + _ => "other", + } +} + fn normalize_path(request: &Request) -> String { request .extensions()