2023-02-21 18:18:47 +01:00
|
|
|
//! Contains all the custom middleware used in meilisearch
|
|
|
|
|
2022-08-17 13:44:55 +02:00
|
|
|
use std::future::{ready, Ready};
|
|
|
|
|
2022-10-20 18:00:07 +02:00
|
|
|
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform};
|
2023-10-16 22:48:57 +02:00
|
|
|
use actix_web::web::Data;
|
2023-10-17 17:17:13 +02:00
|
|
|
use actix_web::Error;
|
2022-08-17 13:44:55 +02:00
|
|
|
use futures_util::future::LocalBoxFuture;
|
2023-10-16 22:48:57 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2023-02-21 18:18:47 +01:00
|
|
|
use prometheus::HistogramTimer;
|
2022-08-17 13:44:55 +02:00
|
|
|
|
|
|
|
pub struct RouteMetrics;
|
|
|
|
|
|
|
|
// Middleware factory is `Transform` trait from actix-service crate
|
|
|
|
// `S` - type of the next service
|
|
|
|
// `B` - type of response's body
|
2023-10-17 17:17:13 +02:00
|
|
|
impl<S, B> Transform<S, ServiceRequest> for RouteMetrics
|
2022-08-17 13:44:55 +02:00
|
|
|
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 {
|
2023-10-17 17:17:13 +02:00
|
|
|
ready(Ok(RouteMetricsMiddleware { service }))
|
2022-08-17 13:44:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-10-17 17:17:13 +02:00
|
|
|
|
2023-10-18 17:28:24 +02:00
|
|
|
// calling unwrap here is safe because index scheduler is added to app data while creating actix app.
|
|
|
|
// also, the tests will fail if this is not present.
|
|
|
|
let index_scheduler = req.app_data::<Data<IndexScheduler>>().unwrap();
|
2023-10-23 10:38:56 +02:00
|
|
|
let features = index_scheduler.features();
|
2023-10-18 17:28:24 +02:00
|
|
|
|
|
|
|
if features.check_metrics().is_ok() {
|
2023-10-16 22:48:57 +02:00
|
|
|
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::MEILISEARCH_HTTP_RESPONSE_TIME_SECONDS
|
|
|
|
.with_label_values(&[&request_method, request_path])
|
|
|
|
.start_timer(),
|
|
|
|
);
|
|
|
|
crate::metrics::MEILISEARCH_HTTP_REQUESTS_TOTAL
|
2022-08-17 13:44:55 +02:00
|
|
|
.with_label_values(&[&request_method, request_path])
|
2023-10-16 22:48:57 +02:00
|
|
|
.inc();
|
|
|
|
}
|
|
|
|
};
|
2022-08-17 13:44:55 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|