8000 Catch non-integer bucket/count values. (#726) · barbdowns/client_python@ff19604 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff19604

Browse files
authored
Catch non-integer bucket/count values. (prometheus#726)
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 038b5bd commit ff19604

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,16 @@ def build_metric(name, documentation, typ, unit, samples):
560560

561561
if typ == 'stateset' and name not in sample.labels:
562562
raise ValueError("Stateset missing label: " + line)
563-
if (typ in ['histogram', 'gaugehistogram'] and name + '_bucket' == sample.name
563+
if (name + '_bucket' == sample.name
564564
and (sample.labels.get('le', "NaN") == "NaN"
565565
or _isUncanonicalNumber(sample.labels['le']))):
566566
raise ValueError("Invalid le label: " + line)
567+
if (name + '_bucket' == sample.name
568+
and (not isinstance(sample.value, int) and not sample.value.is_integer())):
569+
raise ValueError("Bucket value must be an integer: " + line)
570+
if ((name + '_count' == sample.name or name + '_gcount' == sample.name)
571+
and (not isinstance(sample.value, int) and not sample.value.is_integer())):
572+
raise ValueError("Count value must be an integer: " + line)
567573
if (typ == 'summary' and name == sample.name
568574
and (not (0 <= float(sample.labels.get('quantile', -1)) <= 1)
569575
or _isUncanonicalNumber(sample.labels['quantile']))):

tests/openmetrics/test_parser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ def test_simple_histogram(self):
112112
a_count 3
113113
a_sum 2
114114
# 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
115127
""")
116128
self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
117129
list(families))
@@ -759,15 +771,20 @@ def test_invalid_input(self):
759771
('# TYPE a histogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
760772
('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum -1\n# EOF\n'),
761773
('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum 1\n# EOF\n'),
774+
('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
775+
('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\na_count 0.5\na_sum 0\n# EOF\n'),
762776
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
763777
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\na_gcount -1\n# EOF\n'),
764778
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
765779
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\n# EOF\n'),
766780
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum NaN\n# EOF\n'),
781+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
782+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\na_gsum 0.5\na_gcount 0\n# EOF\n'),
767783
('# TYPE a summary\na_sum NaN\n# EOF\n'),
768784
('# TYPE a summary\na_count NaN\n# EOF\n'),
769785
('# TYPE a summary\na_sum -1\n# EOF\n'),
770786
('# TYPE a summary\na_count -1\n# EOF\n'),
787+
('# TYPE a summary\na_count 0.5\n# EOF\n'),
771788
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
772789
# Bad info and stateset values.
773790
('# TYPE a info\na_info{foo="bar"} 2\n# EOF\n'),

0 commit comments

Comments
 (0)
0