macro_rules! counter {
($($input:tt)*) => { ... };
}Expand description
Registers a counter.
Counters represent a single monotonic value, which means the value can only be incremented, not decremented, and always starts out with an initial value of zero.
A handle to the counter – Counter – is returned by this macro and can be held on to in order
to amortize the cost of registration.
§Usage
counter!([named_param: value,] <$name,> [$labels,])
Only a name is required to initialize a counter.
Named parameters must always come before the counter name, and the counter name must come before any labels.
§Required parameters
$name- Name of the counter. Must be a string literal or an expression that results inStringor&'static str.
§Named Parameters
The following parameters can be provided in any order relative to other named parameters:
target:- Module path of the counter. Defaults to::core::module_path!().level:- Verbosity level of the counter. Defaults toINFO.description:- Description of the counter. If specified,$namewill be used twice.unit:- Unit of measurement of the counter. Description must be provided in order to specify units.
§Labels
Labels can be passed as one of following:
- Arbitrary number of
<key> => <value>wherekeyandvaluecan be a string literal or an expression that results inStringor&'static str. - Static reference to collection of Label.
- Collection/iterator that implements IntoLabels.
§Example
// A basic counter:
let counter = counter!("some_metric_name");
counter.increment(1);
// Specifying labels inline, including using constants for either the key or value:
let counter = counter!("some_metric_name", "service" => "http");
counter.absolute(42);
const SERVICE_LABEL: &'static str = "service";
const SERVICE_HTTP: &'static str = "http";
let counter = counter!("some_metric_name", SERVICE_LABEL => SERVICE_HTTP);
counter.increment(123);
// 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 counter = counter!("some_metric_name", &labels);
// 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 counter = counter!(name);
let counter = counter!(format!("{}_via_format", "name"));
// Using all of the above, we can customize the counter's description, unit, target, and level:
let counter = counter!(
description: "super counter",
unit: metrics::Unit::Bytes,
target: ::core::module_path!(),
level: metrics::Level::INFO,
"super_counter",
"label1" => "value1",
"label2" => "value2"
);