rust-prometheus/static-metric/examples/simple.rs

32 lines
850 B
Rust

// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use prometheus::{CounterVec, Opts};
use prometheus_static_metric::make_static_metric;
make_static_metric! {
pub struct MyStaticCounterVec: Counter {
"method" => {
post,
get,
put,
delete,
},
"product" => {
foo,
bar,
},
}
}
fn main() {
let vec = CounterVec::new(Opts::new("foo", "bar"), &["method", "product"]).unwrap();
let static_counter_vec = MyStaticCounterVec::from(&vec);
static_counter_vec.post.foo.inc();
static_counter_vec.delete.bar.inc_by(4.0);
assert_eq!(static_counter_vec.post.bar.get(), 0.0);
assert_eq!(vec.with_label_values(&["post", "foo"]).get(), 1.0);
assert_eq!(vec.with_label_values(&["delete", "bar"]).get(), 4.0);
}