WIP: Don't collect path-based metrics for 404, 405

This commit is contained in:
asonix 2022-11-21 11:27:22 -06:00
parent cdaf3b2fa3
commit 8afc16786d

View file

@ -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 futures_util::future::LocalBoxFuture;
use std::{ use std::{
future::{ready, Ready}, future::{ready, Ready},
@ -12,18 +15,21 @@ struct LogOnDrop {
begin: Instant, begin: Instant,
path: String, path: String,
method: String, method: String,
disarm: bool,
} }
impl Drop for LogOnDrop { impl Drop for LogOnDrop {
fn drop(&mut self) { fn drop(&mut self) {
let duration = self.begin.elapsed(); if !self.disarm {
metrics::histogram!("relay.request.complete", duration, "path" => self.path.clone(), "method" => self.method.clone()); let duration = self.begin.elapsed();
metrics::histogram!("relay.request.complete", duration, "path" => self.path.clone(), "method" => self.method.clone());
}
} }
} }
impl<S> Transform<S, ServiceRequest> for Timings impl<S> Transform<S, ServiceRequest> for Timings
where where
S: Service<ServiceRequest>, S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
S::Future: 'static, S::Future: 'static,
{ {
type Response = S::Response; type Response = S::Response;
@ -39,7 +45,7 @@ where
impl<S> Service<ServiceRequest> for TimingsMiddleware<S> impl<S> Service<ServiceRequest> for TimingsMiddleware<S>
where where
S: Service<ServiceRequest>, S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
S::Future: 'static, S::Future: 'static,
{ {
type Response = S::Response; type Response = S::Response;
@ -54,16 +60,26 @@ where
} }
fn call(&self, req: ServiceRequest) -> Self::Future { fn call(&self, req: ServiceRequest) -> Self::Future {
let logger = LogOnDrop { let mut logger = LogOnDrop {
begin: Instant::now(), begin: Instant::now(),
path: req.path().to_string(), path: req.path().to_string(),
method: req.method().to_string(), method: req.method().to_string(),
disarm: false,
}; };
let fut = self.0.call(req); let fut = self.0.call(req);
Box::pin(async move { Box::pin(async move {
let res = fut.await; 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); drop(logger);
res res