2023-02-21 18:18:47 +01:00
|
|
|
//! Contains all the custom middleware used in meilisearch
|
|
|
|
|
2022-08-17 17:14:55 +05:30
|
|
|
use std::future::{ready, Ready};
|
|
|
|
|
2022-10-20 18:00:07 +02:00
|
|
|
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform};
|
2023-02-21 18:18:47 +01:00
|
|
|
use actix_web::Error;
|
2022-08-17 17:14:55 +05:30
|
|
|
use futures_util::future::LocalBoxFuture;
|
2023-02-21 18:18:47 +01:00
|
|
|
use prometheus::HistogramTimer;
|
2022-08-17 17:14:55 +05:30
|
|
|
|
|
|
|
pub struct RouteMetrics;
|
|
|
|
|
|
|
|
// Middleware factory is `Transform` trait from actix-service crate
|
|
|
|
// `S` - type of the next service
|
|
|
|
// `B` - type of response's body
|
|
|
|
impl<S, B> Transform<S, ServiceRequest> for RouteMetrics
|
|
|
|
where
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = RouteMetricsMiddleware<S>;
|
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
ready(Ok(RouteMetricsMiddleware { service }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RouteMetricsMiddleware<S> {
|
|
|
|
service: S,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service<ServiceRequest> for RouteMetricsMiddleware<S>
|
|
|
|
where
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
|
|
|
|
|
|
|
dev::forward_ready!(service);
|
|
|
|
|
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
|
|
|
let mut histogram_timer: Option<HistogramTimer> = None;
|
|
|
|
let request_path = req.path();
|
|
|
|
let is_registered_resource = req.resource_map().has_resource(request_path);
|
|
|
|
if is_registered_resource {
|
|
|
|
let request_method = req.method().to_string();
|
|
|
|
histogram_timer = Some(
|
|
|
|
crate::metrics::HTTP_RESPONSE_TIME_SECONDS
|
|
|
|
.with_label_values(&[&request_method, request_path])
|
|
|
|
.start_timer(),
|
|
|
|
);
|
|
|
|
crate::metrics::HTTP_REQUESTS_TOTAL
|
|
|
|
.with_label_values(&[&request_method, request_path])
|
|
|
|
.inc();
|
|
|
|
}
|
|
|
|
|
|
|
|
let fut = self.service.call(req);
|
|
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
let res = fut.await?;
|
|
|
|
|
|
|
|
if let Some(histogram_timer) = histogram_timer {
|
|
|
|
histogram_timer.observe_duration();
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|