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(feature = "protobuf")]
133macro_rules! from_vec {
134    ($e: expr) => {
135        ::protobuf::RepeatedField::from_vec($e)
136    };
137}
138
139#[cfg(not(feature = "protobuf"))]
140#[path = "plain_model.rs"]
141pub mod proto;
142
143#[cfg(not(feature = "protobuf"))]
144macro_rules! from_vec {
145    ($e: expr) => {
146        $e
147    };
148}
149
150#[macro_use]
151mod macros;
152mod atomic64;
153mod auto_flush;
154mod counter;
155mod desc;
156mod encoder;
157mod errors;
158mod gauge;
159mod histogram;
160mod metrics;
161mod pulling_gauge;
162#[cfg(feature = "push")]
163mod push;
164mod registry;
165mod value;
166mod vec;
167
168// Public for generated code.
169#[doc(hidden)]
170pub mod timer;
171
172#[cfg(all(feature = "process", target_os = "linux"))]
173pub mod process_collector;
174
175pub mod local {
176    /*!
177
178    Unsync local metrics, provides better performance.
179
180    */
181    pub use super::counter::{
182        CounterWithValueType, LocalCounter, LocalCounterVec, LocalIntCounter, LocalIntCounterVec,
183    };
184    pub use super::histogram::{LocalHistogram, LocalHistogramTimer, LocalHistogramVec};
185    pub use super::metrics::{LocalMetric, MayFlush};
186
187    pub use super::auto_flush::{
188        AFLocalCounter, AFLocalHistogram, CounterDelegator, HistogramDelegator,
189    };
190}
191
192pub mod core {
193    /*!
194
195    Core traits and types.
196
197    */
198
199    pub use super::atomic64::*;
200    pub use super::counter::{
201        GenericCounter, GenericCounterVec, GenericLocalCounter, GenericLocalCounterVec,
202    };
203    pub use super::desc::{Desc, Describer};
204    pub use super::gauge::{GenericGauge, GenericGaugeVec};
205    pub use super::metrics::{Collector, Metric, Opts};
206    pub use super::vec::{MetricVec, MetricVecBuilder};
207}
208
209pub use self::counter::{Counter, CounterVec, IntCounter, IntCounterVec};
210pub use self::encoder::Encoder;
211#[cfg(feature = "protobuf")]
212pub use self::encoder::ProtobufEncoder;
213pub use self::encoder::TextEncoder;
214#[cfg(feature = "protobuf")]
215pub use self::encoder::PROTOBUF_FORMAT;
216pub use self::encoder::TEXT_FORMAT;
217pub use self::errors::{Error, Result};
218pub use self::gauge::{Gauge, GaugeVec, IntGauge, IntGaugeVec};
219pub use self::histogram::DEFAULT_BUCKETS;
220pub use self::histogram::{exponential_buckets, linear_buckets};
221pub use self::histogram::{Histogram, HistogramOpts, HistogramTimer, HistogramVec};
222pub use self::metrics::Opts;
223pub use self::pulling_gauge::PullingGauge;
224#[cfg(feature = "push")]
225pub use self::push::{
226    hostname_grouping_key, push_add_collector, push_add_metrics, push_collector, push_metrics,
227    BasicAuthentication,
228};
229pub use self::registry::Registry;
230pub use self::registry::{default_registry, gather, register, unregister};