#metrics #twitter #instance #registered #counter #lib #name

twitter/rustcommon-metrics

Common Twitter Rust lib

2 releases (1 stable)

2.0.0-alpha.0 Sep 11, 2020
1.0.0 Jun 29, 2020

#3 in #registered

341 stars & 11 watchers

Apache-2.0

39KB
759 lines

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.

use rustcommon_metrics::*;
/// 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();
#
#

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.

#
#
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], "my.metric.name");
assert_eq!(names[1], concat!(module_path!(), "::", "COUNTER_A"));

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.

Dependencies

~0.7–5.5MB
~28K SLoC