Macro prometheus_metrics

Source
prometheus_metrics!() { /* proc-macro */ }
Expand description

Define Prometheus metric families with automatic registration.

This macro generates:

  • Static metric instances with proper types
  • Label structs for metrics with labels
  • Automatic registration with a global registry

§Syntax

use metrics_macro::prometheus_metrics;
use prometheus_client::metrics::counter::Counter;

prometheus_metrics! {
    /// Documentation comment for the metric
    #[labels(label_name = String)]  // Optional labels
    static metric_name: Counter;
}

§Supported Metric Types

  • Counter - Monotonically increasing counter
  • Gauge - Value that can go up and down

§Examples

§Simple Counter

use metrics_macro::prometheus_metrics;
use prometheus_client::metrics::counter::Counter;

prometheus_metrics! {
    /// Total number of requests processed
    static requests_total: Counter;
}

§Labeled Gauge

use metrics_macro::prometheus_metrics;
use prometheus_client::metrics::gauge::Gauge;

prometheus_metrics! {
    /// Current memory usage by component
    #[labels(component = String, unit = String)]
    static memory_usage: Gauge;
}