prometheus_client/
lib.rs

1#![deny(dead_code)]
2#![deny(missing_docs)]
3#![deny(unused)]
4#![forbid(unsafe_code)]
5#![warn(missing_debug_implementations)]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7
8//! Client library implementation of the [Open Metrics
9//! specification](https://github.com/OpenObservability/OpenMetrics). Allows
10//! developers to instrument applications and thus enables operators to monitor
11//! said applications with monitoring systems like
12//! [Prometheus](https://prometheus.io/).
13//!
14//! # Examples
15//!
16//! ```
17//! use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
18//! use prometheus_client::encoding::text::encode;
19//! use prometheus_client::metrics::counter::{Atomic, Counter};
20//! use prometheus_client::metrics::family::Family;
21//! use prometheus_client::registry::Registry;
22//! use std::io::Write;
23//!
24//! // Create a metric registry.
25//! //
26//! // Note the angle brackets to make sure to use the default (dynamic
27//! // dispatched boxed metric) for the generic type parameter.
28//! let mut registry = <Registry>::default();
29//!
30//! // Define a type representing a metric label set, i.e. a key value pair.
31//! //
32//! // You could as well use `(String, String)` to represent a label set,
33//! // instead of the custom type below.
34//! #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
35//! struct Labels {
36//!   // Use your own enum types to represent label values.
37//!   method: Method,
38//!   // Or just a plain string.
39//!   path: String,
40//! };
41//!
42//! #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelValue)]
43//! enum Method {
44//!   GET,
45//!   PUT,
46//! };
47//!
48//! // Create a sample counter metric family utilizing the above custom label
49//! // type, representing the number of HTTP requests received.
50//! let http_requests = Family::<Labels, Counter>::default();
51//!
52//! // Register the metric family with the registry.
53//! registry.register(
54//!   // With the metric name.
55//!   "http_requests",
56//!   // And the metric help text.
57//!   "Number of HTTP requests received",
58//!   http_requests.clone(),
59//! );
60//!
61//! // Somewhere in your business logic record a single HTTP GET request.
62//! http_requests.get_or_create(
63//!     &Labels { method: Method::GET, path: "/metrics".to_string() }
64//! ).inc();
65//!
66//! // When a monitoring system like Prometheus scrapes the local node, encode
67//! // all metrics in the registry in the text format, and send the encoded
68//! // metrics back.
69//! let mut buffer = String::new();
70//! encode(&mut buffer, &registry).unwrap();
71//!
72//! let expected = "# HELP http_requests Number of HTTP requests received.\n".to_owned() +
73//!                "# TYPE http_requests counter\n" +
74//!                "http_requests_total{method=\"GET\",path=\"/metrics\"} 1\n" +
75//!                "# EOF\n";
76//! assert_eq!(expected, buffer);
77//! ```
78//! See [examples] directory for more.
79//!
80//! [examples]: https://github.com/prometheus/client_rust/tree/master/examples
81
82pub mod collector;
83pub mod encoding;
84pub mod metrics;
85pub mod registry;