3 releases

new 0.1.2 Feb 11, 2026
0.1.1 Feb 1, 2026
0.1.0 Sep 30, 2024

#1663 in Procedural macros

Download history 25561/week @ 2025-10-23 31920/week @ 2025-10-30 25652/week @ 2025-11-06 21917/week @ 2025-11-13 31643/week @ 2025-11-20 28222/week @ 2025-11-27 40840/week @ 2025-12-04 32784/week @ 2025-12-11 26098/week @ 2025-12-18 15316/week @ 2025-12-25 29006/week @ 2026-01-01 39032/week @ 2026-01-08 35875/week @ 2026-01-15 35364/week @ 2026-01-22 33469/week @ 2026-01-29 26443/week @ 2026-02-05

138,084 downloads per month
Used in rollup-boost

MIT/Apache

39KB
570 lines

metrics-derive

Build Status Crates.io Documentation Rust

metrics-derive provides the Metrics derive macro, allowing you to automatically implement metrics description and initialization.

Originally introduced in paradigmxyz/reth repository in https://github.com/paradigmxyz/reth/pull/592.

Installation

To use metrics-derive, add it to your Cargo.toml:

[dependencies]
metrics-derive = "0.1"

This crate requires a metrics peer dependency.

Usage

use metrics::{Counter, Gauge, Histogram};
use metrics_derive::Metrics;

#[derive(Metrics)]
#[metrics(scope = "metrics_custom")]
pub struct CustomMetrics {
    /// A gauge with doc comment description.
    gauge: Gauge,
    #[metric(rename = "second_gauge", describe = "A gauge with metric attribute description.")]
    gauge2: Gauge,
    /// Some doc comment
    #[metric(describe = "Metric attribute description will be preferred over doc comment.")]
    counter: Counter,
    /// A renamed histogram.
    #[metric(rename = "histogram")]
    histo: Histogram,
}

The example above will be expanded to:

pub struct CustomMetrics {
    /// A gauge with doc comment description.
    gauge: metrics::Gauge,
    gauge2: metrics::Gauge,
    /// Some doc comment
    counter: metrics::Counter,
    /// A renamed histogram.
    histo: metrics::Histogram,
}

impl Default for CustomMetrics {
    fn default() -> Self {
        Self {
            gauge: metrics::gauge!("metrics_custom_gauge"),
            gauge2: metrics::gauge!("metrics_custom_second_gauge"),
            counter: metrics::counter!("metrics_custom_counter"),
            histo: metrics::histogram!("metrics_custom_histogram"),
        }
    }
}

impl CustomMetrics {
    /// Describe all exposed metrics
    pub fn describe() {
        metrics::describe_gauge!(
            "metrics_custom_gauge",
            "A gauge with doc comment description."
        );
        metrics::describe_gauge!(
            "metrics_custom_second_gauge",
            "A gauge with metric attribute description."
        );
        metrics::describe_counter!(
            "metrics_custom_counter",
            "Metric attribute description will be preferred over doc comment."
        );
        metrics::describe_histogram!("metrics_custom_histogram", "A renamed histogram.");
    }
}

impl std::fmt::Debug for CustomMetrics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CustomMetrics").finish()
    }
}

Dependencies

~125–500KB
~12K SLoC