Add runtime check to metrics middleware

This commit is contained in:
bwbonanno 2023-10-16 13:48:57 -07:00
parent 12fc878640
commit 956cfc5487
2 changed files with 31 additions and 18 deletions

View File

@ -114,10 +114,7 @@ pub fn create_app(
.configure(routes::configure)
.configure(|s| dashboard(s, enable_dashboard));
let app = app.wrap(actix_web::middleware::Condition::new(
opt.experimental_enable_metrics,
middleware::RouteMetrics,
));
let app = app.wrap(middleware::RouteMetricsMiddlewareFactory::new(index_scheduler));
app.wrap(
Cors::default()
.send_wildcard()

View File

@ -4,15 +4,28 @@ use std::future::{ready, Ready};
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::Error;
use actix_web::web::Data;
use futures_util::future::LocalBoxFuture;
use index_scheduler::IndexScheduler;
use prometheus::HistogramTimer;
pub struct RouteMetrics;
pub struct RouteMetricsMiddlewareFactory {
index_scheduler: Data<IndexScheduler>,
}
impl RouteMetricsMiddlewareFactory {
pub fn new(index_scheduler: Data<IndexScheduler>) -> Self {
RouteMetricsMiddlewareFactory { index_scheduler }
}
}
// 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
impl<S, B> Transform<S, ServiceRequest> for RouteMetricsMiddlewareFactory
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
@ -25,12 +38,13 @@ where
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(RouteMetricsMiddleware { service }))
ready(Ok(RouteMetricsMiddleware { service, index_scheduler: self.index_scheduler.clone() }))
}
}
pub struct RouteMetricsMiddleware<S> {
service: S,
index_scheduler: Data<IndexScheduler>,
}
impl<S, B> Service<ServiceRequest> for RouteMetricsMiddleware<S>
@ -47,19 +61,21 @@ where
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::MEILISEARCH_HTTP_RESPONSE_TIME_SECONDS
if let Ok(()) = self.index_scheduler.features().and_then(|features| features.check_metrics()) {
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
.with_label_values(&[&request_method, request_path])
.start_timer(),
);
crate::metrics::MEILISEARCH_HTTP_REQUESTS_TOTAL
.with_label_values(&[&request_method, request_path])
.inc();
}
.inc();
}
};
let fut = self.service.call(req);