Use index_scheduler from configured app_data in middleware

This commit is contained in:
bwbonanno 2023-10-17 08:17:13 -07:00
parent 956cfc5487
commit 2b3adef796
2 changed files with 13 additions and 17 deletions

View File

@ -114,7 +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(middleware::RouteMetricsMiddlewareFactory::new(index_scheduler)); let app = app.wrap(middleware::RouteMetrics);
app.wrap( app.wrap(
Cors::default() Cors::default()
.send_wildcard() .send_wildcard()

View File

@ -3,29 +3,18 @@
use std::future::{ready, Ready}; 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::web::Data; use actix_web::web::Data;
use actix_web::Error;
use futures_util::future::LocalBoxFuture; use futures_util::future::LocalBoxFuture;
use index_scheduler::IndexScheduler; 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 RouteMetricsMiddlewareFactory impl<S, B> Transform<S, ServiceRequest> for RouteMetrics
where where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static, S::Future: 'static,
@ -38,13 +27,12 @@ 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, index_scheduler: self.index_scheduler.clone() })) ready(Ok(RouteMetricsMiddleware { service }))
} }
} }
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>
@ -61,7 +49,15 @@ 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;
if let Ok(()) = self.index_scheduler.features().and_then(|features| features.check_metrics()) { let data = req.app_data::<Data<IndexScheduler>>();
let metrics_enabled = data
.ok_or("Could not get index scheduler")
.and_then(|index_scheduler| {
index_scheduler.features().map_err(|_| "Could not get features")
})
.and_then(|features| features.check_metrics().map_err(|_| "Metrics not enabled"));
if let Ok(()) = metrics_enabled {
let request_path = req.path(); let request_path = req.path();
let is_registered_resource = req.resource_map().has_resource(request_path); let is_registered_resource = req.resource_map().has_resource(request_path);
if is_registered_resource { if is_registered_resource {