8000 Check for invalid info/stateset values. · ethervoid/client_python@addb28c · GitHub
[go: up one dir, main page]

Skip to content

Commit addb28c

Browse files
committed
Check for invalid info/stateset values.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 0b4e983 commit addb28c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ def build_metric(name, documentation, typ, unit, samples):
257257
metric = core.Metric(name, documentation, typ, unit)
258258
# TODO: check labelvalues are valid utf8
259259
# TODO: check samples are appropriately grouped and ordered
260-
# TODO: check info/stateset values are 1/0
261260
# TODO: Check histogram bucket rules being followed
262261
# TODO: Check for dupliate samples
263262
# TODO: Check for decresing timestamps
@@ -329,6 +328,10 @@ def build_metric(name, documentation, typ, unit, samples):
329328
allowed_names = [sample.name]
330329
else:
331330
samples.append(sample)
331+
if typ == 'stateset' and sample.value not in [0, 1]:
332+
raise ValueError("Stateset samples can only have values zero and one: " + line)
333+
if typ == 'info' and sample.value != 1:
334+
raise ValueError("Info samples can only have value one: " + line)
332335
if sample.exemplar and not (
333336
typ in ['histogram', 'gaugehistogram']
334337
and sample.name.endswith('_bucket')):

tests/openmetrics/test_parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_simple_stateset(self):
160160
families = text_string_to_metric_families("""# TYPE a stateset
161161
# HELP a help
162162
a{a="bar"} 0
163-
a{a="foo"} 1
163+
a{a="foo"} 1.0
164164
# EOF
165165
""")
166166
self.assertEqual([StateSetMetricFamily("a", "help", {'foo': True, 'bar': False})], list(families))
@@ -464,6 +464,11 @@ def test_invalid_input(self):
464464
('# TYPE a histogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
465465
('# TYPE a gaugehistogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
466466
('# TYPE a_bucket gauge\na_bucket 1 # {a="b"} 0.5\n# EOF\n'),
467+
# Bad stateset/info values.
468+
('# TYPE a stateset\na 2\n# EOF\n'),
469+
('# TYPE a info\na 2\n# EOF\n'),
470+
('# TYPE a stateset\na 2.0\n# EOF\n'),
471+
('# TYPE a info\na 2.0\n# EOF\n'),
467472
]:
468473
with self.assertRaises(ValueError):
469474
list(text_string_to_metric_families(case))

0 commit comments

Comments
 (0)
0