intentforge / src /metrics /mod.rs
Likhith Sai Seemala
Deploy IntentForge v2 to HF Spaces
0ca462b
Raw History Blame Contribute Delete
8.19 kB
#![allow(dead_code)]
//! Prometheus metrics module for IntentForge v2
//!
//! Exposes operational metrics for monitoring crawl throughput, indexing latency,
//! search performance, and system health.
use prometheus::{Counter, Histogram, HistogramOpts, Registry};
/// Prometheus metrics for IntentForge
#[derive(Clone)]
pub struct Metrics {
pub registry: Registry,
// Crawl metrics
pub crawl_requests_total: Counter,
pub crawl_latency_seconds: Histogram,
// Index metrics
pub index_documents_total: Counter,
pub index_latency_seconds: Histogram,
// Search metrics
pub search_requests_total: Counter,
pub search_latency_seconds: Histogram,
pub zero_result_searches_total: Counter,
pub low_quality_searches_total: Counter,
// Inference metrics
pub inference_duration_seconds: Histogram,
// Trending metrics
pub trending_fetch_total: Counter,
pub trending_items_total: Counter,
// Gap filling metrics
pub gap_detected_total: Counter,
pub gap_filled_total: Counter,
pub gap_documents_indexed_total: Counter,
pub gap_processing_seconds: Histogram,
pub gap_quality_score: Histogram,
}
impl Metrics {
/// Create new metrics instance with a fresh registry
pub fn new() -> Self {
let registry = Registry::new();
let crawl_requests_total =
Counter::new("intentforge_crawl_requests_total", "Total crawl requests").unwrap();
let crawl_latency_seconds = Histogram::with_opts(
HistogramOpts::new("intentforge_crawl_latency_seconds", "Crawl latency")
.buckets(vec![0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0]),
)
.unwrap();
let index_documents_total = Counter::new(
"intentforge_index_documents_total",
"Total documents indexed",
)
.unwrap();
let index_latency_seconds = Histogram::with_opts(
HistogramOpts::new("intentforge_index_latency_seconds", "Index latency")
.buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0]),
)
.unwrap();
let search_requests_total =
Counter::new("intentforge_search_requests_total", "Total search requests").unwrap();
let search_latency_seconds = Histogram::with_opts(
HistogramOpts::new("intentforge_search_latency_seconds", "Search latency")
.buckets(vec![0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0]),
)
.unwrap();
let zero_result_searches_total = Counter::new(
"intentforge_zero_result_searches_total",
"Total searches with zero results (triggers background indexing)",
)
.unwrap();
let low_quality_searches_total = Counter::new(
"intentforge_low_quality_searches_total",
"Total searches with few results or low relevance (triggers enrichment)",
)
.unwrap();
let inference_duration_seconds = Histogram::with_opts(
HistogramOpts::new(
"intentforge_inference_duration_seconds",
"ONNX inference duration",
)
.buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0]),
)
.unwrap();
let trending_fetch_total = Counter::new(
"intentforge_trending_fetch_total",
"Total trending fetch operations",
)
.unwrap();
let trending_items_total = Counter::new(
"intentforge_trending_items_total",
"Total trending items discovered",
)
.unwrap();
// Gap filling metrics
let gap_detected_total = Counter::new(
"intentforge_gap_detected_total",
"Total search gaps detected",
)
.unwrap();
let gap_filled_total = Counter::new(
"intentforge_gap_filled_total",
"Total gaps filled successfully",
)
.unwrap();
let gap_documents_indexed_total = Counter::new(
"intentforge_gap_documents_indexed_total",
"Total documents indexed from gap filling",
)
.unwrap();
let gap_processing_seconds = Histogram::with_opts(
HistogramOpts::new("intentforge_gap_processing_seconds", "Gap processing time")
.buckets(vec![1.0, 5.0, 10.0, 30.0, 60.0, 120.0, 300.0]),
)
.unwrap();
let gap_quality_score = Histogram::with_opts(
HistogramOpts::new(
"intentforge_gap_quality_score",
"Quality score of gap-filled documents",
)
.buckets(vec![0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]),
)
.unwrap();
registry
.register(Box::new(crawl_requests_total.clone()))
.unwrap();
registry
.register(Box::new(crawl_latency_seconds.clone()))
.unwrap();
registry
.register(Box::new(index_documents_total.clone()))
.unwrap();
registry
.register(Box::new(index_latency_seconds.clone()))
.unwrap();
registry
.register(Box::new(search_requests_total.clone()))
.unwrap();
registry
.register(Box::new(search_latency_seconds.clone()))
.unwrap();
registry
.register(Box::new(zero_result_searches_total.clone()))
.unwrap();
registry
.register(Box::new(low_quality_searches_total.clone()))
.unwrap();
registry
.register(Box::new(inference_duration_seconds.clone()))
.unwrap();
registry
.register(Box::new(trending_fetch_total.clone()))
.unwrap();
registry
.register(Box::new(trending_items_total.clone()))
.unwrap();
registry
.register(Box::new(gap_detected_total.clone()))
.unwrap();
registry
.register(Box::new(gap_filled_total.clone()))
.unwrap();
registry
.register(Box::new(gap_documents_indexed_total.clone()))
.unwrap();
registry
.register(Box::new(gap_processing_seconds.clone()))
.unwrap();
registry
.register(Box::new(gap_quality_score.clone()))
.unwrap();
Self {
registry,
crawl_requests_total,
crawl_latency_seconds,
index_documents_total,
index_latency_seconds,
search_requests_total,
search_latency_seconds,
zero_result_searches_total,
low_quality_searches_total,
inference_duration_seconds,
trending_fetch_total,
trending_items_total,
gap_detected_total,
gap_filled_total,
gap_documents_indexed_total,
gap_processing_seconds,
gap_quality_score,
}
}
}
impl Default for Metrics {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_metrics_creation() {
let metrics = Metrics::new();
let gathered = metrics.registry.gather();
assert!(!gathered.is_empty(), "Metrics registry should have metrics");
}
#[test]
fn test_crawl_counter() {
let metrics = Metrics::new();
metrics.crawl_requests_total.inc();
metrics.crawl_requests_total.inc_by(5.0);
let gathered = metrics.registry.gather();
let crawl_metric = gathered
.iter()
.find(|m| m.get_name() == "intentforge_crawl_requests_total");
assert!(crawl_metric.is_some());
}
#[test]
fn test_search_latency() {
let metrics = Metrics::new();
metrics.search_latency_seconds.observe(0.025);
metrics.search_latency_seconds.observe(0.05);
let gathered = metrics.registry.gather();
let search_metric = gathered
.iter()
.find(|m| m.get_name() == "intentforge_search_latency_seconds");
assert!(search_metric.is_some());
}
}