File size: 8,189 Bytes
0ca462b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#![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());
    }
}