Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch non-integer bucket/count values. #726

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,16 @@ def build_metric(name, documentation, typ, unit, samples):

if typ == 'stateset' and name not in sample.labels:
raise ValueError("Stateset missing label: " + line)
if (typ in ['histogram', 'gaugehistogram'] and name + '_bucket' == sample.name
if (name + '_bucket' == sample.name
csmarchbanks marked this conversation as resolved.
Show resolved Hide resolved
and (sample.labels.get('le', "NaN") == "NaN"
or _isUncanonicalNumber(sample.labels['le']))):
raise ValueError("Invalid le label: " + line)
if (name + '_bucket' == sample.name
and (not isinstance(sample.value, int) and not sample.value.is_integer())):
raise ValueError("Bucket value must be an integer: " + line)
if ((name + '_count' == sample.name or name + '_gcount' == sample.name)
and (not isinstance(sample.value, int) and not sample.value.is_integer())):
raise ValueError("Count value must be an integer: " + line)
if (typ == 'summary' and name == sample.name
and (not (0 <= float(sample.labels.get('quantile', -1)) <= 1)
or _isUncanonicalNumber(sample.labels['quantile']))):
Expand Down
17 changes: 17 additions & 0 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ def test_simple_histogram(self):
a_count 3
a_sum 2
# EOF
""")
self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
list(families))

def test_simple_histogram_float_values(self):
families = text_string_to_metric_families("""# TYPE a histogram
# HELP a help
a_bucket{le="1.0"} 0.0
a_bucket{le="+Inf"} 3.0
a_count 3.0
a_sum 2.0
# EOF
""")
self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1.0", 0.0), ("+Inf", 3.0)])],
list(families))
Expand Down Expand Up @@ -763,15 +775,20 @@ def test_invalid_input(self):
('# TYPE a histogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum -1\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="-1.0"} 1\na_bucket{le="+Inf"} 2\na_sum 1\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} 0.5\na_count 0.5\na_sum 0\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\na_gcount -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum NaN\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0.5\na_gsum 0.5\na_gcount 0\n# EOF\n'),
('# TYPE a summary\na_sum NaN\n# EOF\n'),
('# TYPE a summary\na_count NaN\n# EOF\n'),
('# TYPE a summary\na_sum -1\n# EOF\n'),
('# TYPE a summary\na_count -1\n# EOF\n'),
('# TYPE a summary\na_count 0.5\n# EOF\n'),
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
# Bad info and stateset values.
('# TYPE a info\na_info{foo="bar"} 2\n# EOF\n'),
Expand Down