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