#prometheus-metrics #metrics #prometheus #macro #declaring

prometheus-macros

Macros for declaring prometheus metrics

1 unstable release

0.1.0 Aug 4, 2023

#4 in #declaring

Download history 16/week @ 2024-01-06 23/week @ 2024-01-13 131/week @ 2024-01-20 65/week @ 2024-01-27 159/week @ 2024-02-03 44/week @ 2024-02-10 153/week @ 2024-02-17 136/week @ 2024-02-24 71/week @ 2024-03-02 119/week @ 2024-03-09 32/week @ 2024-03-16 34/week @ 2024-03-23 73/week @ 2024-03-30 95/week @ 2024-04-06 79/week @ 2024-04-13 60/week @ 2024-04-20

307 downloads per month

Apache-2.0

15KB
267 lines

Prometheus Macros

GitHub Workflow Status (with event) docs.rs Crates.io

prometheus-macros offers advanced macros for defining prometheus metrics.

Motivation

This crate extends prometheus by introducing declarative macros that minimize boilerplate during the declaration and initialization of metrics. Multiple metrics are often needed, as seen for example in contexts like HTTP request where one needs to declare distinct metrics for request count and request latency.

Although prometheus already offers declarative macros for initializing individual metrics, it can still lead to significant boilerplate when declaring multiple metrics.

Example

use prometheus::{IntGauge, HistogramVec};
use prometheus_macros::composite_metric;

composite_metric! {
    struct CompositeMetric {
        #[name = "custom_gauge"]
        #[desc = "Example gauge metric"]
        custom_gauge: IntGauge,
        #[name = "custom_hist_vec"]
        #[desc = "Example histogram vec"]
        #[labels = ["foo", "bar"]]
        #[buckets = [0.01, 0.1, 0.2]]
        custom_hist_vec: HistogramVec,
    }
}

fn main() {
    let metric = CompositeMetric::register(prometheus::default_registry())
        .expect("failed to register metrics to default registry");
    // access the metrics
    metric.custom_gauge().set(420);
    metric.custom_hist_vec().with_label_values(&["a", "b"]).observe(0.5);
}

Dependencies

~2.5–8.5MB
~70K SLoC