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

```rust use prometheus::{IntGauge, HistogramVec}; use prometheusmacros::compositemetric;

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

fn main() { let metric = CompositeMetric::register(prometheus::defaultregistry()) .expect("failed to register metrics to default registry"); // access the metrics metric.customgauge().set(420); metric.customhistvec().withlabelvalues(&["a", "b"]).observe(0.5); } ```