1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::time::Instant;

use prometheus::{HistogramVec, IntCounterVec};

#[derive(Clone)]
pub struct LibmdbxMetrics {
    read_speed: HistogramVec,
    read_count: IntCounterVec,
}
impl Default for LibmdbxMetrics {
    fn default() -> Self {
        Self::new()
    }
}
impl LibmdbxMetrics {
    pub fn new() -> Self {
        let buckets = prometheus::exponential_buckets(1.0, 2.0, 22).unwrap();

        let read_speed = prometheus::register_histogram_vec!(
            "libmdbx_read_speed_us",
            "the time for a libmdbx read in US",
            &["function_name"],
            buckets.clone()
        )
        .unwrap();

        let read_count = prometheus::register_int_counter_vec!(
            "libmdbx_read_count",
            "amount of reads a function has done",
            &["function_name"]
        )
        .unwrap();

        Self { read_count, read_speed }
    }

    pub fn db_read<R>(self, fn_name: &str, f: impl FnOnce() -> R) -> R {
        self.read_count.with_label_values(&[fn_name]).inc();

        let now = Instant::now();
        let res = f();
        let elasped = now.elapsed().as_micros();

        self.read_speed
            .with_label_values(&[fn_name])
            .observe(elasped as f64);

        res
    }
}