use std::sync::OnceLock;
use prometheus::{IntCounterVec, register_int_counter_vec};
const CATALOG_LABELS: [(&str, &str); 8] = [
("visible", "persona"),
("visible", "conversation_grant_web_session"),
("visible", "conversation_grant_persona"),
("visible", "admin_fleet"),
("fleet", "persona"),
("fleet", "conversation_grant_web_session"),
("fleet", "conversation_grant_persona"),
("fleet", "admin_fleet"),
];
fn catalog_described_total() -> &'static IntCounterVec {
static V: OnceLock<IntCounterVec> = OnceLock::new();
V.get_or_init(|| {
register_int_counter_vec!(
"polychrome_query_catalog_described_total",
"Count of DescribeCatalog lookups answered, by realm and principal kind.",
&["realm", "principal_kind"]
)
.expect("register polychrome_query_catalog_described_total")
})
}
pub(crate) fn record_catalog_described(realm: &str, principal_kind: &str) {
catalog_described_total()
.with_label_values(&[realm, principal_kind])
.inc();
}
pub(crate) const REFUSAL_REASONS: [&str; 11] = [
"statement_kind",
"statement_parse",
"function_surface",
"expression_nodes",
"expression_depth",
"function_calls",
"select_items",
"query_nodes",
"expression_output_references",
"column_fanout",
"statement_column_references",
];
fn statement_refusals_total() -> &'static IntCounterVec {
static V: OnceLock<IntCounterVec> = OnceLock::new();
V.get_or_init(|| {
register_int_counter_vec!(
"polychrome_query_statement_refusals_total",
"Count of statements the query gate refused, by realm, principal kind, and reason.",
&["realm", "principal_kind", "reason"]
)
.expect("register polychrome_query_statement_refusals_total")
})
}
pub(crate) fn record_statement_refusal(realm: &str, principal_kind: &str, reason: &str) {
statement_refusals_total()
.with_label_values(&[realm, principal_kind, reason])
.inc();
}
pub(crate) fn force() {
for pair in CATALOG_LABELS {
catalog_described_total().with_label_values(&<[&str; 2]>::from(pair));
for reason in REFUSAL_REASONS {
statement_refusals_total().with_label_values(&[pair.0, pair.1, reason]);
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::pedantic, clippy::nursery, missing_docs)]
use prometheus::{Encoder as _, TextEncoder};
use super::record_catalog_described;
fn scrape() -> String {
let mut buf = Vec::new();
TextEncoder::new()
.encode(&prometheus::default_registry().gather(), &mut buf)
.expect("encode");
String::from_utf8(buf).expect("utf8")
}
#[test]
fn record_catalog_described_increments_by_exactly_one() {
let metric = "polychrome_query_catalog_described_total";
let needle = |text: &str, realm: &str, kind: &str| {
let pattern = format!("{metric}{{principal_kind=\"{kind}\",realm=\"{realm}\"}} ");
text.lines()
.find(|line| line.starts_with(&pattern))
.and_then(|line| line.rsplit(' ').next())
.and_then(|v| v.parse::<f64>().ok())
.unwrap_or(0.0)
};
let before = needle(&scrape(), "visible", "persona");
record_catalog_described("visible", "persona");
let after = needle(&scrape(), "visible", "persona");
assert_eq!(after - before, 1.0, "{metric} must increment by exactly 1");
assert_eq!(
needle(&scrape(), "fleet", "admin_fleet"),
0.0,
"an unrelated label pair is untouched"
);
}
}