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
use std::sync::Arc;
use proto;
use metrics::{Opts, Collector, Metric};
use value::{Value, ValueType};
use desc::Desc;
use errors::{Result, Error};
use vec::{MetricVec, MetricVecBuilder};
#[derive(Clone)]
pub struct Counter {
v: Arc<Value>,
}
impl Counter {
pub fn new<S: Into<String>>(name: S, help: S) -> Result<Counter> {
let opts = Opts::new(name, help);
Counter::with_opts(opts)
}
pub fn with_opts(opts: Opts) -> Result<Counter> {
let desc = try!(Desc::new(opts.fq_name(), opts.help, vec![], opts.const_labels));
Counter::with_desc(desc, &[])
}
fn with_desc(desc: Desc, label_values: &[&str]) -> Result<Counter> {
let v = try!(Value::new(desc, ValueType::Counter, 0.0, label_values));
Ok(Counter { v: Arc::new(v) })
}
#[inline]
pub fn inc_by(&self, v: f64) -> Result<()> {
if v < 0.0 {
return Err(Error::DecreaseCounter(v));
}
Ok(self.v.inc_by(v))
}
#[inline]
pub fn inc(&self) {
self.inc_by(1.0).unwrap()
}
#[inline]
pub fn get(&self) -> f64 {
self.v.get()
}
}
impl Collector for Counter {
fn desc(&self) -> &Desc {
&self.v.desc
}
fn collect(&self) -> proto::MetricFamily {
self.v.collect()
}
}
impl Metric for Counter {
fn metric(&self) -> proto::Metric {
self.v.metric()
}
}
#[derive(Clone)]
pub struct CounterVecBuilder {}
impl MetricVecBuilder for CounterVecBuilder {
type Output = Counter;
fn build(&self, desc: &Desc, vals: &[&str]) -> Result<Counter> {
Counter::with_desc(desc.clone(), vals)
}
}
pub type CounterVec = MetricVec<CounterVecBuilder>;
impl CounterVec {
pub fn new(opts: Opts, label_names: &[&str]) -> Result<CounterVec> {
let variable_names = label_names.iter().map(|s| (*s).to_owned()).collect();
let desc = try!(Desc::new(opts.fq_name(), opts.help, variable_names, opts.const_labels));
let metric_vec = MetricVec::create(desc, proto::MetricType::COUNTER, CounterVecBuilder {});
Ok(metric_vec as CounterVec)
}
}
#[cfg(test)]
mod tests {
use metrics::{Opts, Collector};
use super::*;
#[test]
fn test_counter() {
let opts = Opts::new("test", "test help").const_label("a", "1").const_label("b", "2");
let counter = Counter::with_opts(opts).unwrap();
counter.inc();
assert_eq!(counter.get() as u64, 1);
counter.inc_by(42.0).unwrap();
assert_eq!(counter.get() as u64, 43);
let mf = counter.collect();
let m = mf.get_metric().as_ref().get(0).unwrap();
assert_eq!(m.get_label().len(), 2);
assert_eq!(m.get_counter().get_value() as u64, 43);
}
}