From ff2394856436121c5751924d11eae4aa16d0b153 Mon Sep 17 00:00:00 2001 From: Chris Marchbanks Date: Wed, 21 Dec 2022 16:49:45 -0700 Subject: [PATCH] Allow Prometheus parser to handle missing space If there is no delimiter separating the labels and value when parsing the Prometheus format the parser currently will chop off the first digit of the value. Instead, allow a missing delimiter in the Prometheus format to align with what the Go parser does. In addition, add a test to specifically check that a delimiter is still required in OpenMetrics. Signed-off-by: Chris Marchbanks --- prometheus_client/parser.py | 4 ++-- tests/openmetrics/test_parser.py | 2 ++ tests/test_parser.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/prometheus_client/parser.py b/prometheus_client/parser.py index ad3e1c60..7135bc8a 100644 --- a/prometheus_client/parser.py +++ b/prometheus_client/parser.py @@ -116,8 +116,8 @@ def _parse_sample(text): name = text[:label_start].strip() # We ignore the starting curly brace label = text[label_start + 1:label_end] - # The value is after the label end (ignoring curly brace and space) - value, timestamp = _parse_value_and_timestamp(text[label_end + 2:]) + # The value is after the label end (ignoring curly brace) + value, timestamp = _parse_value_and_timestamp(text[label_end + 1:]) return Sample(name, _parse_labels(label), value, timestamp) # We don't have labels diff --git a/tests/openmetrics/test_parser.py b/tests/openmetrics/test_parser.py index 4b4aecd6..937aef5c 100644 --- a/tests/openmetrics/test_parser.py +++ b/tests/openmetrics/test_parser.py @@ -642,6 +642,8 @@ def test_invalid_input(self): ('a{a""} 1\n# EOF\n'), ('a{a=} 1\n# EOF\n'), ('a{a="} 1\n# EOF\n'), + # Missing delimiters. + ('a{a="1"}1\n# EOF\n'), # Missing or extra commas. ('a{a="1"b="2"} 1\n# EOF\n'), ('a{a="1",,b="2"} 1\n# EOF\n'), diff --git a/tests/test_parser.py b/tests/test_parser.py index 1d20d276..61b3c8ae 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -146,6 +146,7 @@ def test_spaces(self): a { foo = "buz" } 3 a\t { \t foo\t = "biz"\t } \t 4 a \t{\t foo = "boz"\t}\t 5 +a{foo="bez"}6 """) metric_family = CounterMetricFamily("a", "help", labels=["foo"]) metric_family.add_metric(["bar"], 1) @@ -153,6 +154,7 @@ def test_spaces(self): metric_family.add_metric(["buz"], 3) metric_family.add_metric(["biz"], 4) metric_family.add_metric(["boz"], 5) + metric_family.add_metric(["bez"], 6) self.assertEqualMetrics([metric_family], list(families)) def test_commas(self):