Debian Codegraph @

tests/openmetrics/test_parser.py

raw symbols SCIP
1 import math
2 import unittest
3
4 from prometheus_client.core import (
5 CollectorRegistry, CounterMetricFamily, Exemplar,
6 GaugeHistogramMetricFamily, GaugeMetricFamily, HistogramMetricFamily,
7 InfoMetricFamily, Metric, Sample, StateSetMetricFamily,
8 SummaryMetricFamily, Timestamp,
9 )
10 from prometheus_client.openmetrics.exposition import generate_latest
11 from prometheus_client.openmetrics.parser import text_string_to_metric_families
12
13
14 class TestParse(unittest.TestCase):
15
16 def test_simple_counter(self):
17 families = text_string_to_metric_families("""# TYPE a counter
18 # HELP a help
19 a_total 1
20 # EOF
21 """)
22 self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
23
24 def test_uint64_counter(self):
25 families = text_string_to_metric_families("""# TYPE a counter
26 # HELP a help
27 a_total 9223372036854775808
28 # EOF
29 """)
30 self.assertEqual([CounterMetricFamily("a", "help", value=9223372036854775808)], list(families))
31
32 def test_simple_gauge(self):
33 families = text_string_to_metric_families("""# TYPE a gauge
34 # HELP a help
35 a 1
36 # EOF
37 """)
38 self.assertEqual([GaugeMetricFamily("a", "help", value=1)], list(families))
39
40 def test_float_gauge(self):
41 families = text_string_to_metric_families("""# TYPE a gauge
42 # HELP a help
43 a 1.2
44 # EOF
45 """)
46 self.assertEqual([GaugeMetricFamily("a", "help", value=1.2)], list(families))
47
48 def test_leading_zeros_simple_gauge(self):
49 families = text_string_to_metric_families("""# TYPE a gauge
50 # HELP a help
51 a 0000000000000000000000000000000000000000001
52 # EOF
53 """)
54 self.assertEqual([GaugeMetricFamily("a", "help", value=1)], list(families))
55
56 def test_leading_zeros_float_gauge(self):
57 families = text_string_to_metric_families("""# TYPE a gauge
58 # HELP a help
59 a 0000000000000000000000000000000000000000001.2e-1
60 # EOF
61 """)
62 self.assertEqual([GaugeMetricFamily("a", "help", value=.12)], list(families))
63
64 def test_nan_gauge(self):
65 families = text_string_to_metric_families("""# TYPE a gauge
66 # HELP a help
67 a NaN
68 # EOF
69 """)
70 self.assertTrue(math.isnan(list(families)[0].samples[0].value))
71
72 def test_unit_gauge(self):
73 families = text_string_to_metric_families("""# TYPE a_seconds gauge
74 # UNIT a_seconds seconds
75 # HELP a_seconds help
76 a_seconds 1
77 # EOF
78 """)
79 self.assertEqual([GaugeMetricFamily("a_seconds", "help", value=1, unit='seconds')], list(families))
80
81 def test_simple_summary(self):
82 families = text_string_to_metric_families("""# TYPE a summary
83 # HELP a help
84 a_count 1
85 a_sum 2
86 # EOF
87 """)
88 summary = SummaryMetricFamily("a", "help", count_value=1, sum_value=2)
89 self.assertEqual([summary], list(families))
90
91 def test_summary_quantiles(self):
92 families = text_string_to_metric_families("""# TYPE a summary
93 # HELP a help
94 a_count 1
95 a_sum 2
96 a{quantile="0.5"} 0.7
97 a{quantile="1"} 0.8
98 # EOF
99 """)
100 # The Python client doesn't support quantiles, but we
101 # still need to be able to parse them.
102 metric_family = SummaryMetricFamily("a", "help", count_value=1, sum_value=2)
103 metric_family.add_sample("a", {"quantile": "0.5"}, 0.7)
104 metric_family.add_sample("a", {"quantile": "1"}, 0.8)
105 self.assertEqual([metric_family], list(families))
106
107 def test_simple_histogram(self):
108 families = text_string_to_metric_families("""# TYPE a histogram
109 # HELP a help
110 a_bucket{le="1.0"} 0
111 a_bucket{le="+Inf"} 3
112 a_count 3
113 a_sum 2
114 # EOF
115 """)
116 self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
117 list(families))
118
119 def test_simple_histogram_float_values(self):
120 families = text_string_to_metric_families("""# TYPE a histogram
121 # HELP a help
122 a_bucket{le="1.0"} 0.0
123 a_bucket{le="+Inf"} 3.0
124 a_count 3.0
125 a_sum 2.0
126 # EOF
127 """)
128 self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
129 list(families))
130
131 def test_histogram_noncanonical(self):
132 families = text_string_to_metric_families("""# TYPE a histogram
133 # HELP a help
134 a_bucket{le="0"} 0
135 a_bucket{le="0.00000000001"} 0
136 a_bucket{le="0.0000000001"} 0
137 a_bucket{le="1e-04"} 0
138 a_bucket{le="1.1e-4"} 0
139 a_bucket{le="1.1e-3"} 0
140 a_bucket{le="1.1e-2"} 0
141 a_bucket{le="1"} 0
142 a_bucket{le="1e+05"} 0
143 a_bucket{le="10000000000"} 0
144 a_bucket{le="100000000000.0"} 0
145 a_bucket{le="+Inf"} 3
146 a_count 3
147 a_sum 2
148 # EOF
149 """)
150 list(families)
151
152 def test_negative_bucket_histogram(self):
153 families = text_string_to_metric_families("""# TYPE a histogram
154 # HELP a help
155 a_bucket{le="-1.0"} 0
156 a_bucket{le="1.0"} 1
157 a_bucket{le="+Inf"} 3
158 # EOF
159 """)
160 self.assertEqual([HistogramMetricFamily("a", "help", buckets=[("-1.0", 0.0), ("1.0", 1.0), ("+Inf", 3.0)])],
161 list(families))
162
163 def test_histogram_exemplars(self):
164 families = text_string_to_metric_families("""# TYPE a histogram
165 # HELP a help
166 a_bucket{le="1.0"} 0 # {a="b"} 0.5
167 a_bucket{le="2.0"} 2 # {a="c"} 0.5
168 a_bucket{le="+Inf"} 3 # {a="2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"} 4 123
169 # EOF
170 """)
171 hfm = HistogramMetricFamily("a", "help")
172 hfm.add_sample("a_bucket", {"le": "1.0"}, 0.0, None, Exemplar({"a": "b"}, 0.5))
173 hfm.add_sample("a_bucket", {"le": "2.0"}, 2.0, None, Exemplar({"a": "c"}, 0.5)),
174 hfm.add_sample("a_bucket", {"le": "+Inf"}, 3.0, None,
175 Exemplar({"a": "2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"}, 4,
176 Timestamp(123, 0)))
177 self.assertEqual([hfm], list(families))
178
179 def test_simple_gaugehistogram(self):
180 families = text_string_to_metric_families("""# TYPE a gaugehistogram
181 # HELP a help
182 a_bucket{le="1.0"} 0
183 a_bucket{le="+Inf"} 3
184 a_gcount 3
185 a_gsum 2
186 # EOF
187 """)
188 self.assertEqual([GaugeHistogramMetricFamily("a", "help", gsum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
189 list(families))
190
191 def test_negative_bucket_gaugehistogram(self):
192 families = text_string_to_metric_families("""# TYPE a gaugehistogram
193 # HELP a help
194 a_bucket{le="-1.0"} 1
195 a_bucket{le="1.0"} 2
196 a_bucket{le="+Inf"} 3
197 a_gcount 3
198 a_gsum -5
199 # EOF
200 """)
201 self.assertEqual([GaugeHistogramMetricFamily("a", "help", gsum_value=-5, buckets=[("-1.0", 1.0), ("1.0", 2.0), ("+Inf", 3.0)])],
202 list(families))
203
204 def test_gaugehistogram_exemplars(self):
205 families = text_string_to_metric_families("""# TYPE a gaugehistogram
206 # HELP a help
207 a_bucket{le="1.0"} 0 123 # {a="b"} 0.5
208 a_bucket{le="2.0"} 2 123 # {a="c"} 0.5
209 a_bucket{le="+Inf"} 3 123 # {a="d"} 4 123
210 # EOF
211 """)
212 hfm = GaugeHistogramMetricFamily("a", "help")
213 hfm.add_sample("a_bucket", {"le": "1.0"}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
214 hfm.add_sample("a_bucket", {"le": "2.0"}, 2.0, Timestamp(123, 0), Exemplar({"a": "c"}, 0.5)),
215 hfm.add_sample("a_bucket", {"le": "+Inf"}, 3.0, Timestamp(123, 0), Exemplar({"a": "d"}, 4, Timestamp(123, 0)))
216 self.assertEqual([hfm], list(families))
217
218 def test_counter_exemplars(self):
219 families = text_string_to_metric_families("""# TYPE a counter
220 # HELP a help
221 a_total 0 123 # {a="b"} 0.5
222 # EOF
223 """)
224 cfm = CounterMetricFamily("a", "help")
225 cfm.add_sample("a_total", {}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
226 self.assertEqual([cfm], list(families))
227
228 def test_counter_exemplars_empty_brackets(self):
229 families = text_string_to_metric_families("""# TYPE a counter
230 # HELP a help
231 a_total{} 0 123 # {a="b"} 0.5
232 # EOF
233 """)
234 cfm = CounterMetricFamily("a", "help")
235 cfm.add_sample("a_total", {}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
236 self.assertEqual([cfm], list(families))
237
238 def test_simple_info(self):
239 families = text_string_to_metric_families("""# TYPE a info
240 # HELP a help
241 a_info{foo="bar"} 1
242 # EOF
243 """)
244 self.assertEqual([InfoMetricFamily("a", "help", {'foo': 'bar'})], list(families))
245
246 def test_info_timestamps(self):
247 families = text_string_to_metric_families("""# TYPE a info
248 # HELP a help
249 a_info{a="1",foo="bar"} 1 1
250 a_info{a="2",foo="bar"} 1 0
251 # EOF
252 """)
253 imf = InfoMetricFamily("a", "help")
254 imf.add_sample("a_info", {"a": "1", "foo": "bar"}, 1, Timestamp(1, 0))
255 imf.add_sample("a_info", {"a": "2", "foo": "bar"}, 1, Timestamp(0, 0))
256 self.assertEqual([imf], list(families))
257
258 def test_simple_stateset(self):
259 families = text_string_to_metric_families("""# TYPE a stateset
260 # HELP a help
261 a{a="bar"} 0
262 a{a="foo"} 1.0
263 # EOF
264 """)
265 self.assertEqual([StateSetMetricFamily("a", "help", {'foo': True, 'bar': False})], list(families))
266
267 def test_duplicate_timestamps(self):
268 families = text_string_to_metric_families("""# TYPE a gauge
269 # HELP a help
270 a{a="1",foo="bar"} 1 0.0000000000
271 a{a="1",foo="bar"} 2 0.0000000001
272 a{a="1",foo="bar"} 3 0.0000000010
273 a{a="2",foo="bar"} 4 0.0000000000
274 a{a="2",foo="bar"} 5 0.0000000001
275 # EOF
276 """)
277 imf = GaugeMetricFamily("a", "help")
278 imf.add_sample("a", {"a": "1", "foo": "bar"}, 1, Timestamp(0, 0))
279 imf.add_sample("a", {"a": "1", "foo": "bar"}, 3, Timestamp(0, 1))
280 imf.add_sample("a", {"a": "2", "foo": "bar"}, 4, Timestamp(0, 0))
281 self.assertEqual([imf], list(families))
282
283 def test_no_metadata(self):
284 families = text_string_to_metric_families("""a 1
285 # EOF
286 """)
287 metric_family = Metric("a", "", "untyped")
288 metric_family.add_sample("a", {}, 1)
289 self.assertEqual([metric_family], list(families))
290
291 def test_empty_metadata(self):
292 families = text_string_to_metric_families("""# HELP a
293 # UNIT a
294 # EOF
295 """)
296 metric_family = Metric("a", "", "untyped")
297 self.assertEqual([metric_family], list(families))
298
299 def test_untyped(self):
300 # https://github.com/prometheus/client_python/issues/79
301 families = text_string_to_metric_families("""# HELP redis_connected_clients Redis connected clients
302 # TYPE redis_connected_clients unknown
303 redis_connected_clients{instance="rough-snowflake-web",port="6380"} 10.0
304 redis_connected_clients{instance="rough-snowflake-web",port="6381"} 12.0
305 # EOF
306 """)
307 m = Metric("redis_connected_clients", "Redis connected clients", "untyped")
308 m.samples = [
309 Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6380"}, 10),
310 Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6381"}, 12),
311 ]
312 self.assertEqual([m], list(families))
313
314 def test_type_help_switched(self):
315 families = text_string_to_metric_families("""# HELP a help
316 # TYPE a counter
317 a_total 1
318 # EOF
319 """)
320 self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
321
322 def test_labels_with_curly_braces(self):
323 families = text_string_to_metric_families("""# TYPE a counter
324 # HELP a help
325 a_total{foo="bar",bar="b{a}z"} 1
326 # EOF
327 """)
328 metric_family = CounterMetricFamily("a", "help", labels=["foo", "bar"])
329 metric_family.add_metric(["bar", "b{a}z"], 1)
330 self.assertEqual([metric_family], list(families))
331
332 def test_empty_help(self):
333 families = text_string_to_metric_families("""# TYPE a counter
334 # HELP a
335 a_total 1
336 # EOF
337 """)
338 self.assertEqual([CounterMetricFamily("a", "", value=1)], list(families))
339
340 def test_labels_and_infinite(self):
341 families = text_string_to_metric_families("""# TYPE a gauge
342 # HELP a help
343 a{foo="bar"} +Inf
344 a{foo="baz"} -Inf
345 # EOF
346 """)
347 metric_family = GaugeMetricFamily("a", "help", labels=["foo"])
348 metric_family.add_metric(["bar"], float('inf'))
349 metric_family.add_metric(["baz"], float('-inf'))
350 self.assertEqual([metric_family], list(families))
351
352 def test_empty_brackets(self):
353 families = text_string_to_metric_families("""# TYPE a counter
354 # HELP a help
355 a_total{} 1
356 # EOF
357 """)
358 self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
359
360 def test_nan(self):
361 families = text_string_to_metric_families("""a NaN
362 # EOF
363 """)
364 self.assertTrue(math.isnan(list(families)[0].samples[0][2]))
365
366 def test_no_newline_after_eof(self):
367 families = text_string_to_metric_families("""# TYPE a gauge
368 # HELP a help
369 a 1
370 # EOF""")
371 self.assertEqual([GaugeMetricFamily("a", "help", value=1)], list(families))
372
373 def test_empty_label(self):
374 families = text_string_to_metric_families("""# TYPE a counter
375 # HELP a help
376 a_total{foo="bar"} 1
377 a_total{foo=""} 2
378 # EOF
379 """)
380 metric_family = CounterMetricFamily("a", "help", labels=["foo"])
381 metric_family.add_metric(["bar"], 1)
382 metric_family.add_metric([""], 2)
383 self.assertEqual([metric_family], list(families))
384
385 def test_label_escaping(self):
386 for escaped_val, unescaped_val in [('foo', 'foo'), ('\\foo', '\\foo'), ('\\\\foo', '\\foo'),
387 ('foo\\\\', 'foo\\'), ('\\\\', '\\'), ('\\n', '\n'),
388 ('\\\\n', '\\n'), ('\\\\\\n', '\\\n'), ('\\"', '"'), ('\\\\\\"', '\\"')]:
389 families = list(text_string_to_metric_families("""# TYPE a counter
390 # HELP a help
391 a_total{foo="%s",bar="baz"} 1
392 # EOF
393 """ % escaped_val))
394 metric_family = CounterMetricFamily(
395 "a", "help", labels=["foo", "bar"])
396 metric_family.add_metric([unescaped_val, "baz"], 1)
397 self.assertEqual([metric_family], list(families))
398
399 def test_help_escaping(self):
400 for escaped_val, unescaped_val in [
401 ('foo', 'foo'),
402 ('\\foo', '\\foo'),
403 ('\\\\foo', '\\foo'),
404 ('foo\\', 'foo\\'),
405 ('foo\\\\', 'foo\\'),
406 ('\\n', '\n'),
407 ('\\\\n', '\\n'),
408 ('\\\\\\n', '\\\n'),
409 ('\\"', '"'),
410 ('\\\\"', '\\"'),
411 ('\\\\\\"', '\\"')]:
412 families = list(text_string_to_metric_families("""# TYPE a counter
413 # HELP a %s
414 a_total{foo="bar"} 1
415 # EOF
416 """ % escaped_val))
417 metric_family = CounterMetricFamily("a", unescaped_val, labels=["foo"])
418 metric_family.add_metric(["bar"], 1)
419 self.assertEqual([metric_family], list(families))
420
421 def test_escaping(self):
422 families = text_string_to_metric_families("""# TYPE a counter
423 # HELP a he\\n\\\\l\\tp
424 a_total{foo="b\\"a\\nr"} 1
425 a_total{foo="b\\\\a\\z"} 2
426 a_total{foo="b\\"a\\nr # "} 3
427 a_total{foo="b\\\\a\\z # "} 4
428 # EOF
429 """)
430 metric_family = CounterMetricFamily("a", "he\n\\l\\tp", labels=["foo"])
431 metric_family.add_metric(["b\"a\nr"], 1)
432 metric_family.add_metric(["b\\a\\z"], 2)
433 metric_family.add_metric(["b\"a\nr # "], 3)
434 metric_family.add_metric(["b\\a\\z # "], 4)
435 self.assertEqual([metric_family], list(families))
436
437 def test_null_byte(self):
438 families = text_string_to_metric_families("""# TYPE a counter
439 # HELP a he\0lp
440 # EOF
441 """)
442 metric_family = CounterMetricFamily("a", "he\0lp")
443 self.assertEqual([metric_family], list(families))
444
445 def test_timestamps(self):
446 families = text_string_to_metric_families("""# TYPE a counter
447 # HELP a help
448 a_total{foo="1"} 1 000
449 a_total{foo="2"} 1 0.0
450 a_total{foo="3"} 1 1.1
451 a_total{foo="4"} 1 12345678901234567890.1234567890
452 a_total{foo="5"} 1 1.5e3
453 # TYPE b counter
454 # HELP b help
455 b_total 2 1234567890
456 # EOF
457 """)
458 a = CounterMetricFamily("a", "help", labels=["foo"])
459 a.add_metric(["1"], 1, timestamp=Timestamp(0, 0))
460 a.add_metric(["2"], 1, timestamp=Timestamp(0, 0))
461 a.add_metric(["3"], 1, timestamp=Timestamp(1, 100000000))
462 a.add_metric(["4"], 1, timestamp=Timestamp(12345678901234567890, 123456789))
463 a.add_metric(["5"], 1, timestamp=1500.0)
464 b = CounterMetricFamily("b", "help")
465 b.add_metric([], 2, timestamp=Timestamp(1234567890, 0))
466 self.assertEqual([a, b], list(families))
467
468 def test_hash_in_label_value(self):
469 families = text_string_to_metric_families("""# TYPE a counter
470 # HELP a help
471 a_total{foo="foo # bar"} 1
472 a_total{foo="} foo # bar # "} 1
473 # EOF
474 """)
475 a = CounterMetricFamily("a", "help", labels=["foo"])
476 a.add_metric(["foo # bar"], 1)
477 a.add_metric(["} foo # bar # "], 1)
478 self.assertEqual([a], list(families))
479
480 def test_exemplars_with_hash_in_label_values(self):
481 families = text_string_to_metric_families("""# TYPE a histogram
482 # HELP a help
483 a_bucket{le="1.0",foo="bar # "} 0 # {a="b",foo="bar # bar"} 0.5
484 a_bucket{le="2.0",foo="bar # "} 2 # {a="c",foo="bar # bar"} 0.5
485 a_bucket{le="+Inf",foo="bar # "} 3 # {a="d",foo="bar # bar"} 4
486 # EOF
487 """)
488 hfm = HistogramMetricFamily("a", "help")
489 hfm.add_sample("a_bucket", {"le": "1.0", "foo": "bar # "}, 0.0, None, Exemplar({"a": "b", "foo": "bar # bar"}, 0.5))
490 hfm.add_sample("a_bucket", {"le": "2.0", "foo": "bar # "}, 2.0, None, Exemplar({"a": "c", "foo": "bar # bar"}, 0.5))
491 hfm.add_sample("a_bucket", {"le": "+Inf", "foo": "bar # "}, 3.0, None, Exemplar({"a": "d", "foo": "bar # bar"}, 4))
492 self.assertEqual([hfm], list(families))
493
494 def test_fallback_to_state_machine_label_parsing(self):
495 from unittest.mock import patch
496
497 from prometheus_client.openmetrics.parser import _parse_sample
498
499 parse_sample_function = "prometheus_client.openmetrics.parser._parse_sample"
500 parse_labels_function = "prometheus_client.openmetrics.parser._parse_labels"
501 parse_remaining_function = "prometheus_client.openmetrics.parser._parse_remaining_text"
502 state_machine_function = "prometheus_client.openmetrics.parser._parse_labels_with_state_machine"
503
504 parse_sample_return_value = Sample("a_total", {"foo": "foo # bar"}, 1)
505 with patch(parse_sample_function, return_value=parse_sample_return_value) as mock:
506 families = text_string_to_metric_families("""# TYPE a counter
507 # HELP a help
508 a_total{foo="foo # bar"} 1
509 # EOF
510 """)
511 a = CounterMetricFamily("a", "help", labels=["foo"])
512 a.add_metric(["foo # bar"], 1)
513 self.assertEqual([a], list(families))
514 mock.assert_called_once_with('a_total{foo="foo # bar"} 1')
515
516 # First fallback case
517 state_machine_return_values = [{"foo": "foo # bar"}, len('foo="foo # bar"}')]
518 parse_remaining_values = [1, None, None]
519 with patch(parse_labels_function) as mock1:
520 with patch(state_machine_function, return_value=state_machine_return_values) as mock2:
521 with patch(parse_remaining_function, return_value=parse_remaining_values) as mock3:
522 sample = _parse_sample('a_total{foo="foo # bar"} 1')
523 s = Sample("a_total", {"foo": "foo # bar"}, 1)
524 self.assertEqual(s, sample)
525 mock1.assert_not_called()
526 mock2.assert_called_once_with('foo="foo # bar"} 1')
527 mock3.assert_called_once_with('1')
528
529 # Second fallback case
530 state_machine_return_values = [{"le": "1.0"}, len('le="1.0"}')]
531 parse_remaining_values = [0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5)]
532 with patch(parse_labels_function) as mock1:
533 with patch(state_machine_function, return_value=state_machine_return_values) as mock2:
534 with patch(parse_remaining_function, return_value=parse_remaining_values) as mock3:
535 sample = _parse_sample('a_bucket{le="1.0"} 0 123 # {a="b"} 0.5')
536 s = Sample("a_bucket", {"le": "1.0"}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
537 self.assertEqual(s, sample)
538 mock1.assert_not_called()
539 mock2.assert_called_once_with('le="1.0"} 0 123 # {a="b"} 0.5')
540 mock3.assert_called_once_with('0 123 # {a="b"} 0.5')
541
542 # No need to fallback case
543 parse_labels_return_values = {"foo": "foo#bar"}
544 parse_remaining_values = [1, None, None]
545 with patch(parse_labels_function, return_value=parse_labels_return_values) as mock1:
546 with patch(state_machine_function) as mock2:
547 with patch(parse_remaining_function, return_value=parse_remaining_values) as mock3:
548 sample = _parse_sample('a_total{foo="foo#bar"} 1')
549 s = Sample("a_total", {"foo": "foo#bar"}, 1)
550 self.assertEqual(s, sample)
551 mock1.assert_called_once_with('foo="foo#bar"')
552 mock2.assert_not_called()
553 mock3.assert_called_once_with('1')
554
555 def test_roundtrip(self):
556 text = """# HELP go_gc_duration_seconds A summary of the GC invocation durations.
557 # TYPE go_gc_duration_seconds summary
558 go_gc_duration_seconds{quantile="0.0"} 0.013300656000000001
559 go_gc_duration_seconds{quantile="0.25"} 0.013638736
560 go_gc_duration_seconds{quantile="0.5"} 0.013759906
561 go_gc_duration_seconds{quantile="0.75"} 0.013962066
562 go_gc_duration_seconds{quantile="1.0"} 0.021383540000000003
563 go_gc_duration_seconds_sum 56.12904785
564 go_gc_duration_seconds_count 7476.0
565 # HELP go_goroutines Number of goroutines that currently exist.
566 # TYPE go_goroutines gauge
567 go_goroutines 166.0
568 # HELP prometheus_local_storage_indexing_batch_duration_milliseconds Quantiles for batch indexing duration in milliseconds.
569 # TYPE prometheus_local_storage_indexing_batch_duration_milliseconds summary
570 prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.5"} NaN
571 prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.9"} NaN
572 prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.99"} NaN
573 prometheus_local_storage_indexing_batch_duration_milliseconds_sum 871.5665949999999
574 prometheus_local_storage_indexing_batch_duration_milliseconds_count 229.0
575 # HELP process_cpu_seconds Total user and system CPU time spent in seconds.
576 # TYPE process_cpu_seconds counter
577 process_cpu_seconds_total 29323.4
578 # HELP process_virtual_memory_bytes Virtual memory size in bytes.
579 # TYPE process_virtual_memory_bytes gauge
580 process_virtual_memory_bytes 2.478268416e+09
581 # HELP prometheus_build_info A metric with a constant '1' value labeled by version, revision, and branch from which Prometheus was built.
582 # TYPE prometheus_build_info gauge
583 prometheus_build_info{branch="HEAD",revision="ef176e5",version="0.16.0rc1"} 1.0
584 # HELP prometheus_local_storage_chunk_ops The total number of chunk operations by their type.
585 # TYPE prometheus_local_storage_chunk_ops counter
586 prometheus_local_storage_chunk_ops_total{type="clone"} 28.0
587 prometheus_local_storage_chunk_ops_total{type="create"} 997844.0
588 prometheus_local_storage_chunk_ops_total{type="drop"} 1.345758e+06
589 prometheus_local_storage_chunk_ops_total{type="load"} 1641.0
590 prometheus_local_storage_chunk_ops_total{type="persist"} 981408.0
591 prometheus_local_storage_chunk_ops_total{type="pin"} 32662.0
592 prometheus_local_storage_chunk_ops_total{type="transcode"} 980180.0
593 prometheus_local_storage_chunk_ops_total{type="unpin"} 32662.0
594 # HELP foo histogram Testing histogram buckets
595 # TYPE foo histogram
596 foo_bucket{le="0.0"} 0.0
597 foo_bucket{le="1e-05"} 0.0
598 foo_bucket{le="0.0001"} 0.0
599 foo_bucket{le="0.1"} 8.0
600 foo_bucket{le="1.0"} 10.0
601 foo_bucket{le="10.0"} 17.0
602 foo_bucket{le="100000.0"} 17.0
603 foo_bucket{le="1e+06"} 17.0
604 foo_bucket{le="1.55555555555552e+06"} 17.0
605 foo_bucket{le="1e+23"} 17.0
606 foo_bucket{le="+Inf"} 17.0
607 foo_count 17.0
608 foo_sum 324789.3
609 foo_created 1.520430000123e+09
610 # HELP bar histogram Testing with labels
611 # TYPE bar histogram
612 bar_bucket{a="b",le="+Inf"} 0.0
613 bar_bucket{a="c",le="+Inf"} 0.0
614 # EOF
615 """
616 families = list(text_string_to_metric_families(text))
617
618 class TextCollector:
619 def collect(self):
620 return families
621
622 registry = CollectorRegistry()
623 registry.register(TextCollector())
624 self.assertEqual(text.encode('utf-8'), generate_latest(registry))
625
626 def test_invalid_input(self):
627 for case in [
628 # No EOF.
629 (''),
630 # Blank line
631 ('a 1\n\n# EOF\n'),
632 # Text after EOF.
633 ('a 1\n# EOF\nblah'),
634 ('a 1\n# EOFblah'),
635 # Missing or wrong quotes on label value.
636 ('a{a=1} 1\n# EOF\n'),
637 ('a{a="1} 1\n# EOF\n'),
638 ('a{a=\'1\'} 1\n# EOF\n'),
639 # Missing equal or label value.
640 ('a{a} 1\n# EOF\n'),
641 ('a{a"value"} 1\n# EOF\n'),
642 ('a{a""} 1\n# EOF\n'),
643 ('a{a=} 1\n# EOF\n'),
644 ('a{a="} 1\n# EOF\n'),
645 # Missing delimiters.
646 ('a{a="1"}1\n# EOF\n'),
647 # Missing or extra commas.
648 ('a{a="1"b="2"} 1\n# EOF\n'),
649 ('a{a="1",,b="2"} 1\n# EOF\n'),
650 ('a{a="1",b="2",} 1\n# EOF\n'),
651 # Invalid labels.
652 ('a{1="1"} 1\n# EOF\n'),
653 ('a{1="1"}1\n# EOF\n'),
654 ('a{a="1",a="1"} 1\n# EOF\n'),
655 ('a{a="1"b} 1\n# EOF\n'),
656 ('a{1=" # "} 1\n# EOF\n'),
657 ('a{a=" # ",a=" # "} 1\n# EOF\n'),
658 ('a{a=" # "}1\n# EOF\n'),
659 ('a{a=" # ",b=}1\n# EOF\n'),
660 ('a{a=" # "b}1\n# EOF\n'),
661 # Missing value.
662 ('a\n# EOF\n'),
663 ('a \n# EOF\n'),
664 # Bad HELP.
665 ('# HELP\n# EOF\n'),
666 ('# HELP \n# EOF\n'),
667 ('# HELP a\n# EOF\n'),
668 ('# HELP a\t\n# EOF\n'),
669 (' # HELP a meh\n# EOF\n'),
670 # Bad TYPE.
671 ('# TYPE\n# EOF\n'),
672 ('# TYPE \n# EOF\n'),
673 ('# TYPE a\n# EOF\n'),
674 ('# TYPE a\t\n# EOF\n'),
675 ('# TYPE a meh\n# EOF\n'),
676 ('# TYPE a meh \n# EOF\n'),
677 ('# TYPE a gauge \n# EOF\n'),
678 ('# TYPE a untyped\n# EOF\n'),
679 # Bad UNIT.
680 ('# UNIT\n# EOF\n'),
681 ('# UNIT \n# EOF\n'),
682 ('# UNIT a\n# EOF\n'),
683 ('# UNIT a\t\n# EOF\n'),
684 ('# UNIT a seconds\n# EOF\n'),
685 ('# UNIT a_seconds seconds \n# EOF\n'),
686 ('# TYPE x_u info\n# UNIT x_u u\n# EOF\n'),
687 ('# TYPE x_u stateset\n# UNIT x_u u\n# EOF\n'),
688 # Metadata in wrong place.
689 ('# HELP a x\na 1\n# TYPE a gauge\n# EOF\n'),
690 ('# TYPE a gauge\na 1\n# HELP a gauge\n# EOF\n'),
691 ('# TYPE a_s gauge\na_s 1\n# UNIT a_s s\n# EOF\n'),
692 # Repeated metadata.
693 ('# HELP a \n# HELP a \n# EOF\n'),
694 ('# HELP a x\n# HELP a x\n# EOF\n'),
695 ('# TYPE a untyped\n# TYPE a untyped\n# EOF\n'),
696 ('# UNIT a_s s\n# UNIT a_s s\n# EOF\n'),
697 # Bad metadata.
698 ('# FOO a x\n# EOF\n'),
699 # Bad metric names.
700 ('0a 1\n# EOF\n'),
701 ('a.b 1\n# EOF\n'),
702 ('a-b 1\n# EOF\n'),
703 # Bad value.
704 ('a a\n# EOF\n'),
705 ('a 1\n# EOF\n'),
706 ('a 1\t\n# EOF\n'),
707 ('a 1 \n# EOF\n'),
708 ('a 1_2\n# EOF\n'),
709 ('a 0x1p-3\n# EOF\n'),
710 ('a 0x1P-3\n# EOF\n'),
711 ('a 0b1\n# EOF\n'),
712 ('a 0B1\n# EOF\n'),
713 ('a 0x1\n# EOF\n'),
714 ('a 0X1\n# EOF\n'),
715 ('a 0o1\n# EOF\n'),
716 ('a 0O1\n# EOF\n'),
717 # Bad timestamp.
718 ('a 1 z\n# EOF\n'),
719 ('a 1 1z\n# EOF\n'),
720 ('a 1 1_2\n# EOF\n'),
721 ('a 1 1.1.1\n# EOF\n'),
722 ('a 1 NaN\n# EOF\n'),
723 ('a 1 Inf\n# EOF\n'),
724 ('a 1 +Inf\n# EOF\n'),
725 ('a 1 -Inf\n# EOF\n'),
726 ('a 1 0x1p-3\n# EOF\n'),
727 # Bad exemplars.
728 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 #\n# EOF\n'),
729 ('# TYPE a histogram\na_bucket{le="+Inf"} 1# {} 1\n# EOF\n'),
730 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 #{} 1\n# EOF\n'),
731 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {}1\n# EOF\n'),
732 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 \n# EOF\n'),
733 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 1 \n# EOF\n'),
734 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # '
735 '{a="23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"} 1 1\n# EOF\n'),
736 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 0x1p-3\n# EOF\n'),
737 ('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 0x1p-3\n# EOF\n'),
738 ('# TYPE a counter\na_total 1 1 # {id="a"} \n# EOF\n'),
739 ('# TYPE a counter\na_total 1 1 # id="a"} 1\n# EOF\n'),
740 ('# TYPE a counter\na_total 1 1 #id=" # "} 1\n# EOF\n'),
741 ('# TYPE a counter\na_total 1 1 id=" # "} 1\n# EOF\n'),
742 # Exemplars on unallowed samples.
743 ('# TYPE a histogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
744 ('# TYPE a gaugehistogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
745 ('# TYPE a_bucket gauge\na_bucket 1 # {a="b"} 0.5\n# EOF\n'),
746 ('# TYPE a counter\na_created 1 # {a="b"} 0.5\n# EOF\n'),
747 # Exemplars on unallowed metric types.
748 ('# TYPE a gauge\na 1 # {a="b"} 1\n# EOF\n'),
749 ('# TYPE a info\na_info 1 # {a="b"} 1\n# EOF\n'),
750 ('# TYPE a stateset\na{a="b"} 1 # {c="d"} 1\n# EOF\n'),
751 # Bad stateset/info values.
752 ('# TYPE a stateset\na 2\n# EOF\n'),
753 ('# TYPE a info\na 2\n# EOF\n'),
754 ('# TYPE a stateset\na 2.0\n# EOF\n'),
755 ('# TYPE a info\na 2.0\n# EOF\n'),
756 # Missing or invalid labels for a type.
757 ('# TYPE a summary\na 0\n# EOF\n'),
758 ('# TYPE a summary\na{quantile="-1"} 0\n# EOF\n'),
759 ('# TYPE a summary\na{quantile="foo"} 0\n# EOF\n'),
760 ('# TYPE a summary\na{quantile="1.01"} 0\n# EOF\n'),
761 ('# TYPE a summary\na{quantile="NaN"} 0\n# EOF\n'),
762 ('# TYPE a histogram\na_bucket 0\n# EOF\n'),
763 ('# TYPE a gaugehistogram\na_bucket 0\n# EOF\n'),
764 ('# TYPE a stateset\na 0\n# EOF\n'),
765 # Bad counter values.
766 ('# TYPE a counter\na_total NaN\n# EOF\n'),
767 ('# TYPE a counter\na_total -1\n# EOF\n'),
768 ('# TYPE a histogram\na_sum NaN\n# EOF\n'),
769 ('# TYPE a histogram\na_count NaN\n# EOF\n'),
770 ('# TYPE a histogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
771 ('# TYPE a histogram\na_sum -1\n# EOF\n'),
772 ('# TYPE a histogram\na_count -1\n# EOF\n'),
773 ('# TYPE a histogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
774 ('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum -1\n# EOF\n'),
775 ('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum 1\n# EOF\n'),
776 ('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
777 ('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\na_count 0.5\na_sum 0\n# EOF\n'),
778 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
779 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\na_gcount -1\n# EOF\n'),
780 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
781 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\n# EOF\n'),
782 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum NaN\n# EOF\n'),
783 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
784 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\na_gsum 0.5\na_gcount 0\n# EOF\n'),
785 ('# TYPE a summary\na_sum NaN\n# EOF\n'),
786 ('# TYPE a summary\na_count NaN\n# EOF\n'),
787 ('# TYPE a summary\na_sum -1\n# EOF\n'),
788 ('# TYPE a summary\na_count -1\n# EOF\n'),
789 ('# TYPE a summary\na_count 0.5\n# EOF\n'),
790 ('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
791 # Bad info and stateset values.
792 ('# TYPE a info\na_info{foo="bar"} 2\n# EOF\n'),
793 ('# TYPE a stateset\na{a="bar"} 2\n# EOF\n'),
794 # Bad histograms.
795 ('# TYPE a histogram\na_sum 1\n# EOF\n'),
796 ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_sum 0\n# EOF\n'),
797 ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 0\n# EOF\n'),
798 ('# TYPE a histogram\na_bucket{le="-1"} 0\na_bucket{le="+Inf"} 0\na_sum 0\na_count 0\n# EOF\n'),
799 ('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
800 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gsum 0\n# EOF\n'),
801 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gcount 0\n# EOF\n'),
802 ('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\na_gcount 1\n# EOF\n'),
803 ('# TYPE a histogram\na_count 1\na_bucket{le="+Inf"} 0\n# EOF\n'),
804 ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'),
805 ('# TYPE a histogram\na_bucket{le="+INF"} 0\n# EOF\n'),
806 ('# TYPE a histogram\na_bucket{le="2"} 0\na_bucket{le="1"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'),
807 ('# TYPE a histogram\na_bucket{le="1"} 1\na_bucket{le="2"} 1\na_bucket{le="+Inf"} 0\n# EOF\n'),
808 # Bad grouping or ordering.
809 ('# TYPE a histogram\na_sum{a="1"} 0\na_sum{a="2"} 0\na_count{a="1"} 0\n# EOF\n'),
810 ('# TYPE a histogram\na_bucket{a="1",le="1"} 0\na_bucket{a="2",le="+Inf""} '
811 '0\na_bucket{a="1",le="+Inf"} 0\n# EOF\n'),
812 ('# TYPE a gaugehistogram\na_gsum{a="1"} 0\na_gsum{a="2"} 0\na_gcount{a="1"} 0\n# EOF\n'),
813 ('# TYPE a summary\nquantile{quantile="0"} 0\na_sum{a="1"} 0\nquantile{quantile="1"} 0\n# EOF\n'),
814 ('# TYPE a gauge\na 0 -1\na 0 -2\n# EOF\n'),
815 ('# TYPE a gauge\na 0 -1\na 0 -1.1\n# EOF\n'),
816 ('# TYPE a gauge\na 0 1\na 0 -1\n# EOF\n'),
817 ('# TYPE a gauge\na 0 1.1\na 0 1\n# EOF\n'),
818 ('# TYPE a gauge\na 0 1\na 0 0\n# EOF\n'),
819 ('# TYPE a gauge\na 0\na 0 0\n# EOF\n'),
820 ('# TYPE a gauge\na 0 0\na 0\n# EOF\n'),
821 # Clashing names.
822 ('# TYPE a counter\n# TYPE a counter\n# EOF\n'),
823 ('# TYPE a info\n# TYPE a counter\n# EOF\n'),
824 ('# TYPE a_created gauge\n# TYPE a counter\n# EOF\n'),
825 ]:
826 with self.assertRaises(ValueError, msg=case):
827 list(text_string_to_metric_families(case))
828
829
830 if __name__ == '__main__':
831 unittest.main()