Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

27.4 KB
Raw Blame
//! Prometheus metrics for the xerj engine.
//!
//! Metrics are registered in a dedicated [`prometheus::Registry`] (not the
//! global default) so that tests can create isolated registries and crates
//! can embed the registry in their own state without racing on global
//! singletons.
//!
//! ## Usage
//!
//! ```no_run
//! use xerj_common::metrics::Metrics;
//!
//! let metrics = Metrics::new().expect("metrics init failed");
//!
//! // Record a document being indexed
//! metrics.docs_indexed.inc();
//! metrics.index_latency.observe(0.003); // 3 ms
//!
//! // Expose via HTTP scrape endpoint:
//! // let body = metrics.gather_text();
//! ```

use prometheus::{
    exponential_buckets, histogram_opts, Histogram, HistogramOpts, HistogramVec, IntCounter,
    IntCounterVec, IntGauge, Opts, Registry,
};

use crate::error::XerjError;

// ═════════════════════════════════════════════════════════════════════════════
// Process-global metrics handle
// ═════════════════════════════════════════════════════════════════════════════

// The registry stays per-instance (see the module docs), but some call sites
// are constructed before the server's `Metrics` exists and without a handle
// to it: the engine's flush/merge pipeline (#804) and the storage layer's
// segment-open path (#819). The server installs the single process-wide
// instance here once at startup; call sites record through `global_metrics()`,
// which is `None` (a no-op) when nothing is installed, so libraries never
// fabricate observations. `xerj_engine::set_engine_metrics` delegates here,
// keeping one install site per process.
static GLOBAL_METRICS: std::sync::OnceLock<std::sync::Arc<Metrics>> = std::sync::OnceLock::new();

/// Install the process-wide metrics handle. Idempotent; the first call wins
/// (later calls are ignored).
pub fn set_global_metrics(metrics: std::sync::Arc<Metrics>) {
    let _ = GLOBAL_METRICS.set(metrics);
}

/// The installed process-wide metrics handle, or `None` if no server has
/// installed one (unit tests, embedded uses).
pub fn global_metrics() -> Option<&'static std::sync::Arc<Metrics>> {
    GLOBAL_METRICS.get()
}

// ═════════════════════════════════════════════════════════════════════════════
// Metrics collection
// ═════════════════════════════════════════════════════════════════════════════

/// All Prometheus metrics for the engine, grouped by type.
///
/// A single `Metrics` instance is created at startup and shared (via `Arc`)
/// across all subsystems.
#[derive(Clone)]
pub struct Metrics {
    /// The Prometheus registry that owns all metrics in this struct.
    pub registry: Registry,

    // ── Counters ─────────────────────────────────────────────────────────────
    /// Total number of documents successfully indexed.
    pub docs_indexed: IntCounter,
    /// Total number of search queries executed.
    pub queries_executed: IntCounter,
    /// Total bytes written to segment files (uncompressed).
    pub bytes_written: IntCounter,
    /// Total on-disk bytes read from segment (.seg) files at open-time
    /// validation (reader-cache misses only).
    pub bytes_read: IntCounter,

    // ── Per-index counters ────────────────────────────────────────────────────
    /// Documents indexed, labelled by index name.
    pub docs_indexed_by_index: IntCounterVec,
    /// Queries executed, labelled by index name.
    pub queries_by_index: IntCounterVec,

    // ── Histograms ────────────────────────────────────────────────────────────
    /// End-to-end query latency in seconds.
    pub query_latency: Histogram,
    /// End-to-end index (ingest) latency in seconds.
    pub index_latency: Histogram,
    /// Time taken to flush an in-memory buffer to a segment in seconds.
    pub flush_duration: Histogram,
    /// Time taken to complete a segment merge in seconds.
    pub merge_duration: Histogram,
    /// WAL write latency in seconds.
    pub wal_write_latency: Histogram,

    // ── Per-operation histograms ──────────────────────────────────────────────
    /// Query latency in seconds, labelled by query type.
    pub query_latency_by_type: HistogramVec,

    // ── Gauges ────────────────────────────────────────────────────────────────
    /// Number of open segments across all indices.
    pub segment_count: IntGauge,
    /// Total number of live (non-deleted) documents across all indices.
    pub doc_count: IntGauge,
    /// Approximate engine memory usage in bytes.
    pub memory_usage: IntGauge,
    /// Number of searches currently in flight.
    pub active_searches: IntGauge,
    /// Size of the WAL on disk in bytes.
    pub wal_size_bytes: IntGauge,

    // ── Query cache (RC4-W4 item 4) ───────────────────────────────────────────
    /// Cumulative internal query-result cache hits across all indices. Exposed
    /// as a gauge because it is reconciled from the engine's own atomic counters
    /// at scrape time (Prometheus `IntCounter` has no set()); the value is
    /// monotonic across a process lifetime, so `hits / (hits + misses)` gives
    /// the cache hit ratio directly.
    pub query_cache_hits: IntGauge,
    /// Cumulative internal query-result cache misses across all indices.
    pub query_cache_misses: IntGauge,

    // ── Rerank stage ──────────────────────────────────────────────────────────
    // Reranking is a search feature the operator pays a third party for, per
    // judged document, on a volume the CALLER chooses. These are the meter.
    /// `rerank` requests by outcome: `applied` (the judge's order was served),
    /// `degraded` (deadline missed, engine order served under a 200),
    /// `refused` (400 / 403 / 503 — nothing was sent to the provider) and
    /// `failed` (502 — the provider was called and the contract broke).
    pub rerank_requests: IntCounterVec,
    /// Documents the provider returned a verdict for. The billing unit.
    pub rerank_documents_judged: IntCounter,
    /// Tokens the provider reported, by `kind` (`input` / `output`). Zero when
    /// the provider omits usage — this is its meter reading, not an estimate.
    pub rerank_provider_tokens: IntCounterVec,
}

impl Metrics {
    /// Create a new `Metrics` instance with its own isolated registry.
    pub fn new() -> Result<Self, XerjError> {
        let registry = Registry::new();
        Self::with_registry(registry)
    }

    /// Create `Metrics` registered against the provided registry.
    ///
    /// Use this when you need to merge xerj metrics into an existing
    /// Prometheus registry (e.g., alongside application-level metrics).
    pub fn with_registry(registry: Registry) -> Result<Self, XerjError> {
        // ── Counters ──────────────────────────────────────────────────────────
        let docs_indexed = IntCounter::with_opts(Opts::new(
            "xerj_docs_indexed_total",
            "Total number of documents successfully indexed",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let queries_executed = IntCounter::with_opts(Opts::new(
            "xerj_queries_executed_total",
            "Total number of search queries executed",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let bytes_written = IntCounter::with_opts(Opts::new(
            "xerj_bytes_written_total",
            "Total on-disk bytes written to segment (.seg) files by flush and merge",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let bytes_read = IntCounter::with_opts(Opts::new(
            "xerj_bytes_read_total",
            "Total on-disk bytes read from segment (.seg) files at open-time validation (reader-cache misses only)",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let docs_indexed_by_index = IntCounterVec::new(
            Opts::new(
                "xerj_docs_indexed_by_index_total",
                "Documents indexed, labelled by index name",
            ),
            &["index"],
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let queries_by_index = IntCounterVec::new(
            Opts::new(
                "xerj_queries_by_index_total",
                "Queries executed, labelled by index name",
            ),
            &["index"],
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        // ── Histograms ────────────────────────────────────────────────────────
        // Latency buckets: 500µs → 30s (15 buckets)
        let latency_buckets =
            exponential_buckets(0.0005, 2.0, 15).expect("latency bucket config is valid");

        let query_latency = Histogram::with_opts(
            HistogramOpts::new("xerj_query_latency_seconds", "End-to-end query latency")
                .buckets(latency_buckets.clone()),
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let index_latency = Histogram::with_opts(
            HistogramOpts::new(
                "xerj_index_latency_seconds",
                "End-to-end document indexing latency",
            )
            .buckets(latency_buckets.clone()),
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        // Flush/merge duration buckets: 10ms → ~5min
        let duration_buckets =
            exponential_buckets(0.01, 2.0, 15).expect("duration bucket config is valid");

        let flush_duration = Histogram::with_opts(
            HistogramOpts::new(
                "xerj_flush_duration_seconds",
                "Time to flush an in-memory buffer to a segment",
            )
            .buckets(duration_buckets.clone()),
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let merge_duration = Histogram::with_opts(
            HistogramOpts::new(
                "xerj_merge_duration_seconds",
                "Time to complete a segment merge operation",
            )
            .buckets(duration_buckets),
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let wal_write_latency = Histogram::with_opts(
            HistogramOpts::new("xerj_wal_write_latency_seconds", "WAL write latency")
                .buckets(latency_buckets),
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let query_latency_by_type = HistogramVec::new(
            histogram_opts!(
                "xerj_query_latency_by_type_seconds",
                "Query latency, labelled by query type",
                exponential_buckets(0.0005, 2.0, 15).expect("valid")
            ),
            &["query_type"],
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        // ── Gauges ────────────────────────────────────────────────────────────
        let segment_count = IntGauge::with_opts(Opts::new(
            "xerj_segment_count",
            "Number of open segments across all indices",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let doc_count = IntGauge::with_opts(Opts::new(
            "xerj_doc_count",
            "Total live documents across all indices",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let memory_usage = IntGauge::with_opts(Opts::new(
            "xerj_memory_usage_bytes",
            "Approximate engine memory usage in bytes",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let active_searches = IntGauge::with_opts(Opts::new(
            "xerj_active_searches",
            "Number of searches currently in flight",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let wal_size_bytes = IntGauge::with_opts(Opts::new(
            "xerj_wal_size_bytes",
            "Current WAL size on disk in bytes",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let query_cache_hits = IntGauge::with_opts(Opts::new(
            "xerj_query_cache_hits",
            "Cumulative internal query-result cache hits across all indices",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let query_cache_misses = IntGauge::with_opts(Opts::new(
            "xerj_query_cache_misses",
            "Cumulative internal query-result cache misses across all indices",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let rerank_requests = IntCounterVec::new(
            Opts::new(
                "xerj_rerank_requests_total",
                "Searches that carried a `rerank` block, by outcome \
                 (applied, degraded, refused, failed)",
            ),
            &["outcome"],
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let rerank_documents_judged = IntCounter::with_opts(Opts::new(
            "xerj_rerank_documents_judged_total",
            "Documents the rerank provider returned a verdict for",
        ))
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        let rerank_provider_tokens = IntCounterVec::new(
            Opts::new(
                "xerj_rerank_provider_tokens_total",
                "Tokens the rerank provider reported, by kind (input, output)",
            ),
            &["kind"],
        )
        .map_err(|e| XerjError::internal(format!("metrics: {e}")))?;

        // ── Register everything ───────────────────────────────────────────────
        macro_rules! reg {
            ($metric:expr) => {
                registry
                    .register(Box::new($metric.clone()))
                    .map_err(|e| XerjError::internal(format!("metrics register: {e}")))?;
            };
        }

        reg!(docs_indexed);
        reg!(queries_executed);
        reg!(bytes_written);
        reg!(bytes_read);
        reg!(docs_indexed_by_index);
        reg!(queries_by_index);
        reg!(query_latency);
        reg!(index_latency);
        reg!(flush_duration);
        reg!(merge_duration);
        reg!(wal_write_latency);
        reg!(query_latency_by_type);
        reg!(segment_count);
        reg!(doc_count);
        reg!(memory_usage);
        reg!(active_searches);
        reg!(wal_size_bytes);
        reg!(query_cache_hits);
        reg!(query_cache_misses);
        reg!(rerank_requests);
        reg!(rerank_documents_judged);
        reg!(rerank_provider_tokens);

        Ok(Self {
            registry,
            docs_indexed,
            queries_executed,
            bytes_written,
            bytes_read,
            docs_indexed_by_index,
            queries_by_index,
            query_latency,
            index_latency,
            flush_duration,
            merge_duration,
            wal_write_latency,
            query_latency_by_type,
            segment_count,
            doc_count,
            memory_usage,
            active_searches,
            wal_size_bytes,
            query_cache_hits,
            query_cache_misses,
            rerank_requests,
            rerank_documents_judged,
            rerank_provider_tokens,
        })
    }

    /// Reconcile the query-cache gauges from the engine's cumulative counters.
    /// Called by the `/v1/metrics` handler at scrape time.
    pub fn set_query_cache(&self, hits: u64, misses: u64) {
        self.query_cache_hits.set(hits as i64);
        self.query_cache_misses.set(misses as i64);
    }

    // ── Scrape helpers ────────────────────────────────────────────────────────

    /// Gather all metrics from the registry as a `MetricFamily` vector.
    pub fn gather(&self) -> Vec<prometheus::proto::MetricFamily> {
        self.registry.gather()
    }

    /// Encode all metrics to the Prometheus text exposition format.
    ///
    /// The returned string can be served directly from `/metrics`.
    pub fn gather_text(&self) -> Result<String, XerjError> {
        use prometheus::Encoder;
        let encoder = prometheus::TextEncoder::new();
        let mut buf = Vec::new();
        encoder
            .encode(&self.gather(), &mut buf)
            .map_err(|e| XerjError::internal(format!("metrics encode: {e}")))?;
        String::from_utf8(buf).map_err(|e| XerjError::internal(format!("metrics utf8: {e}")))
    }

    // ── Ergonomic recording helpers ───────────────────────────────────────────

    /// Record a successful document indexing operation.
    ///
    /// Increments both the global counter and the per-index counter.
    pub fn record_doc_indexed(&self, index: &str) {
        self.docs_indexed.inc();
        self.docs_indexed_by_index.with_label_values(&[index]).inc();
    }

    /// Record `n` documents successfully indexed into `index` in a single
    /// batch. Increments the global and per-index counters with one atomic
    /// `inc_by` each — cheaper on the hot bulk path than calling
    /// [`Self::record_doc_indexed`] per document (which contends the global
    /// counter once per doc). A no-op when `n == 0`.
    pub fn record_docs_indexed(&self, index: &str, n: u64) {
        if n == 0 {
            return;
        }
        self.docs_indexed.inc_by(n);
        self.docs_indexed_by_index
            .with_label_values(&[index])
            .inc_by(n);
    }

    /// Record `n` on-disk bytes written to a segment (.seg) file — called
    /// once per segment finalized by flush or merge with the sealed segment's
    /// `size_bytes`. A no-op when `n == 0`. (#804: the counter was registered
    /// but never incremented, so it read a flat zero regardless of write
    /// volume.)
    pub fn record_bytes_written(&self, n: u64) {
        if n == 0 {
            return;
        }
        self.bytes_written.inc_by(n);
    }

    /// Record `n` on-disk bytes read from a segment (.seg) file, called once
    /// per validated `SegmentReader` open with the opened file's length (the
    /// open path reads and CRC-checks the whole file; the reader-cache hit
    /// path reuses the mmap and reads nothing). A no-op when `n == 0`.
    /// (#819: the counter was registered but never incremented, so it read a
    /// flat zero regardless of read volume.)
    pub fn record_bytes_read(&self, n: u64) {
        if n != 0 {
            self.bytes_read.inc_by(n);
        }
    }

    /// Drop the per-index label series for `index` (RC4-W4 item 5).
    ///
    /// Called when an index is deleted so its `queries_by_index` /
    /// `docs_indexed_by_index` label series don't linger for the lifetime of
    /// the process (unbounded metric cardinality). Removing a label that was
    /// never created is a harmless no-op — the error is ignored.
    pub fn prune_index_labels(&self, index: &str) {
        let _ = self.queries_by_index.remove_label_values(&[index]);
        let _ = self.docs_indexed_by_index.remove_label_values(&[index]);
    }

    /// Record a completed query.
    ///
    /// Increments counters and records latency histograms.
    pub fn record_query(&self, index: &str, query_type: &str, latency_secs: f64) {
        self.queries_executed.inc();
        self.queries_by_index.with_label_values(&[index]).inc();
        self.query_latency.observe(latency_secs);
        self.query_latency_by_type
            .with_label_values(&[query_type])
            .observe(latency_secs);
    }

    /// Record the index-agnostic side of a completed query: the global counter
    /// and both the overall and by-type latency histograms — but NOT
    /// `queries_by_index`. Use this when the caller increments
    /// `queries_by_index` itself, once per *resolved* concrete index (a
    /// multi-index or wildcard search touches several), which is more accurate
    /// than attributing the whole request to a single raw label.
    pub fn record_query_global(&self, query_type: &str, latency_secs: f64) {
        self.queries_executed.inc();
        self.query_latency.observe(latency_secs);
        self.query_latency_by_type
            .with_label_values(&[query_type])
            .observe(latency_secs);
    }

    /// Guard type that decrements `active_searches` when dropped.
    ///
    /// ```no_run
    /// # use xerj_common::metrics::Metrics;
    /// # let metrics = Metrics::new().unwrap();
    /// let _guard = metrics.active_search_guard();
    /// // active_searches is now incremented; decremented when `_guard` is dropped.
    /// ```
    pub fn active_search_guard(&self) -> ActiveSearchGuard {
        self.active_searches.inc();
        ActiveSearchGuard {
            gauge: self.active_searches.clone(),
        }
    }

    // ── Type-erased observe helpers ───────────────────────────────────────────
    //
    // These let crates that do NOT depend on the `prometheus` crate (e.g.
    // `xerj-engine`) record into the duration histograms at their real call
    // sites without ever naming a `prometheus` type. `secs` is the observed
    // duration in seconds (Prometheus convention).

    /// Observe one completed memtable→segment flush, in seconds.
    pub fn observe_flush(&self, secs: f64) {
        self.flush_duration.observe(secs);
    }

    /// Observe one completed segment merge, in seconds.
    pub fn observe_merge(&self, secs: f64) {
        self.merge_duration.observe(secs);
    }

    /// Observe one WAL append (durability write) latency, in seconds.
    pub fn observe_wal_write(&self, secs: f64) {
        self.wal_write_latency.observe(secs);
    }

    /// Observe one end-to-end indexing (ingest) operation latency, in seconds.
    pub fn observe_index(&self, secs: f64) {
        self.index_latency.observe(secs);
    }
}

/// RAII guard for the `active_searches` gauge.
pub struct ActiveSearchGuard {
    gauge: IntGauge,
}

impl Drop for ActiveSearchGuard {
    fn drop(&mut self) {
        self.gauge.dec();
    }
}

// ═════════════════════════════════════════════════════════════════════════════
// Tests
// ═════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn metrics_init_succeeds() {
        Metrics::new().expect("metrics should initialise without error");
    }

    #[test]
    fn counter_increments() {
        let m = Metrics::new().unwrap();
        assert_eq!(m.docs_indexed.get(), 0);
        m.record_doc_indexed("test-index");
        assert_eq!(m.docs_indexed.get(), 1);
        assert_eq!(
            m.docs_indexed_by_index
                .with_label_values(&["test-index"])
                .get(),
            1
        );
    }

    #[test]
    fn active_search_guard_decrements_on_drop() {
        let m = Metrics::new().unwrap();
        assert_eq!(m.active_searches.get(), 0);
        {
            let _g = m.active_search_guard();
            assert_eq!(m.active_searches.get(), 1);
        }
        assert_eq!(m.active_searches.get(), 0);
    }

    #[test]
    fn gather_text_is_valid_utf8() {
        let m = Metrics::new().unwrap();
        m.docs_indexed.inc();
        let text = m.gather_text().unwrap();
        assert!(text.contains("xerj_docs_indexed_total"));
    }

    /// `/metrics` must emit the Prometheus text exposition format exactly:
    /// a `# HELP` line, a `# TYPE` line, then the sample.
    ///
    /// This pins the wire contract independently of which `proto` backend the
    /// `prometheus` crate was compiled with. We build with
    /// `default-features = false`, which drops the optional `protobuf` feature
    /// (and with it protobuf 2.x, GHSA-2gh3-rmm4-6rq5); `proto::MetricFamily`
    /// is then prometheus's pure-Rust `plain_model` rather than generated
    /// protobuf code. The text output is required to be byte-identical either
    /// way, and a scrape breaking silently is exactly the regression that
    /// feature flip could cause.
    #[test]
    fn gather_text_emits_prometheus_exposition_format() {
        let m = Metrics::new().unwrap();
        m.docs_indexed.inc_by(3);
        let text = m.gather_text().unwrap();

        assert!(
            text.contains(
                "# HELP xerj_docs_indexed_total Total number of documents successfully indexed"
            ),
            "missing/incorrect HELP line:\n{text}"
        );
        assert!(
            text.contains("# TYPE xerj_docs_indexed_total counter"),
            "missing/incorrect TYPE line:\n{text}"
        );
        assert!(
            text.lines().any(|l| l == "xerj_docs_indexed_total 3"),
            "missing unlabelled sample line:\n{text}"
        );
    }

    /// Labelled series must survive the same encoder path — label rendering is
    /// the other place a `proto` backend swap could diverge.
    #[test]
    fn gather_text_renders_labelled_series() {
        let m = Metrics::new().unwrap();
        m.record_docs_indexed("my-index", 2);
        let text = m.gather_text().unwrap();

        assert!(
            text.contains("# TYPE xerj_docs_indexed_by_index_total counter"),
            "missing TYPE line for labelled counter:\n{text}"
        );
        assert!(
            text.lines()
                .any(|l| l == "xerj_docs_indexed_by_index_total{index=\"my-index\"} 2"),
            "missing/incorrect labelled sample line:\n{text}"
        );
    }

    #[test]
    fn isolated_registries_do_not_conflict() {
        // Two Metrics instances must not share state
        let m1 = Metrics::new().unwrap();
        let m2 = Metrics::new().unwrap();
        m1.docs_indexed.inc();
        assert_eq!(m1.docs_indexed.get(), 1);
        assert_eq!(m2.docs_indexed.get(), 0);
    }
}