10000 Check for NaN counter values · sxlstevengit/client_python@e8c3daa · GitHub
[go: up one dir, main page]

Skip to content

Commit e8c3daa

Browse files
committed
Check for NaN counter values
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent c8c5a02 commit e8c3daa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import unicode_literals
44

5+
import math
6+
57
try:
68
import StringIO
79
except ImportError:
@@ -328,10 +330,13 @@ def build_metric(name, documentation, typ, unit, samples):
328330
allowed_names = [sample.name]
329331
else:
330332
samples.append(sample)
333+
331334
if typ == 'stateset' and sample.value not in [0, 1]:
332335
raise ValueError("Stateset samples can only have values zero and one: " + line)
333336
if typ == 'info' and sample.value != 1:
334337
raise ValueError("Info samples can only have value one: " + line)
338+
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket'] and math.isnan(sample.value):
339+
raise ValueError("Counter-like samples cannot be NaN: " + line)
335340
if sample.exemplar and not (
336341
typ in ['histogram', 'gaugehistogram']
337342
and sample.name.endswith('_bucket')):

tests/openmetrics/test_parser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def test_float_gauge(self):
6565
""")
6666
self.assertEqual([GaugeMetricFamily("a", "help", value=1.2)], list(families))
6767

68+
def test_nan_gauge(self):
69+
families = text_string_to_metric_families("""# TYPE a gauge
70+
# HELP a help
71+
a NaN
72+
# EOF
73+
""")
74+
self.assertTrue(math.isnan(list(families)[0].samples[0].value))
75+
6876
def test_unit_gauge(self):
6977
families = text_string_to_metric_families("""# TYPE a_seconds gauge
7078
# UNIT a_seconds seconds
@@ -485,6 +493,14 @@ def test_invalid_input(self):
485493
('# TYPE a info\na 2\n# EOF\n'),
486494
('# TYPE a stateset\na 2.0\n# EOF\n'),
487495
('# TYPE a info\na 2.0\n# EOF\n'),
496+
# Bad counter values.
497+
('# TYPE a counter\na_total NaN\n# EOF\n'),
498+
('# TYPE a histogram\na_sum NaN\n# EOF\n'),
499+
('# TYPE a histogram\na_count NaN\n# EOF\n'),
500+
('# TYPE a histogram\na_bucket NaN\n# EOF\n'),
501+
('# TYPE a gaugehistogram\na_bucket NaN\n# EOF\n'),
502+
('# TYPE a summary\na_sum NaN\n# EOF\n'),
503+
('# TYPE a summary\na_count NaN\n# EOF\n'),
488504
]:
489505
with self.assertRaises(ValueError):
490506
list(text_string_to_metric_families(case))

0 commit comments

Comments
 (0)
0