Skip to content
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
6 changes: 5 additions & 1 deletion prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,12 @@ def build_metric(name, documentation, typ, unit, samples):
raise ValueError("Stateset samples can only have values zero and one: " + line)
if typ == 'info' and sample.value != 1:
raise ValueError("Info samples can only have value one: " + line)
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket'] and math.isnan(sample.value):
if typ == 'summary' and name == sample.name and sample.value < 0:
raise ValueError("Quantile values cannot be negative: " + line)
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and math.isnan(sample.value):
raise ValueError("Counter-like samples cannot be NaN: " + line)
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and sample.value < 0:
raise ValueError("Counter-like samples cannot be negative: " + line)
if sample.exemplar and not (
typ in ['histogram', 'gaugehistogram']
and sample.name.endswith('_bucket')):
Expand Down
19 changes: 15 additions & 4 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ def test_empty_help(self):
self.assertEqual([CounterMetricFamily("a", "", value=1)], list(families))

def test_labels_and_infinite(self):
families = text_string_to_metric_families("""# TYPE a counter
families = text_string_to_metric_families("""# TYPE a gauge
# HELP a help
a_total{foo="bar"} +Inf
a_total{foo="baz"} -Inf
a{foo="bar"} +Inf
a{foo="baz"} -Inf
# EOF
""")
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
metric_family = GaugeMetricFamily("a", "help", labels=["foo"])
metric_family.add_metric(["bar"], float('inf'))
metric_family.add_metric(["baz"], float('-inf'))
self.assertEqual([metric_family], list(families))
Expand Down Expand Up @@ -527,12 +527,23 @@ def test_invalid_input(self):
('# TYPE a stateset\na 0\n# EOF\n'),
# Bad counter values.
('# TYPE a counter\na_total NaN\n# EOF\n'),
('# TYPE a counter\na_total -1\n# EOF\n'),
('# TYPE a histogram\na_sum NaN\n# EOF\n'),
('# TYPE a histogram\na_count NaN\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
('# TYPE a histogram\na_sum -1\n# EOF\n'),
('# TYPE a histogram\na_count -1\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} -1\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 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{quantile="0.5"} -1\n# EOF\n'),
# Bad histograms.
('# TYPE a histogram\na_sum 1\n# EOF\n'),
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
Expand Down