Expand description
Easily registered distributed metrics.
§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 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();If you want to create and remove metrics dynamically at runtime check out
the dynmetrics module.
§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], "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.
Re-exports§
pub use crate::histogram::AtomicHistogram;pub use crate::histogram::RwLockHistogram;
Modules§
- dynmetrics
- Methods and structs for working with dynamically created and destroyed metrics.
- histogram
Structs§
- Counter
- A counter. Can be incremented or added to.
- DynBoxed
Metric - A dynamic metric that stores the metric instance on the heap.
- DynMetrics
Iter - An iterator over all dynamically registered metrics.
- DynPinned
Metric - A dynamic metric that stores the metric inline.
- Gauge
- A gauge. Indicates the current value of some host parameter.
- Lazy
- A value which is initialized on the first access.
- Metadata
- Metadata for a metric.
- Metadata
Iter - An iterator over the entries of a
Metadata. - Metric
Builder - Builder for creating a dynamic metric.
- Metric
Entry - A statically declared metric entry.
- Metrics
- Provides access to all registered metrics both static and dynamic.
- Metrics
Iter - An iterator over all registered metrics.
Enums§
Traits§
- Metric
- Global interface to a metric.
Functions§
- default_
formatter - The default formatter supports Prometheus-style exposition, and otherwise simply prints the metric name.
- metrics
- The list of all metrics registered via the either
#[metric]attribute or by using the types within thedynmetricsmodule.
Type Aliases§
- Lazy
Counter - A counter holds a unsigned 64bit monotonically non-decreasing value. The counter behavior is to wrap on overflow.
- Lazy
Gauge - A gauge holds a signed 64-bit value and is used to represent metrics which may increase or decrease in value. The behavior is to wrap around on overflow and underflow.
Attribute Macros§
- metric
- Declare a global metric that can be accessed via the
metricsmethod.