mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-11 07:28:56 +01:00
Add runtime check to metrics middleware
This commit is contained in:
parent
12fc878640
commit
956cfc5487
@ -114,10 +114,7 @@ pub fn create_app(
|
|||||||
.configure(routes::configure)
|
.configure(routes::configure)
|
||||||
.configure(|s| dashboard(s, enable_dashboard));
|
.configure(|s| dashboard(s, enable_dashboard));
|
||||||
|
|
||||||
let app = app.wrap(actix_web::middleware::Condition::new(
|
let app = app.wrap(middleware::RouteMetricsMiddlewareFactory::new(index_scheduler));
|
||||||
opt.experimental_enable_metrics,
|
|
||||||
middleware::RouteMetrics,
|
|
||||||
));
|
|
||||||
app.wrap(
|
app.wrap(
|
||||||
Cors::default()
|
Cors::default()
|
||||||
.send_wildcard()
|
.send_wildcard()
|
||||||
|
@ -4,15 +4,28 @@ use std::future::{ready, Ready};
|
|||||||
|
|
||||||
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform};
|
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
use actix_web::Error;
|
use actix_web::Error;
|
||||||
|
use actix_web::web::Data;
|
||||||
use futures_util::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
use index_scheduler::IndexScheduler;
|
||||||
use prometheus::HistogramTimer;
|
use prometheus::HistogramTimer;
|
||||||
|
|
||||||
pub struct RouteMetrics;
|
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
|
// Middleware factory is `Transform` trait from actix-service crate
|
||||||
// `S` - type of the next service
|
// `S` - type of the next service
|
||||||
// `B` - type of response's body
|
// `B` - type of response's body
|
||||||
impl<S, B> Transform<S, ServiceRequest> for RouteMetrics
|
impl<S, B> Transform<S, ServiceRequest> for RouteMetricsMiddlewareFactory
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
@ -25,12 +38,13 @@ where
|
|||||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
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> {
|
pub struct RouteMetricsMiddleware<S> {
|
||||||
service: S,
|
service: S,
|
||||||
|
index_scheduler: Data<IndexScheduler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, B> Service<ServiceRequest> for RouteMetricsMiddleware<S>
|
impl<S, B> Service<ServiceRequest> for RouteMetricsMiddleware<S>
|
||||||
@ -47,19 +61,21 @@ where
|
|||||||
|
|
||||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let mut histogram_timer: Option<HistogramTimer> = None;
|
let mut histogram_timer: Option<HistogramTimer> = None;
|
||||||
let request_path = req.path();
|
if let Ok(()) = self.index_scheduler.features().and_then(|features| features.check_metrics()) {
|
||||||
let is_registered_resource = req.resource_map().has_resource(request_path);
|
let request_path = req.path();
|
||||||
if is_registered_resource {
|
let is_registered_resource = req.resource_map().has_resource(request_path);
|
||||||
let request_method = req.method().to_string();
|
if is_registered_resource {
|
||||||
histogram_timer = Some(
|
let request_method = req.method().to_string();
|
||||||
crate::metrics::MEILISEARCH_HTTP_RESPONSE_TIME_SECONDS
|
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])
|
.with_label_values(&[&request_method, request_path])
|
||||||
.start_timer(),
|
.inc();
|
||||||
);
|
}
|
||||||
crate::metrics::MEILISEARCH_HTTP_REQUESTS_TOTAL
|
};
|
||||||
.with_label_values(&[&request_method, request_path])
|
|
||||||
.inc();
|
|
||||||
}
|
|
||||||
|
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user