8000 Make OM code effectively 100% covered. (#602) · Global-localhost/client_python@9df3bd1 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit 9df3bd1

Browse files
authored
Make OM code effectively 100% covered. (prometheus#602)
Also remove some dead code. The untaken branches claimed by coverage are due to loops that always iterate at least once, or because state machines only ever have known states. Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent bae17eb commit 9df3bd1

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,7 @@ def build_metric(name, documentation, typ, unit, samples):
529529
if parts[1] == 'HELP':
530530
if documentation is not None:
531531
raise ValueError("More than one HELP for metric: " + line)
532-
if len(parts) == 4:
533-
documentation = _unescape_help(parts[3])
534-
elif len(parts) == 3:
535-
raise ValueError("Invalid line: " + line)
532+
documentation = _unescape_help(parts[3])
536533
elif parts[1] == 'TYPE':
537534
if typ is not None:
538535
raise ValueError("More than one TYPE for metric: " + line)

tests/openmetrics/test_exposition.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def test_counter_total(self):
4848
self.assertEqual(b'# HELP cc A counter\n# TYPE cc counter\ncc_total 1.0\ncc_created 123.456\n# EOF\n',
4949
generate_latest(self.registry))
5050

51+
def test_counter_unit(self):
52+
c = Counter('cc_seconds', 'A counter', registry=self.registry, unit="seconds")
53+
c.inc()
54+
self.assertEqual(b'# HELP cc_seconds A counter\n# TYPE cc_seconds counter\n# UNIT cc_seconds seconds\ncc_seconds_total 1.0\ncc_seconds_created 123.456\n# EOF\n',
55+
generate_latest(self.registry))
56+
5157
def test_gauge(self):
5258
g = Gauge('gg', 'A gauge', registry=self.registry)
5359
g.set(17)

tests/openmetrics/test_parser.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ def test_counter_exemplars(self):
216216
# HELP a help
217217
a_total 0 123 # {a="b"} 0.5
218218
# EOF
219+
""")
220+
cfm = CounterMetricFamily("a", "help")
221+
cfm.add_sample("a_total", {}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
222+
self.assertEqual([cfm], list(families))
223+
224+
def test_counter_exemplars_empty_brackets(self):
225+
families = text_string_to_metric_families("""# TYPE a counter
226+
# HELP a help
227+
a_total{} 0 123 # {a="b"} 0.5
228+
# EOF
219229
""")
220230
cfm = CounterMetricFamily("a", "help")
221231
cfm.add_sample("a_total", {}, 0.0, Timestamp(123, 0), Exemplar({"a": "b"}, 0.5))
@@ -409,11 +419,15 @@ def test_escaping(self):
409419
# HELP a he\\n\\\\l\\tp
410420
a_total{foo="b\\"a\\nr"} 1
411421
a_total{foo="b\\\\a\\z"} 2
422+
a_total{foo="b\\"a\\nr # "} 3
423+
a_total{foo="b\\\\a\\z # "} 4
412424
# EOF
413425
""")
414426
metric_family = CounterMetricFamily("a", "he\n\\l\\tp", labels=["foo"])
415427
metric_family.add_metric(["b\"a\nr"], 1)
416428
metric_family.add_metric(["b\\a\\z"], 2)
429+
metric_family.add_metric(["b\"a\nr # "], 3)
430+
metric_family.add_metric(["b\\a\\z # "], 4)
417431
self.assertEqual([metric_family], list(families))
418432

419433
def test_null_byte(self):
@@ -591,6 +605,10 @@ def test_roundtrip(self):
591605
foo_count 17.0
592606
foo_sum 324789.3
593607
foo_created 1.520430000123e+09
608+
# HELP bar histogram Testing with labels
609+
# TYPE bar histogram
610+
bar_bucket{a="b",le="+Inf"} 0.0
611+
bar_bucket{a="c",le="+Inf"} 0.0
594612
# EOF
595613
"""
596614
families = list(text_string_to_metric_families(text))
@@ -628,9 +646,14 @@ def test_invalid_input(self):
628646
('a{a="1",b="2",} 1\n# EOF\n'),
629647
# Invalid labels.
630648
('a{1="1"} 1\n# EOF\n'),
649+
('a{1="1"}1\n# EOF\n'),
631650
('a{a="1",a="1"} 1\n# EOF\n'),
651+
('a{a="1"b} 1\n# EOF\n'),
632652
('a{1=" # "} 1\n# EOF\n'),
633653
('a{a=" # ",a=" # "} 1\n# EOF\n'),
654+
('a{a=" # "}1\n# EOF\n'),
655+
('a{a=" # ",b=}1\n# EOF\n'),
656+
('a{a=" # "b}1\n# EOF\n'),
634657
# Missing value.
635658
('a\n# EOF\n'),
636659
('a \n# EOF\n'),
@@ -667,6 +690,8 @@ def test_invalid_input(self):
667690
('# HELP a x\n# HELP a x\n# EOF\n'),
668691
('# TYPE a untyped\n# TYPE a untyped\n# EOF\n'),
669692
('# UNIT a_s s\n# UNIT a_s s\n# EOF\n'),
693+
# Bad metadata.
694+
('# FOO a x\n# EOF\n'),
670695
# Bad metric names.
671696
('0a 1\n# EOF\n'),
672697
('a.b 1\n# EOF\n'),
@@ -706,6 +731,10 @@ def test_invalid_input(self):
706731
'{a="23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"} 1 1\n# EOF\n'),
707732
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 0x1p-3\n# EOF\n'),
708733
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 0x1p-3\n# EOF\n'),
734+
('# TYPE a counter\na_total 1 1 # {id="a"} \n# EOF\n'),
735+
('# TYPE a counter\na_total 1 1 # id="a"} 1\n# EOF\n'),
736+
('# TYPE a counter\na_total 1 1 #id=" # "} 1\n# EOF\n'),
737+
('# TYPE a counter\na_total 1 1 id=" # "} 1\n# EOF\n'),
709738
# Exemplars on unallowed samples.
710739
('# TYPE a histogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
711740
('# TYPE a gaugehistogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
@@ -750,13 +779,18 @@ def test_invalid_input(self):
750779
('# TYPE a summary\na_sum -1\n# EOF\n'),
751780
('# TYPE a summary\na_count -1\n# EOF\n'),
752781
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
782+
# Bad info and stateset values.
783+
('# TYPE a info\na_info{foo="bar"} 2\n# EOF\n'),
784+
('# TYPE a stateset\na{a="bar"} 2\n# EOF\n'),
753785
# Bad histograms.
754786
('# TYPE a histogram\na_sum 1\n# EOF\n'),
755-
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_sum 0\n# EOF\n'),
756-
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_count 0\n# EOF\n'),
787+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_sum 0\n# EOF\n'),
788+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 0\n# EOF\n'),
789+
('# TYPE a histogram\na_bucket{le="-1"} 0\na_bucket{le="+Inf"} 0\na_sum 0\na_count 0\n# EOF\n'),
757790
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
758791
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gsum 0\n# EOF\n'),
759792
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gcount 0\n# EOF\n'),
793+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\na_gcount 1\n# EOF\n'),
760794
('# TYPE a histogram\na_count 1\na_bucket{le="+Inf"} 0\n# EOF\n'),
761795
('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'),
762796
('# TYPE a histogram\na_bucket{le="+INF"} 0\n# EOF\n'),

0 commit comments

Comments
 (0)
0