Fix metrics feature

Metrics feature was relying on old references. Refactored with inspiration from the `get_stats` method in `meilisearch/src/routes/lib.rs`. `enable_metrics_routes` added to options in `segment_analytics`.

Resolves: #3469
See also: #2763
This commit is contained in:
James 2023-02-17 20:11:57 +00:00
parent 62358bd31c
commit 91048d209d
3 changed files with 12 additions and 5 deletions

View File

@ -249,6 +249,7 @@ impl From<Opt> for Infos {
// to add analytics when we add a field in the Opt. // to add analytics when we add a field in the Opt.
// Thus we must not insert `..` at the end. // Thus we must not insert `..` at the end.
let Opt { let Opt {
enable_metrics_route: _,
db_path, db_path,
http_addr, http_addr,
master_key: _, master_key: _,

View File

@ -92,7 +92,8 @@ pub fn create_app(
let app = app.configure(|s| configure_metrics_route(s, opt.enable_metrics_route)); let app = app.configure(|s| configure_metrics_route(s, opt.enable_metrics_route));
#[cfg(feature = "metrics")] #[cfg(feature = "metrics")]
let app = app.wrap(Condition::new(opt.enable_metrics_route, route_metrics::RouteMetrics)); let app =
app.wrap(middleware::Condition::new(opt.enable_metrics_route, route_metrics::RouteMetrics));
app.wrap( app.wrap(
Cors::default() Cors::default()
.send_wildcard() .send_wildcard()

View File

@ -2,21 +2,26 @@ 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::http::header; use actix_web::http::header;
use actix_web::web::Data;
use actix_web::{Error, HttpResponse}; use actix_web::{Error, HttpResponse};
use futures_util::future::LocalBoxFuture; use futures_util::future::LocalBoxFuture;
use meilisearch_lib::MeiliSearch; use index_scheduler::IndexScheduler;
use meilisearch_auth::AuthController;
use meilisearch_types::error::ResponseError; use meilisearch_types::error::ResponseError;
use meilisearch_types::keys::actions; use meilisearch_types::keys::actions;
use prometheus::{Encoder, HistogramTimer, TextEncoder}; use prometheus::{Encoder, HistogramTimer, TextEncoder};
use crate::extractors::authentication::policies::ActionPolicy; use crate::extractors::authentication::policies::ActionPolicy;
use crate::extractors::authentication::GuardedData; use crate::extractors::authentication::GuardedData;
use crate::routes::create_all_stats;
pub async fn get_metrics( pub async fn get_metrics(
meilisearch: GuardedData<ActionPolicy<{ actions::METRICS_GET }>, MeiliSearch>, index_scheduler: GuardedData<ActionPolicy<{ actions::METRICS_GET }>, Data<IndexScheduler>>,
auth_controller: GuardedData<ActionPolicy<{ actions::METRICS_GET }>, AuthController>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let search_rules = &meilisearch.filters().search_rules; let search_rules = &index_scheduler.filters().search_rules;
let response = meilisearch.get_all_stats(search_rules).await?; let response =
create_all_stats((*index_scheduler).clone(), (*auth_controller).clone(), search_rules)?;
crate::metrics::MEILISEARCH_DB_SIZE_BYTES.set(response.database_size as i64); crate::metrics::MEILISEARCH_DB_SIZE_BYTES.set(response.database_size as i64);
crate::metrics::MEILISEARCH_INDEX_COUNT.set(response.indexes.len() as i64); crate::metrics::MEILISEARCH_INDEX_COUNT.set(response.indexes.len() as i64);