metriken 0.2.4

A fast and lightweight metrics library
Documentation

Easily registered distributed metrics.

More docs todo...

Creating a Metric

Registering a metric is straightforward. All that's needed is to declare a static within the [metric] macro. By default, the metric will have the name of the path to the static variable you used to declare it but this can be overridden by passing the name parameter to the macro.

# // This should remain in sync with the example below.
use metriken::*;
/// A counter metric named "<crate name>::COUNTER_A"
#[metric]
static COUNTER_A: Counter = Counter::new();

/// A counter metric named "my.metric.name"
#[metric(name = "my.metric.name")]
static COUNTER_B: Counter = Counter::new();
#
# let metrics = metrics();
# // Metrics may be in any arbitrary order
# let mut names: Vec<_> = metrics.iter().map(|metric| metric.name()).collect();
# names.sort();
#
# assert_eq!(names.len(), 2);
# assert_eq!(names[0], "COUNTER_A");
# assert_eq!(names[1], "my.metric.name");

Accessing Metrics

All metrics registered via the [metric] macro can be accessed by calling the [metrics] function. This will return an instance of the [Metric] struct which allows you to access all staticly and dynamically registered metrics.

Suppose we have the metrics declared in the example above.

# // This should remain in sync with the example above.
# use metriken::*;
# /// A counter metric named "<crate name>::COUNTER_A"
# #[metric]
# static COUNTER_A: Counter = Counter::new();
#
# /// A counter metric named "my.metric.name"
# #[metric(name = "my.metric.name")]
# static COUNTER_B: Counter = Counter::new();
#
let metrics = metrics();
// Metrics may be in any arbitrary order
let mut names: Vec<_> = metrics.iter().map(|metric| metric.name()).collect();
names.sort();

assert_eq!(names.len(), 2);
assert_eq!(names[0], "COUNTER_A");
assert_eq!(names[1], "my.metric.name");

How it Works

Behind the scenes, this crate uses the [linkme] crate to create a distributed slice containing a [MetricEntry] instance for each metric that is registered via the [metric] attribute.