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 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<S> Transform<S, ServiceRequest> for Timings
where
S: Service<ServiceRequest>,
S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
S::Future: 'static,
{
type Response = S::Response;
@ -39,7 +45,7 @@ where
impl<S> Service<ServiceRequest> for TimingsMiddleware<S>
where
S: Service<ServiceRequest>,
S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
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