prometheus-macros 0.2.0

Macros for declaring prometheus metrics
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented2 out of 6 items with examples
  • Size
  • Source code size: 33.41 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.96 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 28s Average build duration of successful builds.
  • all releases: 24s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • heat1q/prometheus-macros
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • heat1q

Prometheus Macros

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);
}