pd::metrics

Macro gauge

macro_rules! gauge {
    (target: $target:expr, level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => { ... };
    (target: $target:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => { ... };
    (level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => { ... };
    ($name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => { ... };
}
Expand description

Registers a gauge.

Gauges represent a single value that can go up or down over time, and always starts out with an initial value of zero.

Metrics can be registered, which provides a handle to directly update that metric. For gauges, Gauge is provided which can be incremented, decrement, or set to an absolute value.

Metric names are shown below using string literals, but they can also be owned String values, which includes using macros such as format! directly at the callsite. String literals are preferred for performance where possible.

ยงExample

// A basic gauge:
let gauge = gauge!("some_metric_name");
gauge.increment(1.0);

// Specifying labels inline, including using constants for either the key or value:
let gauge = gauge!("some_metric_name", "service" => "http");
gauge.decrement(42.0);

const SERVICE_LABEL: &'static str = "service";
const SERVICE_HTTP: &'static str = "http";
let gauge = gauge!("some_metric_name", SERVICE_LABEL => SERVICE_HTTP);
gauge.increment(3.14);

// We can also pass labels by giving a vector or slice of key/value pairs.  In this scenario,
// a unit or description can still be passed in their respective positions:
let dynamic_val = "woo";
let labels = [("dynamic_key", format!("{}!", dynamic_val))];
let gauge = gauge!("some_metric_name", &labels);
gauge.set(1337.0);

// As mentioned in the documentation, metric names also can be owned strings, including ones
// generated at the callsite via things like `format!`:
let name = String::from("some_owned_metric_name");
let gauge = gauge!(name);

let gauge = gauge!(format!("{}_via_format", "name"));