use std::sync::LazyLock;
#[doc(hidden)]
pub use linera_base::prometheus_util::{self, exponential_bucket_latencies};
use prometheus::IntCounterVec;
pub fn increment_counter(counter: &LazyLock<IntCounterVec>, struct_name: &str, base_key: &[u8]) {
let base_key = hex::encode(base_key);
let labels = [struct_name, &base_key];
counter.with_label_values(&labels).inc();
}
linera_base::declare_metrics! {
#[doc(hidden)]
pub static LOAD_VIEW_LATENCY: prometheus::HistogramVec =
prometheus_util::register_histogram_vec(
"load_view_latency",
"Load view latency in milliseconds",
&[],
exponential_bucket_latencies(1000.0),
);
#[doc(hidden)]
pub static LOAD_VIEW_COUNTER: IntCounterVec =
prometheus_util::register_int_counter_vec(
"load_view",
"The metric counting how often a view is read from storage",
&["type", "base_key"],
);
#[doc(hidden)]
pub static SAVE_VIEW_COUNTER: IntCounterVec =
prometheus_util::register_int_counter_vec(
"save_view",
"The metric counting how often a view is written from storage",
&["type", "base_key"],
);
#[doc(hidden)]
pub static SAVE_VIEW_LATENCY: prometheus::HistogramVec =
prometheus_util::register_histogram_vec(
"save_view_latency",
"Save view latency in milliseconds",
&["type"],
exponential_bucket_latencies(1000.0),
);
}
#[cfg(test)]
mod tests {
#[test]
fn label_free_metrics_are_exported_before_their_code_path_runs() {
crate::views::key_value_store_view::metrics::init_metrics();
let families = prometheus::gather();
let contains_keys = families
.iter()
.find(|family| family.get_name() == "linera_key_value_store_view_contains_keys_latency")
.expect("a label-free metric must be exported once init_metrics has run");
let histogram = contains_keys
.get_metric()
.first()
.expect("registering the vector must also create its single label-free child")
.get_histogram();
assert_eq!(histogram.get_sample_count(), 0);
}
#[test]
fn labelled_metrics_stay_absent_until_a_label_combination_is_observed() {
super::init_metrics();
let exported = prometheus::gather().iter().any(|family| {
family.get_name() == "linera_load_view" && !family.get_metric().is_empty()
});
assert!(!exported);
}
}