[][src]Macro xaynet_macros::metrics

metrics!() { /* proc-macro */ }

Allows one or multiple metrics to be sent through a Sender when the metrics feature is enabled.

The idea is to only include the code for sending metrics if the metrics feature flag is enabled during compilation. This can be achieved through conditional compilation, more precisely with the attribute cfg.

See here for more information about conditional compilation in Rust.

This macro helps to reduce the usage of the attribute #[cfg(feature = "metrics")] within the source code.

Macro arguments:

metrics!(sender, metric_1, metric_2, metric_N)

Basic usage:

This example is not tested
fn main() {
    metrics!(sender, metrics::round::total_number::update(1));

    metrics!(
        sender,
        metrics::round::total_number::update(1),
        metrics::masks::total_number::update(1, 1, PhaseName::Idle)
    );
}

Equivalent code not using metrics!

This example is not tested
fn main() {
    #[cfg(feature = "metrics")]
    {
        sender.send(metrics::round::total_number::update(1)),
    };

    #[cfg(feature = "metrics")]
    {
        sender.send(metrics::round::total_number::update(1)),
        sender.send(metrics::masks::total_number::update(1, 1, PhaseName::Idle)),
    };
}

Sender

A Sender must implement the method pub fn send(&self, metrics: T) where T is the type of the metric.

Example of a Sender implementation

This example is not tested
use influxdb::WriteQuery;
use tokio::sync::mpsc::Sender;

pub struct MetricsSender(Sender<WriteQuery>);

impl MetricsSender {
    pub fn send(&mut self, query: WriteQuery) {
        let _ = self.0.try_send(query).map_err(|e| error!("{}", e));
    }
}