declare_metric

Macro declare_metric 

Source
macro_rules! declare_metric {
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        CounterRegistry<$key:ty>(
            $name:expr $(,)?
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        PruningCounterRegistry<$key:ty>(
            $name:expr $(,)?
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        PruningGaugeRegistry<$key:ty>(
            $name:expr $(,)?
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        IntGaugeVec(
            $name:expr,
            $labels:expr $(,)*
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        IntCounterVec(
            $name:expr,
            $labels:expr $(,)*
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        CounterVec(
            $name:expr,
            $labels:expr $(,)*
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        HistogramVec(
            $name:expr,
            $labels:expr
            $(, $buckets:expr)?
            $(,)*
        );
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        Histogram($name:expr $(, $buckets:expr)?);
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        IntCounter($name:expr);
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        IntGauge($name:expr);
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        $vis:vis
        static $sym:ident:
        Gauge($name:expr);
    ) => { ... };
}
Expand description

This macro aids in declaring metrics. Usage looks like:

declare_metric! {
/// The number of active outgoing connections in the system,
/// keyed by the service name.
pub static CONN_GAUGE: PruningGaugeRegistry<ServiceKey>("connection_count");
}

This will wrap the declaration of the global into a LazyLock as well as capture metadata about the metric in a way that allows it to be retrieved via the export_metadata() function.

Doc comments are required for every metric declared by this macro.

Different types of metric collector are supported, not just the PruningGaugeRegistry shown above.

PruningGaugeRegistry is not actually a real type, it is some sugar allowed here to enable setting up a Gauge rather than a Counter.