#prometheus #timer #raii #macro #monitoring #tracing #prom-timer

macro prom-timer-macro

RAII Prometheus Timer for monitoring & tracing

1 unstable release

0.1.1 Jun 30, 2022

#19 in #raii

MIT license

8KB
66 lines

prom-timer

Rust RAII Prometheus timer

use std::collections::HashMap;
    use std::thread::sleep;
    use std::time::Duration;
    use prometheus::{self, HistogramVec, histogram_opts};
    use prom_timer_macro::timer;
    use lazy_static::lazy_static;

    lazy_static! { static ref TIMER: HistogramVec = HistogramVec::new(
                histogram_opts!("timer", "Timer")
                    .namespace("api")
                    .const_labels(HashMap::new())
                    .buckets(
                        [
                            0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 2.0,
                           3.0, 4.0, 5.0, 10.0,
                        ]
                        .into(),
                    ),
                &["function"],
            )
            .unwrap();}

    #[timer(TIMER.clone(), "f")]
    fn f() {
        sleep(Duration::from_secs(1));
    }

    #[test]
    fn test() {
        f();
    }

lib.rs:

Macros for prom-timer.

Dependencies

~1–6MB
~39K SLoC