linera_views/
metrics.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::sync::LazyLock;
5
6// Re-export for macros.
7#[doc(hidden)]
8pub use linera_base::prometheus_util::{self, exponential_bucket_latencies};
9use prometheus::IntCounterVec;
10
11/// Increments the metrics counter with the given name, with the struct and base key as labels.
12pub fn increment_counter(counter: &LazyLock<IntCounterVec>, struct_name: &str, base_key: &[u8]) {
13    let base_key = hex::encode(base_key);
14    let labels = [struct_name, &base_key];
15    counter.with_label_values(&labels).inc();
16}
17
18/// The metric tracking the latency of the loading of views.
19#[doc(hidden)]
20pub static LOAD_VIEW_LATENCY: LazyLock<prometheus::HistogramVec> = LazyLock::new(|| {
21    prometheus_util::register_histogram_vec(
22        "load_view_latency",
23        "Load view latency",
24        &[],
25        exponential_bucket_latencies(10.0),
26    )
27});
28
29/// The metric counting how often a view is read from storage.
30#[doc(hidden)]
31pub static LOAD_VIEW_COUNTER: LazyLock<IntCounterVec> = LazyLock::new(|| {
32    prometheus_util::register_int_counter_vec(
33        "load_view",
34        "The metric counting how often a view is read from storage",
35        &["type", "base_key"],
36    )
37});
38/// The metric counting how often a view is written from storage.
39#[doc(hidden)]
40pub static SAVE_VIEW_COUNTER: LazyLock<IntCounterVec> = LazyLock::new(|| {
41    prometheus_util::register_int_counter_vec(
42        "save_view",
43        "The metric counting how often a view is written from storage",
44        &["type", "base_key"],
45    )
46});