Skip to main content

histogram

Macro histogram 

Source
macro_rules! histogram {
    ($($input:tt)*) => { ... };
}
Expand description

Registers a histogram.

Histograms measure the distribution of values for a given set of measurements, and start with no initial values.

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

§Usage

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

Only a name is required to initialize a histogram.

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

§Required parameters

  • $name - Name of the histogram. 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 histogram. Defaults to ::core::module_path!().
  • level: - Verbosity level of the histogram. Defaults to INFO.
  • description: - Description of the histogram. If specified, $name will be used twice.
  • unit: - Unit of measurement of the histogram. 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 histogram:
let histogram = histogram!("some_metric_name");
histogram.record(1.0);

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

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

// 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 histogram = histogram!("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 histogram = histogram!(name);

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

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