Skip to main content

gauge

Macro gauge 

Source
macro_rules! gauge {
    ($($input:tt)*) => { ... };
}
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.

A handle to the gauge – Gauge – is returned by this macro and can be held on to in order to amortize the cost of registration.

§Usage

gauge!([named_param: value,] <$name,> [$labels,])

Only a name is required to initialize a gauge.

Named parameters must always come before the gauge name, and the gauge name must come before any labels.

§Required parameters

  • $name - Name of the gauge. Must be a string literal or an expression that results in String or &'static str.

§Named Parameters

The following parameters can be provided in any order relative to other named parameters:

  • target: - Module path of the gauge. Defaults to ::core::module_path!().
  • level: - Verbosity level of the gauge. Defaults to INFO.
  • description: - Description of the gauge. If specified, $name will be used twice.
  • unit: - Unit of measurement of the gauge. Description must be provided in order to specify units.

§Labels

Labels can be passed as one of following:

  • Arbitrary number of <key> => <value> where key and value can be a string literal or an expression that results in String or &'static str.
  • Static reference to collection of Label.
  • Collection/iterator that implements IntoLabels.

§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"));

// Using all of the above, we can customize the gauge's description, unit, target, and level:
let gauge = gauge!(
    description: "super gauge",
    unit: metrics::Unit::Bytes,
    target: ::core::module_path!(),
    level: metrics::Level::INFO,
    "super_gauge",
    "label1" => "value1",
    "label2" => "value2"
);