Skip to content
Open
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
7 changes: 6 additions & 1 deletion prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ def _parse_timestamp(timestamp):
try:
# aaaa.bbbb. Nanosecond resolution supported.
parts = timestamp.split('.', 1)
return Timestamp(int(parts[0]), int(parts[1][:9].ljust(9, "0")))
sec = int(parts[0])
nsec = int(parts[1][:9].ljust(9, "0"))
if sec == 0 and parts[0].startswith('-'):
# int("-0") is 0, so carry the sign the way Timestamp(-0.5, 500000000) does.
return Timestamp(-nsec / 1e9, nsec)
return Timestamp(sec, nsec)
except ValueError:
# Float.
ts = float(timestamp)
Expand Down
14 changes: 14 additions & 0 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,20 @@ def test_null_byte(self):
metric_family = CounterMetricFamily("a", "he\0lp")
self.assertEqual([metric_family], list(families))

def test_negative_subsecond_timestamps(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a help
a_total{foo="1"} 1 -0.500000000
a_total{foo="2"} 1 -1.500000000 # {a="b"} 0.5 -0.250000000
# EOF
""")
a = CounterMetricFamily("a", "help", labels=["foo"])
a.add_metric(["1"], 1, timestamp=Timestamp(-0.5, 500000000))
a.add_metric(["2"], 1, timestamp=Timestamp(-1, 500000000))
a.samples[1] = a.samples[1]._replace(exemplar=Exemplar({"a": "b"}, 0.5, Timestamp(-0.25, 250000000)))
self.assertEqual([a], list(families))
self.assertEqual("-0.500000000", str(a.samples[0].timestamp))

def test_timestamps(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a help
Expand Down
Loading