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

prom-timer-macro

RAII Prometheus 定时器用于监控和跟踪

1 个不稳定版本

0.1.1 2022年6月30日

#20 in #raii

MIT 许可证

8KB
66

prom-timer

Rust RAII Prometheus 定时器

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:

prom-timer 提供宏。

依赖项

~1.2–7.5MB
~42K SLoC