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

prometheus-macros

声明 Prometheus 指标的宏

1 个不稳定版本

0.1.0 2023年8月4日

#3 in #declaring

Download history 42/week @ 2024-03-28 131/week @ 2024-04-04 45/week @ 2024-04-11 69/week @ 2024-04-18 166/week @ 2024-04-25 269/week @ 2024-05-02 184/week @ 2024-05-09 62/week @ 2024-05-16 88/week @ 2024-05-23 87/week @ 2024-05-30 52/week @ 2024-06-06 103/week @ 2024-06-13 99/week @ 2024-06-20 59/week @ 2024-06-27 157/week @ 2024-07-04 166/week @ 2024-07-11

506 每月下载量

Apache-2.0

15KB
267

Prometheus 宏

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

prometheus-macros 提供了用于定义 prometheus 指标的宏。

动机

这个包通过引入声明式宏扩展了 prometheus,以最小化指标声明和初始化期间的样板代码。通常需要多个指标,例如在 HTTP 请求的上下文中,需要声明不同的指标来表示请求数和请求延迟。

尽管 prometheus 已经提供了用于初始化单个指标的声明式宏,但在声明多个指标时仍然会产生大量的样板代码。

示例

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

依赖关系

~2.5–8.5MB
~69K SLoC