Expose lastUpdate and isIndexing in /stats endpoint

This commit is contained in:
Cong Chen 2023-06-23 07:24:25 +08:00
parent 28404d56b7
commit 6d4981ec25
3 changed files with 37 additions and 0 deletions

View File

@ -794,6 +794,12 @@ impl IndexScheduler {
Ok(res)
}
// Return true if there is at least one task that is processing.
pub fn is_task_processing(&self) -> Result<bool> {
let processing_tasks = self.processing_tasks.read().unwrap().processing.len();
Ok(processing_tasks > 0)
}
/// Return true iff there is at least one task associated with this index
/// that is processing.
pub fn is_index_processing(&self, index: &str) -> Result<bool> {
@ -1808,6 +1814,24 @@ mod tests {
snapshot!(snapshot_index_scheduler(&index_scheduler), name: "registered_the_third_task");
}
#[test]
fn test_task_is_processing() {
let (index_scheduler, mut handle) = IndexScheduler::test(true, vec![]);
let (file0, documents_count0) = sample_documents(&index_scheduler, 0, 0);
file0.persist().unwrap();
let _ = index_scheduler
.register(replace_document_import_task("catto", None, 0, documents_count0))
.unwrap();
snapshot!(snapshot_index_scheduler(&index_scheduler), name: "registered_a_task");
handle.advance_till([Start, BatchCreated, InsideProcessBatch]);
snapshot!(snapshot_index_scheduler(&index_scheduler), name: "initial_task_processing");
assert_eq!(index_scheduler.is_task_processing().unwrap(), true);
}
/// We send a lot of tasks but notify the tasks scheduler only once as
/// we send them very fast, we must make sure that they are all processed.
#[test]

View File

@ -50,4 +50,14 @@ lazy_static! {
&["kind", "value"]
)
.expect("Can't create a metric");
pub static ref MEILISEARCH_LAST_UPDATE: IntGauge = register_int_gauge!(opts!(
"meilisearch_last_update",
"Meilisearch Last Update"
))
.expect("Can't create a metric");
pub static ref MEILISEARCH_IS_INDEXING: IntGauge = register_int_gauge!(opts!(
"meilisearch_is_indexing",
"Meilisearch Is Indexing"
))
.expect("Can't create a metric");
}

View File

@ -48,6 +48,9 @@ pub async fn get_metrics(
}
}
crate::metrics::MEILISEARCH_LAST_UPDATE.set(response.last_update.unwrap().unix_timestamp() as i64);
crate::metrics::MEILISEARCH_IS_INDEXING.set(index_scheduler.is_task_processing().unwrap() as i64);
let encoder = TextEncoder::new();
let mut buffer = vec![];
encoder.encode(&prometheus::gather(), &mut buffer).expect("Failed to encode metrics");