prometheus/lib.rs
1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3/*!
4The Rust client library for [Prometheus](https://prometheus.io/).
5
6Use of this library involves a few core concepts:
7
8* [`Metric`s](core/trait.Metric.html) like [`Counter`s](type.Counter.html) that
9 represent information about your system.
10
11* A [`Registry`](struct.Registry.html) that [`Metric`s](core/trait.Metric.html)
12 are registered with.
13
14* An endpoint that calls [`gather`](fn.gather.html) which returns
15 [`MetricFamily`s](proto/struct.MetricFamily.html) through an
16 [`Encoder`](trait.Encoder.html).
17
18
19# Basic Example
20
21```rust
22use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder};
23
24// Create a Counter.
25let counter_opts = Opts::new("test_counter", "test counter help");
26let counter = Counter::with_opts(counter_opts).unwrap();
27
28// Create a Registry and register Counter.
29let r = Registry::new();
30r.register(Box::new(counter.clone())).unwrap();
31
32// Inc.
33counter.inc();
34
35// Gather the metrics.
36let mut buffer = vec![];
37let encoder = TextEncoder::new();
38let metric_families = r.gather();
39encoder.encode(&metric_families, &mut buffer).unwrap();
40
41// Output to the standard output.
42println!("{}", String::from_utf8(buffer).unwrap());
43```
44
45You can find more examples within
46[`/examples`](https://github.com/tikv/rust-prometheus/tree/master/examples).
47
48
49# Static Metrics
50
51This crate supports staticly built metrics. You can use it with
52[`lazy_static`](https://docs.rs/lazy_static/) to quickly build up and collect
53some metrics.
54
55```rust
56use prometheus::{self, IntCounter, TextEncoder, Encoder};
57
58use lazy_static::lazy_static;
59use prometheus::register_int_counter;
60
61lazy_static! {
62 static ref HIGH_FIVE_COUNTER: IntCounter =
63 register_int_counter!("highfives", "Number of high fives received").unwrap();
64}
65
66HIGH_FIVE_COUNTER.inc();
67assert_eq!(HIGH_FIVE_COUNTER.get(), 1);
68```
69
70By default, this registers with a default registry. To make a report, you can call
71[`gather`](fn.gather.html). This will return a family of metrics you can then feed through an
72[`Encoder`](trait.Encoder.html) and report to Promethus.
73
74```
75# use prometheus::IntCounter;
76use prometheus::{self, TextEncoder, Encoder};
77
78use lazy_static::lazy_static;
79use prometheus::register_int_counter;
80
81// Register & measure some metrics.
82# lazy_static! {
83# static ref HIGH_FIVE_COUNTER: IntCounter =
84# register_int_counter!("highfives", "Number of high fives received").unwrap();
85# }
86# HIGH_FIVE_COUNTER.inc();
87
88let mut buffer = Vec::new();
89let encoder = TextEncoder::new();
90
91// Gather the metrics.
92let metric_families = prometheus::gather();
93// Encode them to send.
94encoder.encode(&metric_families, &mut buffer).unwrap();
95
96let output = String::from_utf8(buffer.clone()).unwrap();
97const EXPECTED_OUTPUT: &'static str = "# HELP highfives Number of high fives received\n# TYPE highfives counter\nhighfives 1\n";
98assert!(output.starts_with(EXPECTED_OUTPUT));
99```
100
101See [prometheus_static_metric](https://docs.rs/prometheus-static-metric) for
102additional functionality.
103
104
105# Features
106
107This library supports four features:
108
109* `gen`: To generate protobuf client with the latest protobuf version instead of
110 using the pre-generated client.
111* `nightly`: Enable nightly only features.
112* `process`: For collecting process info.
113* `push`: Enable push support.
114
115*/
116
117#![allow(
118 clippy::needless_pass_by_value,
119 clippy::new_without_default,
120 clippy::new_ret_no_self
121)]
122#![deny(missing_docs)]
123#![deny(missing_debug_implementations)]
124
125/// Protocol buffers format of metrics.
126#[cfg(feature = "protobuf")]
127#[allow(warnings)]
128#[rustfmt::skip]
129#[path = "../proto/proto_model.rs"]
130pub mod proto;
131
132#[cfg(not(feature = "protobuf"))]
133#[path = "plain_model.rs"]
134pub mod proto;
135
136#[cfg(feature = "protobuf")]
137mod proto_ext;
138
139#[macro_use]
140mod macros;
141mod atomic64;
142mod auto_flush;
143mod counter;
144mod desc;
145mod encoder;
146mod errors;
147mod gauge;
148mod histogram;
149mod metrics;
150mod nohash;
151mod pulling_gauge;
152#[cfg(feature = "push")]
153mod push;
154mod registry;
155mod value;
156mod vec;
157
158// Public for generated code.
159#[doc(hidden)]
160pub mod timer;
161
162#[cfg(all(feature = "process", target_os = "linux"))]
163pub mod process_collector;
164
165pub mod local {
166 /*!
167
168 Unsync local metrics, provides better performance.
169
170 */
171 pub use super::counter::{
172 CounterWithValueType, LocalCounter, LocalCounterVec, LocalIntCounter, LocalIntCounterVec,
173 };
174 pub use super::histogram::{LocalHistogram, LocalHistogramTimer, LocalHistogramVec};
175 pub use super::metrics::{LocalMetric, MayFlush};
176
177 pub use super::auto_flush::{
178 AFLocalCounter, AFLocalHistogram, CounterDelegator, HistogramDelegator,
179 };
180}
181
182pub mod core {
183 /*!
184
185 Core traits and types.
186
187 */
188
189 pub use super::atomic64::*;
190 pub use super::counter::{
191 GenericCounter, GenericCounterVec, GenericLocalCounter, GenericLocalCounterVec,
192 };
193 pub use super::desc::{Desc, Describer};
194 pub use super::gauge::{GenericGauge, GenericGaugeVec};
195 pub use super::metrics::{Collector, Metric, Opts};
196 pub use super::vec::{MetricVec, MetricVecBuilder};
197}
198
199pub use self::counter::{Counter, CounterVec, IntCounter, IntCounterVec};
200pub use self::encoder::Encoder;
201#[cfg(feature = "protobuf")]
202pub use self::encoder::ProtobufEncoder;
203pub use self::encoder::TextEncoder;
204#[cfg(feature = "protobuf")]
205pub use self::encoder::PROTOBUF_FORMAT;
206pub use self::encoder::TEXT_FORMAT;
207pub use self::errors::{Error, Result};
208pub use self::gauge::{Gauge, GaugeVec, IntGauge, IntGaugeVec};
209pub use self::histogram::DEFAULT_BUCKETS;
210pub use self::histogram::{exponential_buckets, linear_buckets};
211pub use self::histogram::{Histogram, HistogramOpts, HistogramTimer, HistogramVec};
212pub use self::metrics::Opts;
213pub use self::pulling_gauge::PullingGauge;
214#[cfg(feature = "push")]
215pub use self::push::{
216 hostname_grouping_key, push_add_collector, push_add_metrics, push_collector, push_metrics,
217 BasicAuthentication,
218};
219pub use self::registry::Registry;
220pub use self::registry::{default_registry, gather, register, unregister};