Crate open_metrics_client[][src]

Expand description

Client library implementation of the Open Metrics specification. Allows developers to instrument applications and thus enables operators to monitor said applications with monitoring systems like Prometheus.


// Create a metric registry.
// Note the angle brackets to make sure to use the default (dynamic
// dispatched boxed metric) for the generic type parameter.
let mut registry = <Registry>::default();

// Define a type representing a metric label set, i.e. a key value pair.
// You could as well use `(String, String)` to represent a label set,
// instead of the custom type below.
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
struct Labels {
  // Use your own enum types to represent label values.
  method: Method,
  // Or just a plain string.
  path: String,

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
enum Method {

// Create a sample counter metric family utilizing the above custom label
// type, representing the number of HTTP requests received.
let http_requests = Family::<Labels, Counter>::default();

// Register the metric family with the registry.
  // With the metric name.
  // And the metric help text.
  "Number of HTTP requests received",

// Somewhere in your business logic record a single HTTP GET request.
    &Labels { method: Method::GET, path: "/metrics".to_string() }

// When a monitoring system like Prometheus scrapes the local node, encode
// all metrics in the registry in the text format, and send the encoded
// metrics back.
let mut buffer = vec![];
encode(&mut buffer, &registry).unwrap();

let expected = "# HELP http_requests Number of HTTP requests received.\n".to_owned() +
               "# TYPE http_requests counter\n" +
               "http_requests_total{method=\"GET\",path=\"/metrics\"} 1\n" +
               "# EOF\n";
assert_eq!(expected, String::from_utf8(buffer).unwrap());

See examples directory for more.


Exposition format implementations.

Metric type implementations.

Metric registry implementation.