#prometheus-metrics #macro-derive #instantiation #boilerplate #generate #write #register

prometheus-metric-storage

Derive macro to instantiate and register prometheus metrics without having to write tons of boilerplate code

6 releases (breaking)

0.5.0 May 10, 2022
0.4.0 Oct 6, 2021
0.3.0 Sep 14, 2021
0.2.0 Sep 8, 2021
0.1.1 Sep 7, 2021

#1600 in Rust patterns

Download history 166/week @ 2024-09-18 242/week @ 2024-09-25 279/week @ 2024-10-02 336/week @ 2024-10-09 292/week @ 2024-10-16 283/week @ 2024-10-23 366/week @ 2024-10-30 222/week @ 2024-11-06 244/week @ 2024-11-13 243/week @ 2024-11-20 331/week @ 2024-11-27 211/week @ 2024-12-04 263/week @ 2024-12-11 267/week @ 2024-12-18 166/week @ 2024-12-25 203/week @ 2025-01-01

924 downloads per month
Used in 4 crates (2 directly)

MIT license

28KB
216 lines

Prometheus metric storage

tests



When instrumenting code with prometheus metrics, one is required to write quite a bit of boilerplate code.

This crate will generate most of said boilerplate for you:

#[derive(prometheus_metric_storage::MetricStorage)]
#[metric(subsystem = "transport", labels("endpoint"))]
struct Metrics {
    /// Number of requests that are currently inflight.
    inflight: prometheus::IntGauge,

    /// Number of finished requests by response code.
    #[metric(labels("status"))]
    requests_finished: prometheus::IntCounterVec,

    /// Number of finished requests by total processing duration.
    #[metric(buckets(0.1, 0.2, 0.5, 1, 2, 4, 8))]
    requests_duration_seconds: prometheus::Histogram,
}

fn main() {
    let metrics = Metrics::new(
        prometheus::default_registry(),
        /* endpoint = */ "0.0.0.0:8080"
    ).unwrap();

    metrics.inflight.inc();
    metrics.requests_finished.with_label_values(&["200"]).inc();
    metrics.requests_duration_seconds.observe(0.015);
}

Dependencies

~2.5–8MB
~70K SLoC