diff --git a/src/middleware/timings.rs b/src/middleware/timings.rs index 4a0f2d5..dd8e714 100644 --- a/src/middleware/timings.rs +++ b/src/middleware/timings.rs @@ -1,4 +1,7 @@ -use actix_web::dev::{Service, ServiceRequest, Transform}; +use actix_web::{ + dev::{Service, ServiceRequest, ServiceResponse, Transform}, + http::StatusCode, +}; use futures_util::future::LocalBoxFuture; use std::{ future::{ready, Ready}, @@ -12,18 +15,21 @@ struct LogOnDrop { begin: Instant, path: String, method: String, + disarm: bool, } impl Drop for LogOnDrop { fn drop(&mut self) { - let duration = self.begin.elapsed(); - metrics::histogram!("relay.request.complete", duration, "path" => self.path.clone(), "method" => self.method.clone()); + if !self.disarm { + let duration = self.begin.elapsed(); + metrics::histogram!("relay.request.complete", duration, "path" => self.path.clone(), "method" => self.method.clone()); + } } } impl Transform for Timings where - S: Service, + S: Service, S::Future: 'static, { type Response = S::Response; @@ -39,7 +45,7 @@ where impl Service for TimingsMiddleware where - S: Service, + S: Service, S::Future: 'static, { type Response = S::Response; @@ -54,16 +60,26 @@ where } fn call(&self, req: ServiceRequest) -> Self::Future { - let logger = LogOnDrop { + let mut logger = LogOnDrop { begin: Instant::now(), path: req.path().to_string(), method: req.method().to_string(), + disarm: false, }; let fut = self.0.call(req); Box::pin(async move { let res = fut.await; + let status = match &res { + Ok(res) => res.status(), + Err(e) => e.as_response_error().status_code(), + }; + if status == StatusCode::NOT_FOUND || status == StatusCode::METHOD_NOT_ALLOWED { + logger.disarm = true; + } + + // TODO: Drop after body write drop(logger); res