linera_base/
prometheus_util.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module defines utility functions for interacting with Prometheus (logging metrics, etc)
5
6use prometheus::{
7    exponential_buckets, histogram_opts, linear_buckets, register_histogram,
8    register_histogram_vec, register_int_counter, register_int_counter_vec, register_int_gauge,
9    register_int_gauge_vec, Histogram, HistogramVec, IntCounter, IntCounterVec, IntGauge,
10    IntGaugeVec, Opts,
11};
12
13use crate::time::Instant;
14
15const LINERA_NAMESPACE: &str = "linera";
16
17/// Wrapper around Prometheus `register_int_counter_vec!` macro which also sets the `linera` namespace
18pub fn register_int_counter_vec(
19    name: &str,
20    description: &str,
21    label_names: &[&str],
22) -> IntCounterVec {
23    let counter_opts = Opts::new(name, description).namespace(LINERA_NAMESPACE);
24    register_int_counter_vec!(counter_opts, label_names).expect("IntCounter can be created")
25}
26
27/// Wrapper around Prometheus `register_int_counter_vec!` macro with `linera` namespace and a subsystem.
28/// Results in metrics named `linera_<subsystem>_<name>`.
29pub fn register_int_counter_vec_with_subsystem(
30    subsystem: &str,
31    name: &str,
32    description: &str,
33    label_names: &[&str],
34) -> IntCounterVec {
35    let counter_opts = Opts::new(name, description)
36        .namespace(LINERA_NAMESPACE)
37        .subsystem(subsystem);
38    register_int_counter_vec!(counter_opts, label_names).expect("IntCounter can be created")
39}
40
41/// Wrapper around Prometheus `register_int_counter!` macro which also sets the `linera` namespace
42pub fn register_int_counter(name: &str, description: &str) -> IntCounter {
43    let counter_opts = Opts::new(name, description).namespace(LINERA_NAMESPACE);
44    register_int_counter!(counter_opts).expect("IntCounter can be created")
45}
46
47/// Wrapper around Prometheus `register_histogram_vec!` macro which also sets the `linera` namespace
48pub fn register_histogram_vec(
49    name: &str,
50    description: &str,
51    label_names: &[&str],
52    buckets: Option<Vec<f64>>,
53) -> HistogramVec {
54    let histogram_opts = if let Some(buckets) = buckets {
55        histogram_opts!(name, description, buckets).namespace(LINERA_NAMESPACE)
56    } else {
57        histogram_opts!(name, description).namespace(LINERA_NAMESPACE)
58    };
59
60    register_histogram_vec!(histogram_opts, label_names).expect("Histogram can be created")
61}
62
63/// Wrapper around Prometheus `register_histogram_vec!` macro with `linera` namespace and a subsystem.
64/// Results in metrics named `linera_<subsystem>_<name>`.
65pub fn register_histogram_vec_with_subsystem(
66    subsystem: &str,
67    name: &str,
68    description: &str,
69    label_names: &[&str],
70    buckets: Option<Vec<f64>>,
71) -> HistogramVec {
72    let histogram_opts = if let Some(buckets) = buckets {
73        histogram_opts!(name, description, buckets)
74            .namespace(LINERA_NAMESPACE)
75            .subsystem(subsystem)
76    } else {
77        histogram_opts!(name, description)
78            .namespace(LINERA_NAMESPACE)
79            .subsystem(subsystem)
80    };
81
82    register_histogram_vec!(histogram_opts, label_names).expect("Histogram can be created")
83}
84
85/// Wrapper around Prometheus `register_histogram!` macro which also sets the `linera` namespace
86pub fn register_histogram(name: &str, description: &str, buckets: Option<Vec<f64>>) -> Histogram {
87    let histogram_opts = if let Some(buckets) = buckets {
88        histogram_opts!(name, description, buckets).namespace(LINERA_NAMESPACE)
89    } else {
90        histogram_opts!(name, description).namespace(LINERA_NAMESPACE)
91    };
92
93    register_histogram!(histogram_opts).expect("Histogram can be created")
94}
95
96/// Wrapper around Prometheus `register_histogram!` macro with `linera` namespace and a subsystem.
97/// Results in metrics named `linera_<subsystem>_<name>`.
98pub fn register_histogram_with_subsystem(
99    subsystem: &str,
100    name: &str,
101    description: &str,
102    buckets: Option<Vec<f64>>,
103) -> Histogram {
104    let histogram_opts = if let Some(buckets) = buckets {
105        histogram_opts!(name, description, buckets)
106            .namespace(LINERA_NAMESPACE)
107            .subsystem(subsystem)
108    } else {
109        histogram_opts!(name, description)
110            .namespace(LINERA_NAMESPACE)
111            .subsystem(subsystem)
112    };
113
114    register_histogram!(histogram_opts).expect("Histogram can be created")
115}
116
117/// Wrapper around Prometheus `register_int_gauge!` macro which also sets the `linera` namespace
118pub fn register_int_gauge(name: &str, description: &str) -> IntGauge {
119    let gauge_opts = Opts::new(name, description).namespace(LINERA_NAMESPACE);
120    register_int_gauge!(gauge_opts).expect("IntGauge can be created")
121}
122
123/// Wrapper around Prometheus `register_int_gauge!` macro with `linera` namespace and a subsystem.
124/// Results in metrics named `linera_<subsystem>_<name>`.
125pub fn register_int_gauge_with_subsystem(
126    subsystem: &str,
127    name: &str,
128    description: &str,
129) -> IntGauge {
130    let gauge_opts = Opts::new(name, description)
131        .namespace(LINERA_NAMESPACE)
132        .subsystem(subsystem);
133    register_int_gauge!(gauge_opts).expect("IntGauge can be created")
134}
135
136/// Wrapper around Prometheus `register_int_gauge_vec!` macro which also sets the `linera` namespace
137pub fn register_int_gauge_vec(name: &str, description: &str, label_names: &[&str]) -> IntGaugeVec {
138    let gauge_opts = Opts::new(name, description).namespace(LINERA_NAMESPACE);
139    register_int_gauge_vec!(gauge_opts, label_names).expect("IntGauge can be created")
140}
141
142/// Wrapper around Prometheus `register_int_gauge_vec!` macro with `linera` namespace and a subsystem.
143/// Results in metrics named `linera_<subsystem>_<name>`.
144pub fn register_int_gauge_vec_with_subsystem(
145    subsystem: &str,
146    name: &str,
147    description: &str,
148    label_names: &[&str],
149) -> IntGaugeVec {
150    let gauge_opts = Opts::new(name, description)
151        .namespace(LINERA_NAMESPACE)
152        .subsystem(subsystem);
153    register_int_gauge_vec!(gauge_opts, label_names).expect("IntGauge can be created")
154}
155
156/// Construct the bucket interval exponentially starting from a value and an ending value.
157#[expect(
158    clippy::cast_possible_truncation,
159    clippy::cast_sign_loss,
160    reason = "histogram bucket count; loss of precision is acceptable"
161)]
162pub fn exponential_bucket_interval(start_value: f64, end_value: f64) -> Option<Vec<f64>> {
163    let quot = end_value / start_value;
164    let factor = 3.0_f64;
165    let count_approx = quot.ln() / factor.ln();
166    let count = count_approx.round() as usize;
167    let mut buckets = exponential_buckets(start_value, factor, count)
168        .expect("Exponential buckets creation should not fail!");
169    if let Some(last) = buckets.last() {
170        if *last < end_value {
171            buckets.push(end_value);
172        }
173    }
174    Some(buckets)
175}
176
177/// Construct the latencies exponentially starting from 0.001 and ending at the maximum latency
178pub fn exponential_bucket_latencies(max_latency: f64) -> Option<Vec<f64>> {
179    exponential_bucket_interval(0.001_f64, max_latency)
180}
181
182/// Construct the bucket interval linearly starting from a value and an ending value.
183#[expect(
184    clippy::cast_possible_truncation,
185    clippy::cast_sign_loss,
186    reason = "histogram bucket count; loss of precision is acceptable"
187)]
188pub fn linear_bucket_interval(start_value: f64, width: f64, end_value: f64) -> Option<Vec<f64>> {
189    let count = (end_value - start_value) / width;
190    let count = count.round() as usize;
191    let mut buckets = linear_buckets(start_value, width, count)
192        .expect("Linear buckets creation should not fail!");
193    buckets.push(end_value);
194    Some(buckets)
195}
196
197/// The unit of measurement for latency metrics.
198enum MeasurementUnit {
199    /// Measure latency in milliseconds.
200    Milliseconds,
201    /// Measure latency in microseconds.
202    Microseconds,
203}
204
205/// A guard for an active latency measurement.
206///
207/// Finishes the measurement when dropped, and then updates the `Metric`.
208pub struct ActiveMeasurementGuard<'metric, Metric>
209where
210    Metric: MeasureLatency,
211{
212    start: Instant,
213    metric: Option<&'metric Metric>,
214    unit: MeasurementUnit,
215}
216
217impl<Metric> ActiveMeasurementGuard<'_, Metric>
218where
219    Metric: MeasureLatency,
220{
221    /// Finishes the measurement, updates the `Metric` and returns the measured latency in
222    /// the unit specified when the measurement was started.
223    pub fn finish(mut self) -> f64 {
224        self.finish_by_ref()
225    }
226
227    /// Finishes the measurement without taking ownership of this [`ActiveMeasurementGuard`],
228    /// updates the `Metric` and returns the measured latency in the unit specified when
229    /// the measurement was started.
230    fn finish_by_ref(&mut self) -> f64 {
231        match self.metric.take() {
232            Some(metric) => {
233                let latency = match self.unit {
234                    MeasurementUnit::Milliseconds => self.start.elapsed().as_secs_f64() * 1000.0,
235                    MeasurementUnit::Microseconds => {
236                        self.start.elapsed().as_secs_f64() * 1_000_000.0
237                    }
238                };
239                metric.finish_measurement(latency);
240                latency
241            }
242            None => {
243                // This is getting called from `Drop` after `finish` has already been
244                // executed
245                f64::NAN
246            }
247        }
248    }
249}
250
251impl<Metric> Drop for ActiveMeasurementGuard<'_, Metric>
252where
253    Metric: MeasureLatency,
254{
255    fn drop(&mut self) {
256        self.finish_by_ref();
257    }
258}
259
260/// An extension trait for metrics that can be used to measure latencies.
261pub trait MeasureLatency: Sized {
262    /// Starts measuring the latency in milliseconds, finishing when the returned
263    /// [`ActiveMeasurementGuard`] is dropped.
264    fn measure_latency(&self) -> ActiveMeasurementGuard<'_, Self>;
265
266    /// Starts measuring the latency in microseconds, finishing when the returned
267    /// [`ActiveMeasurementGuard`] is dropped.
268    fn measure_latency_us(&self) -> ActiveMeasurementGuard<'_, Self>;
269
270    /// Updates the metric with measured latency in `milliseconds`.
271    fn finish_measurement(&self, milliseconds: f64);
272}
273
274impl MeasureLatency for HistogramVec {
275    fn measure_latency(&self) -> ActiveMeasurementGuard<'_, Self> {
276        ActiveMeasurementGuard {
277            start: Instant::now(),
278            metric: Some(self),
279            unit: MeasurementUnit::Milliseconds,
280        }
281    }
282
283    fn measure_latency_us(&self) -> ActiveMeasurementGuard<'_, Self> {
284        ActiveMeasurementGuard {
285            start: Instant::now(),
286            metric: Some(self),
287            unit: MeasurementUnit::Microseconds,
288        }
289    }
290
291    fn finish_measurement(&self, milliseconds: f64) {
292        self.with_label_values(&[]).observe(milliseconds);
293    }
294}
295
296impl MeasureLatency for Histogram {
297    fn measure_latency(&self) -> ActiveMeasurementGuard<'_, Self> {
298        ActiveMeasurementGuard {
299            start: Instant::now(),
300            metric: Some(self),
301            unit: MeasurementUnit::Milliseconds,
302        }
303    }
304
305    fn measure_latency_us(&self) -> ActiveMeasurementGuard<'_, Self> {
306        ActiveMeasurementGuard {
307            start: Instant::now(),
308            metric: Some(self),
309            unit: MeasurementUnit::Microseconds,
310        }
311    }
312
313    fn finish_measurement(&self, milliseconds: f64) {
314        self.observe(milliseconds);
315    }
316}
317
318#[cfg(test)]
319mod tests {
320    use super::*;
321
322    // Helper function for approximate floating point comparison
323    fn assert_float_vec_eq(left: &[f64], right: &[f64]) {
324        const EPSILON: f64 = 1e-10;
325
326        assert_eq!(left.len(), right.len(), "Vectors have different lengths");
327        for (i, (l, r)) in left.iter().zip(right.iter()).enumerate() {
328            assert!(
329                (l - r).abs() < EPSILON,
330                "Vectors differ at index {i}: {l} != {r}"
331            );
332        }
333    }
334
335    #[test]
336    fn test_linear_bucket_interval() {
337        // Case 1: Width divides range evenly - small values
338        let buckets = linear_bucket_interval(0.05, 0.01, 0.1).unwrap();
339        assert_float_vec_eq(&buckets, &[0.05, 0.06, 0.07, 0.08, 0.09, 0.1]);
340
341        // Case 2: Width divides range evenly - large values
342        let buckets = linear_bucket_interval(100.0, 50.0, 500.0).unwrap();
343        assert_float_vec_eq(
344            &buckets,
345            &[
346                100.0, 150.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0,
347            ],
348        );
349
350        // Case 3: Width doesn't divide range evenly - small values
351        let buckets = linear_bucket_interval(0.05, 0.12, 0.5).unwrap();
352        assert_float_vec_eq(&buckets, &[0.05, 0.17, 0.29, 0.41, 0.5]);
353
354        // Case 4: Width doesn't divide range evenly - large values
355        let buckets = linear_bucket_interval(100.0, 150.0, 500.0).unwrap();
356        assert_float_vec_eq(&buckets, &[100.0, 250.0, 400.0, 500.0]);
357    }
358}