From 603e0af7b89f9943145268f203f5575c4f618930 Mon Sep 17 00:00:00 2001 From: "Md. Amdadul Bari Imad" Date: Tue, 1 Dec 2026 09:09:32 +0000 Subject: [PATCH] Compare mixed Timestamp and float timestamps in the OpenMetrics parser _parse_timestamp returns a Timestamp for fixed-point values such as "1" or "1.5" and a float for exponent notation such as "1.5e0". When a group mixed the two, the "Timestamps went backwards" check compared them with Timestamp.__gt__/__lt__, which read .sec/.nsec from the float and raised AttributeError instead of parsing the input (or raising ValueError for timestamps that really went backwards). Compare two Timestamps exactly as before, and fall back to comparing float values when either side is a float. Signed-off-by: Md. Amdadul Bari Imad --- prometheus_client/openmetrics/parser.py | 13 ++++++++++++- tests/openmetrics/test_parser.py | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/prometheus_client/openmetrics/parser.py b/prometheus_client/openmetrics/parser.py index ebd6da10..e2000286 100644 --- a/prometheus_client/openmetrics/parser.py +++ b/prometheus_client/openmetrics/parser.py @@ -105,6 +105,17 @@ def _parse_timestamp(timestamp): return ts +def _timestamp_is_after(a, b): + """Return whether timestamp a is later than timestamp b. + + _parse_timestamp returns a Timestamp for fixed-point values and a float + for exponent notation, so the two can be mixed within a group. + """ + if isinstance(a, Timestamp) and isinstance(b, Timestamp): + return a > b + return float(a) > float(b) + + def _is_character_escaped(s, charpos): num_bslashes = 0 while (charpos > num_bslashes @@ -617,7 +628,7 @@ def build_metric(name, documentation, typ, unit, samples): if group is not None and g == group: if (sample.timestamp is None) != (group_timestamp is None): raise ValueError("Mix of timestamp presence within a group: " + line) - if group_timestamp is not None and group_timestamp > sample.timestamp and typ != 'info': + if group_timestamp is not None and _timestamp_is_after(group_timestamp, sample.timestamp) and typ != 'info': raise ValueError("Timestamps went backwards within a group: " + line) else: group_timestamp_samples = set() diff --git a/tests/openmetrics/test_parser.py b/tests/openmetrics/test_parser.py index 78b7af83..46bcb272 100644 --- a/tests/openmetrics/test_parser.py +++ b/tests/openmetrics/test_parser.py @@ -664,6 +664,20 @@ def test_timestamps(self): b.add_metric([], 2, timestamp=Timestamp(1234567890, 0)) self.assertEqual([a, b], list(families)) + def test_mixed_timestamp_formats(self): + families = text_string_to_metric_families("""# TYPE a gauge +# HELP a help +a 1 1 +a 2 1.5e0 +a 3 2.5 +# EOF +""") + a = GaugeMetricFamily("a", "help") + a.add_sample("a", {}, 1, Timestamp(1, 0)) + a.add_sample("a", {}, 2, 1.5) + a.add_sample("a", {}, 3, Timestamp(2, 500000000)) + self.assertEqual([a], list(families)) + def test_hash_in_label_value(self): families = text_string_to_metric_families("""# TYPE a counter # HELP a help @@ -958,6 +972,8 @@ def test_invalid_input(self): ('# TYPE a gauge\na 0 1\na 0 -1\n# EOF\n'), ('# TYPE a gauge\na 0 1.1\na 0 1\n# EOF\n'), ('# TYPE a gauge\na 0 1\na 0 0\n# EOF\n'), + ('# TYPE a gauge\na 0 2\na 0 1.5e0\n# EOF\n'), + ('# TYPE a gauge\na 0 2e0\na 0 1.5\n# EOF\n'), ('# TYPE a gauge\na 0\na 0 0\n# EOF\n'), ('# TYPE a gauge\na 0 0\na 0\n# EOF\n'), # Clashing names.