2023-02-21 18:18:47 +01:00
|
|
|
use actix_web::http::header;
|
|
|
|
use actix_web::web::{self, Data};
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use index_scheduler::IndexScheduler;
|
2023-02-23 17:13:22 +01:00
|
|
|
use meilisearch_auth::AuthController;
|
2023-02-21 18:18:47 +01:00
|
|
|
use meilisearch_types::error::ResponseError;
|
|
|
|
use meilisearch_types::keys::actions;
|
|
|
|
use prometheus::{Encoder, TextEncoder};
|
|
|
|
|
|
|
|
use crate::extractors::authentication::policies::ActionPolicy;
|
2023-02-23 17:13:22 +01:00
|
|
|
use crate::extractors::authentication::{AuthenticationError, GuardedData};
|
2023-02-21 18:18:47 +01:00
|
|
|
use crate::routes::create_all_stats;
|
|
|
|
|
|
|
|
pub fn configure(config: &mut web::ServiceConfig) {
|
|
|
|
config.service(web::resource("").route(web::get().to(get_metrics)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_metrics(
|
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::METRICS_GET }>, Data<IndexScheduler>>,
|
2023-05-24 10:47:05 +02:00
|
|
|
auth_controller: Data<AuthController>,
|
2023-02-21 18:18:47 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2023-06-22 23:14:01 +02:00
|
|
|
index_scheduler.features()?.check_metrics()?;
|
2023-02-23 17:13:22 +01:00
|
|
|
let auth_filters = index_scheduler.filters();
|
|
|
|
if !auth_filters.all_indexes_authorized() {
|
|
|
|
let mut error = ResponseError::from(AuthenticationError::InvalidToken);
|
2023-02-27 13:42:11 +01:00
|
|
|
error
|
|
|
|
.message
|
|
|
|
.push_str(" The API key for the `/metrics` route must allow access to all indexes.");
|
2023-02-23 17:13:22 +01:00
|
|
|
return Err(error);
|
|
|
|
}
|
|
|
|
|
2023-05-24 10:47:05 +02:00
|
|
|
let response = create_all_stats((*index_scheduler).clone(), auth_controller, auth_filters)?;
|
2023-02-21 18:18:47 +01:00
|
|
|
|
|
|
|
crate::metrics::MEILISEARCH_DB_SIZE_BYTES.set(response.database_size as i64);
|
2023-05-25 18:30:30 +02:00
|
|
|
crate::metrics::MEILISEARCH_USED_DB_SIZE_BYTES.set(response.used_database_size as i64);
|
2023-02-21 18:18:47 +01:00
|
|
|
crate::metrics::MEILISEARCH_INDEX_COUNT.set(response.indexes.len() as i64);
|
|
|
|
|
|
|
|
for (index, value) in response.indexes.iter() {
|
|
|
|
crate::metrics::MEILISEARCH_INDEX_DOCS_COUNT
|
|
|
|
.with_label_values(&[index])
|
|
|
|
.set(value.number_of_documents as i64);
|
|
|
|
}
|
|
|
|
|
2023-05-25 12:58:13 +02:00
|
|
|
for (kind, value) in index_scheduler.get_stats()? {
|
|
|
|
for (value, count) in value {
|
2023-05-25 17:41:53 +02:00
|
|
|
crate::metrics::MEILISEARCH_NB_TASKS
|
|
|
|
.with_label_values(&[&kind, &value])
|
|
|
|
.set(count as i64);
|
2023-05-25 12:58:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 15:21:58 +02:00
|
|
|
if let Some(last_update) = response.last_update {
|
|
|
|
crate::metrics::MEILISEARCH_LAST_UPDATE.set(last_update.unix_timestamp());
|
|
|
|
}
|
|
|
|
crate::metrics::MEILISEARCH_IS_INDEXING.set(index_scheduler.is_task_processing()? as i64);
|
2023-06-23 01:24:25 +02:00
|
|
|
|
2023-02-21 18:18:47 +01:00
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
let mut buffer = vec![];
|
|
|
|
encoder.encode(&prometheus::gather(), &mut buffer).expect("Failed to encode metrics");
|
|
|
|
|
|
|
|
let response = String::from_utf8(buffer).expect("Failed to convert bytes to string");
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().insert_header(header::ContentType(mime::TEXT_PLAIN)).body(response))
|
|
|
|
}
|