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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::collections::HashMap;
use desc::Desc;
use proto::{self, LabelPair};
use std::cmp::{Ord, Ordering, Eq, PartialOrd};
pub const SEPARATOR_BYTE: u8 = 0xFF;
pub trait Collector: Sync + Send {
fn desc(&self) -> &Desc;
fn collect(&self) -> proto::MetricFamily;
}
pub trait Metric: Sync + Send + Clone {
fn metric(&self) -> proto::Metric;
}
#[derive(Debug)]
pub struct Opts {
pub namespace: String,
pub sub_system: String,
pub name: String,
pub help: String,
pub const_labels: HashMap<String, String>,
}
impl Opts {
pub fn new<S: Into<String>>(name: S, help: S) -> Opts {
Opts {
namespace: "".to_owned(),
sub_system: "".to_owned(),
name: name.into(),
help: help.into(),
const_labels: HashMap::new(),
}
}
pub fn namespace<S: Into<String>>(mut self, namesapce: S) -> Self {
self.namespace = namesapce.into();
self
}
pub fn sub_system<S: Into<String>>(mut self, sub_system: S) -> Self {
self.sub_system = sub_system.into();
self
}
pub fn const_labels(mut self, labels: HashMap<String, String>) -> Self {
self.const_labels = labels;
self
}
pub fn const_label<S: Into<String>>(mut self, name: S, value: S) -> Self {
self.const_labels.insert(name.into(), value.into());
self
}
pub fn fq_name(&self) -> String {
build_fq_name(&self.namespace, &self.sub_system, &self.name)
}
}
impl Ord for LabelPair {
fn cmp(&self, other: &LabelPair) -> Ordering {
self.get_name().cmp(other.get_name())
}
}
impl Eq for LabelPair {}
impl PartialOrd for LabelPair {
fn partial_cmp(&self, other: &LabelPair) -> Option<Ordering> {
Some(self.cmp(other))
}
}
pub fn build_fq_name(namespace: &str, sub_system: &str, name: &str) -> String {
if name.is_empty() {
return "".to_owned();
}
if !namespace.is_empty() && !sub_system.is_empty() {
return format!("{}_{}_{}", namespace, sub_system, name);
} else if !namespace.is_empty() {
return format!("{}_{}", namespace, name);
} else if !sub_system.is_empty() {
return format!("{}_{}", sub_system, name);
}
name.to_owned()
}
#[cfg(test)]
mod tests {
use std::cmp::{Ord, Ordering};
use super::*;
use proto::LabelPair;
fn new_label_pair(name: &str, value: &str) -> LabelPair {
let mut l = LabelPair::new();
l.set_name(name.to_owned());
l.set_value(value.to_owned());
l
}
#[test]
fn test_label_cmp() {
let tbl = vec![
("k1", "k2", Ordering::Less),
("k1", "k1", Ordering::Equal),
("k1", "k0", Ordering::Greater),
];
for (l1, l2, order) in tbl {
let lhs = new_label_pair(l1, l1);
let rhs = new_label_pair(l2, l2);
assert_eq!(lhs.cmp(&rhs), order);
}
}
#[test]
fn test_build_fq_name() {
let tbl = vec![
("a", "b", "c", "a_b_c"),
("", "b", "c", "b_c"),
("a", "", "c", "a_c"),
("", "", "c", "c"),
("a", "b", "", ""),
("a", "", "", ""),
("", "b", "", ""),
(" ", "", "", ""),
];
for (namespace, sub_system, name, res) in tbl {
assert_eq!(&build_fq_name(namespace, sub_system, name), res);
}
}
}