mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 04:17:10 +02:00
Changed prometheus metrics feature as optional
This commit is contained in:
parent
0b6ca73790
commit
2b8f3c26ec
4 changed files with 95 additions and 35 deletions
|
@ -6,6 +6,7 @@ pub mod task;
|
|||
#[macro_use]
|
||||
pub mod extractors;
|
||||
pub mod option;
|
||||
pub mod route_metrics;
|
||||
pub mod routes;
|
||||
|
||||
use std::sync::{atomic::AtomicBool, Arc};
|
||||
|
@ -140,56 +141,33 @@ pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
|
|||
config.service(web::resource("/").route(web::get().to(routes::running)));
|
||||
}
|
||||
|
||||
pub fn configure_metrics_route(config: &mut web::ServiceConfig, enable_metrics_route: bool) {
|
||||
if enable_metrics_route {
|
||||
config.service(web::resource("/metrics").route(web::get().to(routes::get_metrics)));
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! create_app {
|
||||
($data:expr, $auth:expr, $enable_frontend:expr, $opt:expr, $analytics:expr) => {{
|
||||
use actix_cors::Cors;
|
||||
use actix_web::dev::Service;
|
||||
use actix_web::middleware::Condition;
|
||||
use actix_web::middleware::TrailingSlash;
|
||||
use actix_web::App;
|
||||
use actix_web::{middleware, web};
|
||||
use meilisearch_http::error::MeilisearchHttpError;
|
||||
use meilisearch_http::metrics;
|
||||
use meilisearch_http::route_metrics;
|
||||
use meilisearch_http::routes;
|
||||
use meilisearch_http::{configure_data, dashboard};
|
||||
use meilisearch_http::{configure_data, configure_metrics_route, dashboard};
|
||||
use meilisearch_types::error::ResponseError;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use prometheus::{
|
||||
opts, register_histogram_vec, register_int_counter_vec, register_int_gauge,
|
||||
};
|
||||
use prometheus::{HistogramTimer, HistogramVec, IntCounterVec, IntGauge};
|
||||
|
||||
App::new()
|
||||
.configure(|s| configure_data(s, $data.clone(), $auth.clone(), &$opt, $analytics))
|
||||
.configure(routes::configure)
|
||||
.configure(|s| dashboard(s, $enable_frontend))
|
||||
.wrap_fn(|req, srv| {
|
||||
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(
|
||||
metrics::HTTP_RESPONSE_TIME_SECONDS
|
||||
.with_label_values(&[&request_method, request_path])
|
||||
.start_timer(),
|
||||
);
|
||||
metrics::HTTP_REQUESTS_TOTAL
|
||||
.with_label_values(&[&request_method, request_path])
|
||||
.inc();
|
||||
}
|
||||
|
||||
let fut = srv.call(req);
|
||||
|
||||
async {
|
||||
let res = fut.await?;
|
||||
if let Some(histogram_timer) = histogram_timer {
|
||||
histogram_timer.observe_duration();
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
})
|
||||
.configure(|s| configure_metrics_route(s, $opt.enable_metrics_route))
|
||||
.wrap(
|
||||
Cors::default()
|
||||
.send_wildcard()
|
||||
|
@ -203,5 +181,9 @@ macro_rules! create_app {
|
|||
.wrap(middleware::NormalizePath::new(
|
||||
middleware::TrailingSlash::Trim,
|
||||
))
|
||||
.wrap(Condition::new(
|
||||
$opt.enable_metrics_route,
|
||||
route_metrics::RouteMetrics,
|
||||
))
|
||||
}};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue