#[macro_use]
extern crate lazy_static;
extern crate prometheus;
use prometheus::{Encoder, IntCounter, Registry};
use std::collections::HashMap;
lazy_static! {
static ref DEFAULT_COUNTER: IntCounter = IntCounter::new("default", "generic counter").unwrap();
static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap();
}
fn main() {
default_metrics(prometheus::default_registry());
let mut labels = HashMap::new();
labels.insert("mykey".to_string(), "myvalue".to_string());
let custom_registry = Registry::new_custom(Some("myprefix".to_string()), Some(labels)).unwrap();
custom_metrics(&custom_registry);
let mut buffer = Vec::<u8>::new();
let encoder = prometheus::TextEncoder::new();
encoder.encode(&prometheus::gather(), &mut buffer).unwrap();
println!("## Default registry");
println!("{}", String::from_utf8(buffer.clone()).unwrap());
let mut buffer = Vec::<u8>::new();
let encoder = prometheus::TextEncoder::new();
encoder
.encode(&custom_registry.gather(), &mut buffer)
.unwrap();
println!("## Custom registry");
println!("{}", String::from_utf8(buffer.clone()).unwrap());
}
fn default_metrics(registry: &Registry) {
registry
.register(Box::new(DEFAULT_COUNTER.clone()))
.unwrap();
DEFAULT_COUNTER.inc();
assert_eq!(DEFAULT_COUNTER.get(), 1);
}
fn custom_metrics(registry: &Registry) {
registry.register(Box::new(CUSTOM_COUNTER.clone())).unwrap();
CUSTOM_COUNTER.inc_by(42);
assert_eq!(CUSTOM_COUNTER.get(), 42);
}