-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathlib.rs
213 lines (179 loc) · 6.57 KB
/
lib.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
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
204
205
206
207
208
209
210
211
212
213
use std::sync::Arc;
use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
struct Labels {
method: Method,
path: String,
}
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelValue, Debug)]
enum Method {
Get,
#[allow(dead_code)]
Put,
}
#[test]
fn basic_flow() {
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
family
.get_or_create(&Labels {
method: Method::Get,
path: "/metrics".to_string(),
})
.inc();
// Encode all metrics in the registry in the text format.
let mut buffer = String::new();
encode(&mut buffer, ®istry).unwrap();
let expected = "# HELP my_counter This is my counter.\n".to_owned()
+ "# TYPE my_counter counter\n"
+ "my_counter_total{method=\"Get\",path=\"/metrics\"} 1\n"
+ "# EOF\n";
assert_eq!(expected, buffer);
}
mod protobuf {
use crate::{Labels, Method};
use prometheus_client::encoding::protobuf::encode;
use prometheus_client::encoding::protobuf::openmetrics_data_model;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
#[test]
fn structs() {
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
family
.get_or_create(&Labels {
method: Method::Get,
path: "/metrics".to_string(),
})
.inc();
// Encode all metrics in the registry in the OpenMetrics protobuf format.
let mut metric_set = encode(®istry).unwrap();
let mut family: openmetrics_data_model::MetricFamily =
metric_set.metric_families.pop().unwrap();
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
let method = &metric.labels[0];
assert_eq!("method", method.name);
assert_eq!("Get", method.value);
let path = &metric.labels[1];
assert_eq!("path", path.name);
assert_eq!("/metrics", path.value);
}
#[test]
fn enums() {
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
family
.get_or_create(&Labels {
method: Method::Get,
path: "/metrics".to_string(),
})
.inc();
// Encode all metrics in the registry in the OpenMetrics protobuf format.
let mut metric_set = encode(®istry).unwrap();
let mut family: openmetrics_data_model::MetricFamily =
metric_set.metric_families.pop().unwrap();
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
let label = &metric.labels[0];
assert_eq!("method", label.name);
assert_eq!("Get", label.value);
}
}
#[test]
fn remap_keyword_identifiers() {
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct Labels {
// `r#type` is problematic as `r#` is not a valid OpenMetrics label name
// but one needs to use keyword identifier syntax (aka. raw identifiers)
// as `type` is a keyword.
//
// Test makes sure `r#type` is replaced by `type` in the OpenMetrics
// output.
r#type: u64,
}
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
family.get_or_create(&Labels { r#type: 42 }).inc();
// Encode all metrics in the registry in the text format.
let mut buffer = String::new();
encode(&mut buffer, ®istry).unwrap();
let expected = "# HELP my_counter This is my counter.\n".to_owned()
+ "# TYPE my_counter counter\n"
+ "my_counter_total{type=\"42\"} 1\n"
+ "# EOF\n";
assert_eq!(expected, buffer);
}
#[test]
fn arc_string() {
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct Labels {
client_id: Arc<String>,
}
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
let client_id = Arc::new("client_id".to_string());
family
.get_or_create(&Labels {
client_id: client_id.clone(),
})
.inc();
// Encode all metrics in the registry in the text format.
let mut buffer = String::new();
encode(&mut buffer, ®istry).unwrap();
let expected = "# HELP my_counter This is my counter.\n".to_owned()
+ "# TYPE my_counter counter\n"
+ "my_counter_total{client_id=\"client_id\"} 1\n"
+ "# EOF\n";
assert_eq!(expected, buffer);
}
#[test]
fn flatten() {
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct CommonLabels {
a: u64,
b: u64,
}
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct Labels {
unique: u64,
#[prometheus(flatten)]
common: CommonLabels,
}
let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());
// Record a single HTTP GET request.
family
.get_or_create(&Labels {
unique: 1,
common: CommonLabels { a: 2, b: 3 },
})
.inc();
// Encode all metrics in the registry in the text format.
let mut buffer = String::new();
encode(&mut buffer, ®istry).unwrap();
let expected = "# HELP my_counter This is my counter.\n".to_owned()
+ "# TYPE my_counter counter\n"
+ "my_counter_total{unique=\"1\",a=\"2\",b=\"3\"} 1\n"
+ "# EOF\n";
assert_eq!(expected, buffer);
}
#[test]
fn build() {
let t = trybuild::TestCases::new();
t.pass("tests/build/redefine-prelude-symbols.rs")
}