Prometheus, heavy memory calls removed (#27)
This commit is contained in:
@@ -19,10 +19,47 @@
|
||||
use std::{
|
||||
iter::FromIterator,
|
||||
ops::{Add, Deref, Div, Sub},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
pub extern crate prometheus;
|
||||
|
||||
/// Implements a prometheus metrics collector
|
||||
pub trait PrometheusMetrics {
|
||||
fn prometheus_metrics(&self, registry: &mut prometheus::Registry);
|
||||
}
|
||||
|
||||
/// Adds a new prometheus counter with the specified value
|
||||
pub fn prometheus_counter(reg: &mut prometheus::Registry, name: &str, help: &str, value: i64) {
|
||||
let c = prometheus::IntCounter::new(name, help).expect("name and help must be non-empty");
|
||||
c.inc_by(value);
|
||||
reg.register(Box::new(c))
|
||||
.expect("prometheus identifiers must be unique");
|
||||
}
|
||||
|
||||
/// Adds a new prometheus gauge with the specified gauge
|
||||
pub fn prometheus_gauge(reg: &mut prometheus::Registry, name: &str, help: &str, value: i64) {
|
||||
let g = prometheus::IntGauge::new(name, help).expect("name and help must be non-empty");
|
||||
g.set(value);
|
||||
reg.register(Box::new(g))
|
||||
.expect("prometheus identifiers must be are unique");
|
||||
}
|
||||
|
||||
/// Adds a new prometheus counter with the time spent in running the specified function
|
||||
pub fn prometheus_optime<F: Fn() -> T, T>(r: &mut prometheus::Registry, name: &str, f: &F) -> T {
|
||||
let start = Instant::now();
|
||||
let t = f();
|
||||
let elapsed = start.elapsed();
|
||||
prometheus_gauge(
|
||||
r,
|
||||
&format!("optime_{}", name),
|
||||
&format!("Time to perform {}", name),
|
||||
elapsed.as_millis() as i64,
|
||||
);
|
||||
t
|
||||
}
|
||||
|
||||
/// Sorted corpus of data.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
||||
Reference in New Issue
Block a user