forked from tikv/rust-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_enum.rs
62 lines (48 loc) · 1.72 KB
/
metric_enum.rs
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
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
/*!
Use metric enums to reuse possible values of a label.
*/
use prometheus::{CounterVec, IntCounterVec, Opts};
use prometheus_static_metric::make_static_metric;
make_static_metric! {
pub label_enum Methods {
post,
get,
put,
delete,
}
pub label_enum Products {
foo,
bar,
}
pub struct MyStaticCounterVec: Counter {
"method" => Methods,
"product" => Products,
}
pub struct MyAnotherStaticCounterVec: IntCounter {
"error" => {
error_1,
error_2,
},
"error_method" => Methods,
}
}
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);
// metric enums will expose an enum for type-safe `get()`.
static_counter_vec.get(Methods::post).foo.inc();
assert_eq!(static_counter_vec.post.foo.get(), 2.0);
let vec = IntCounterVec::new(Opts::new("foo", "bar"), &["error", "error_method"]).unwrap();
let static_counter_vec = MyAnotherStaticCounterVec::from(&vec);
static_counter_vec.error_1.post.inc();
static_counter_vec.error_2.delete.inc_by(4);
assert_eq!(static_counter_vec.error_1.delete.get(), 0);
assert_eq!(static_counter_vec.error_1.post.get(), 1);
assert_eq!(vec.with_label_values(&["error_2", "delete"]).get(), 4);
}