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
//! A fork of the lighthouse_metrics crate used to implement prometheus
//!
//! A wrapper around the `prometheus` crate that provides a global, `lazy_static` metrics registry
//! and functions to add and use the following components (more info at
//! [Prometheus docs](https://prometheus.io/docs/concepts/metric_types/)):
//!
//! - `Histogram`: used with `start_timer()` and `observe_duration()` or
//! `observe()` method to record durations (e.g., block processing time).
//! - `IncCounter`: used to represent an ideally ever-growing, never-shrinking
//! integer (e.g., number of block processing requests).
//! - `IntGauge`: used to represent an varying integer (e.g., number of
//! attestations per block).
//!
//! ## Important
//!
//! Metrics will fail if two items have the same `name`. All metrics must have a unique `name`.
//! Because we use a global registry there is no namespace per crate, it's one big global space.
//!
//! See the [Prometheus naming best practices](https://prometheus.io/docs/practices/naming/) when
//! choosing metric names.
//!
//! ## Example
//!
//! ```rust
//! use once_cell::sync::Lazy;
//!
//! use near_metrics::*;
//!
//! // These metrics are "magically" linked to the global registry defined in `lighthouse_metrics`.
//! pub static RUN_COUNT: Lazy<IntCounter> = Lazy::new(|| {
//! try_create_int_counter(
//! "runs_total",
//! "Total number of runs",
//! )
//! .unwrap()
//! });
//! pub static CURRENT_VALUE: Lazy<IntGauge> = Lazy::new(|| {
//! try_create_int_gauge(
//! "current_value",
//! "The current value",
//! )
//! .unwrap()
//! });
//! pub static RUN_TIME: Lazy<Histogram> = Lazy::new(|| {
//! try_create_histogram(
//! "run_seconds",
//! "Time taken (measured to high precision)",
//! )
//! .unwrap()
//! });
//!
//! fn main() {
//! for i in 0..100 {
//! RUN_COUNT.inc();
//! let timer = RUN_TIME.start_timer();
//! for j in 0..10 {
//! CURRENT_VALUE.set(j);
//! println!("Howdy partner");
//! }
//! timer.observe_duration();
//! }
//!
//! assert_eq!(100, RUN_COUNT.get());
//! assert_eq!(9, CURRENT_VALUE.get());
//! assert_eq!(100, RUN_TIME.get_sample_count());
//! assert!(0.0 < RUN_TIME.get_sample_sum());
//! }
//! ```
pub use prometheus::{
Encoder, Histogram, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Result,
TextEncoder,
};
use prometheus::{GaugeVec, HistogramOpts, Opts};
/// Collect all the metrics for reporting.
pub fn gather() -> Vec<prometheus::proto::MetricFamily> {
prometheus::gather()
}
/// Attempts to crate an `IntCounter`, returning `Err` if the registry does not accept the counter
/// (potentially due to naming conflict).
pub fn try_create_int_counter(name: &str, help: &str) -> Result<IntCounter> {
let opts = Opts::new(name, help);
let counter = IntCounter::with_opts(opts)?;
prometheus::register(Box::new(counter.clone()))?;
Ok(counter)
}
/// Attempts to crate an `IntCounterVec`, returning `Err` if the registry does not accept the counter
/// (potentially due to naming conflict).
pub fn try_create_int_counter_vec(
name: &str,
help: &str,
labels: &[&str],
) -> Result<IntCounterVec> {
let opts = Opts::new(name, help);
let counter = IntCounterVec::new(opts, labels)?;
prometheus::register(Box::new(counter.clone()))?;
Ok(counter)
}
/// Creates 'IntCounterVec' - if it has trouble registering to Prometheus
/// it will keep appending a number until the name is unique.
pub fn do_create_int_counter_vec(name: &str, help: &str, labels: &[&str]) -> IntCounterVec {
if let Ok(value) = try_create_int_counter_vec(name, help, labels) {
return value;
}
let mut suffix = 0;
loop {
if let Ok(value) =
try_create_int_counter_vec(format!("{}_{}", name, suffix).as_str(), help, labels)
{
return value;
}
suffix += 1;
}
}
/// Attempts to crate an `IntGauge`, returning `Err` if the registry does not accept the gauge
/// (potentially due to naming conflict).
pub fn try_create_int_gauge(name: &str, help: &str) -> Result<IntGauge> {
let opts = Opts::new(name, help);
let gauge = IntGauge::with_opts(opts)?;
prometheus::register(Box::new(gauge.clone()))?;
Ok(gauge)
}
/// Attempts to crate an `IntGaugeVec`, returning `Err` if the registry does not accept the gauge
/// (potentially due to naming conflict).
pub fn try_create_int_gauge_vec(name: &str, help: &str, labels: &[&str]) -> Result<IntGaugeVec> {
let opts = Opts::new(name, help);
let gauge = IntGaugeVec::new(opts, labels)?;
prometheus::register(Box::new(gauge.clone()))?;
Ok(gauge)
}
/// Attempts to crate a `Histogram`, returning `Err` if the registry does not accept the counter
/// (potentially due to naming conflict).
pub fn try_create_histogram(name: &str, help: &str) -> Result<Histogram> {
let opts = HistogramOpts::new(name, help);
let histogram = Histogram::with_opts(opts)?;
prometheus::register(Box::new(histogram.clone()))?;
Ok(histogram)
}
/// Attempts to create a `HistogramVector`, returning `Err` if the registry does not accept the counter
/// (potentially due to naming conflict).
pub fn try_create_histogram_vec(
name: &str,
help: &str,
labels: &[&str],
buckets: Option<Vec<f64>>,
) -> Result<HistogramVec> {
let mut opts = HistogramOpts::new(name, help);
if let Some(buckets) = buckets {
opts = opts.buckets(buckets);
}
let histogram = HistogramVec::new(opts, labels)?;
prometheus::register(Box::new(histogram.clone()))?;
Ok(histogram)
}
/// Attempts to crate an `GaugeVec`, returning `Err` if the registry does not accept the gauge
/// (potentially due to naming conflict).
pub fn try_create_gauge_vec(name: &str, help: &str, labels: &[&str]) -> Result<GaugeVec> {
let opts = Opts::new(name, help);
let gauge = GaugeVec::new(opts, labels)?;
prometheus::register(Box::new(gauge.clone()))?;
Ok(gauge)
}