Crate prometheus_derive

Source
Expand description

§Metrics - Prometheus metrics with automatic registration

This crate provides easy-to-use Prometheus metrics with automatic registration It uses procedural macros to generate type-safe metrics with minimal boilerplate.

§Example

use prometheus_derive::{prometheus_metrics, GLOBAL_REGISTRY};
use prometheus_client::metrics::{counter::Counter, gauge::Gauge};

prometheus_metrics! {
    /// Total number of HTTP requests received
    #[labels(method = String, status = u16)]
    static HTTP_REQUESTS_TOTAL: Counter;

    /// Current number of active connections
    static ACTIVE_CONNECTIONS: Gauge;
}

// Use the metrics - they automatically register when first accessed
HTTP_REQUESTS_TOTAL
    .get_or_create(&HttpRequestsTotalLabels {
        method: "GET".to_string(),
        status: 200,
    })
    .inc();

ACTIVE_CONNECTIONS.set(42);

// Export metrics for Prometheus scraping
use prometheus_client::encoding::text::encode;
let mut buffer = String::new();
let registry = GLOBAL_REGISTRY.read().unwrap();
encode(&mut buffer, &registry).unwrap();
println!("{}", buffer);

Re-exports§

pub use prometheus_client;

Macros§

prometheus_metrics
Define Prometheus metric families with automatic registration.

Statics§

GLOBAL_REGISTRY
Global registry for all Prometheus metrics.

Functions§

register_metric
Register a metric with the global registry.