Skip to content

Fix OpenMetrics timestamp parsing for exponent notation - #1259

Closed
PerryLink wants to merge 1 commit into
prometheus:masterfrom
PerryLink:fix/openmetrics-exponent-timestamps
Closed

PerryLink wants to merge 1 commit into
prometheus:masterfrom
PerryLink:fix/openmetrics-exponent-timestamps

Conversation

@PerryLink

Copy link
Copy Markdown
Contributor

What

Parse OpenMetrics timestamps that use exponent notation (1.704067200123e9) through the float fallback instead of slicing them as fixed-point nanoseconds.

Why

_parse_timestamp() first tries int(), then splits on . and keeps the first nine characters of the fractional part as nanoseconds. For a mantissa with an exponent the "fraction" contains the exponent (704067200123e9), so:

  • 1.704067200123e9 became Timestamp(1, 704067200) = 1.7040672;
  • 1.123456789e3 became 1.123456789;
  • trailing garbage was silently truncated the same way: 1.123456789garbage parsed as 1.123456789.

Sample and exemplar timestamps share the helper, and negative exponents were affected too.

Fix

Only take the fixed-point branch when the part after the dot is all digits; otherwise fall through to the existing float path, which produces the expected instant and rejects invalid input. Fixed-point timestamps (up to nanosecond precision) are unchanged.

Reproduction

$ python - <<'PY'
from prometheus_client.openmetrics.parser import text_string_to_metric_families
text = '# TYPE a gauge\na 1 1.704067200123e9\n# EOF\n'
print(float(list(text_string_to_metric_families(text))[0].samples[0].timestamp))
PY
1.7040672          # before; expected 1704067200.123

Tests

  • new test_exponent_timestamps (sample and exemplar timestamps, positive and negative exponents) and test_invalid_timestamp_after_nanoseconds;
  • with the parser change reverted both new tests fail; with it: python -m pytest tests/openmetrics/test_parser.py -q → 61 passed;
  • python -m pytest tests/openmetrics tests/test_core.py tests/test_exposition.py -q → 383 passed, 1 skipped.

Fixes #1257

_parse_timestamp() treated the text after the dot as a fixed-point nanosecond
fraction even when the mantissa carried an exponent, so 1.704067200123e9
became 1.7040672 and 1.123456789garbage was silently accepted. Only use the
fixed-point branch when the fraction is all digits, and fall through to the
float path otherwise. Add sample and exemplar regression tests.
@PerryLink

Copy link
Copy Markdown
Contributor Author

Reopened as #1260 with the Signed-off-by line the DCO check asks for — same change, signed commit. Closing this one.

@PerryLink PerryLink closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenMetrics parser drops timestamp exponents after nine fractional digits

1 participant