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.

merge(rc4-w4): Stream B — observability minors (items 3,4,5,6,7)

xerj-org4d515f2674d8
diff --cc engine/crates/xerj-api/src/es_compat.rs
index 5b77a2f42,40b95f1e0..302756510
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@@ -8017,17 -7949,11 +8025,20 @@@ pub async fn search
}
let took_ms = started.elapsed().as_millis() as u64;
- state.metrics.query_latency.observe(took_ms as f64 / 1000.0);
+ // RC4 W4 item 2: record the index-agnostic side of the completed search —
+ // the global `queries_executed` counter plus BOTH latency histograms
+ // (overall + by query type, which were previously never recorded).
+ // `queries_by_index` is deliberately NOT touched here: it is already
+ // incremented once per *resolved* concrete index in the loop above, which
+ // correctly attributes multi-index / wildcard searches (vs. this handler's
+ // single raw target label). Replaces the bare `query_latency.observe`.
+ state
+ .metrics
+ .record_query_global(top_level_query_type(&body.query), took_ms as f64 / 1000.0);
- // v0.8 8-P6: record into the slow query log if over the threshold.
+ // v0.8 8-P6 / RC4-W4 item 3: record into the slow query log if over the
+ // threshold. `maybe_record` also emits the unified slow-query tracing line
+ // (same runtime threshold as the ring buffer), and captures the query body
+ // so the entry is actionable.
state.engine.slow_query.maybe_record(
index.as_str(),
"search",
diff --cc engine/crates/xerj-api/src/native.rs
index 20bf3d9ea,333beede0..c1603b237
--- a/engine/crates/xerj-api/src/native.rs
+++ b/engine/crates/xerj-api/src/native.rs
@@@ -464,11 -463,24 +467,28 @@@ pub async fn search
match idx.search(&search_req).await {
Ok(result) => {
let took_ms = started.elapsed().as_millis() as u64;
- state.metrics.query_latency.observe(took_ms as f64 / 1000.0);
+ // RC4 W4 item 2: one call records the global counter, the per-index
+ // counter, and both latency histograms (overall + by query type).
+ state
+ .metrics
+ .record_query(&name, query_type, took_ms as f64 / 1000.0);
+ // RC4-W4 item 3: the native search path must feed the slow-query
+ // log too (previously ONLY the ES-compat `_search` handler did, so
+ // a slow query via `/v1/indices/:name/search` was invisible to the
+ // operator ring buffer and the tracing line). `maybe_record` emits
+ // both, gated on the shared runtime threshold, and captures the
+ // query body.
+ state.engine.slow_query.maybe_record(
+ &name,
+ "search",
+ started.elapsed(),
+ result.hits.len() as u64,
+ "",
+ &query_body
+ .get("query")
+ .map(|q| q.to_string())
+ .unwrap_or_default(),
+ );
let hits: Vec<Value> = result
.hits
diff --cc engine/crates/xerj-common/src/metrics.rs
index 82fecc764,9698856f7..33ab8c9c0
--- a/engine/crates/xerj-common/src/metrics.rs
+++ b/engine/crates/xerj-common/src/metrics.rs
@@@ -308,21 -341,17 +341,32 @@@ impl Metrics
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);
+ }
+
+ /// 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.