You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenMetrics parser drops timestamp exponents after nine fractional digits #1257
The OpenMetrics parser drops the exponent from a timestamp once its mantissa has at least nine fractional digits. This affects sample and exemplar timestamps.
Reproduced on current master c0f1bdf39755ee9b028a63a1330c60a2b3455b49, Python 3.10.12:
fromprometheus_client.openmetrics.parserimporttext_string_to_metric_familiestext='''# TYPE a gaugea 1 1.704067200123e9# EOF'''sample=list(text_string_to_metric_families(text))[0].samples[0]
print(float(sample.timestamp))
# Actual: 1.7040672# Expected: 1704067200.123
A shorter mantissa such as 1.12345678e3 takes the float fallback correctly. At nine fractional digits, 1.123456789e3 instead becomes 1.123456789. The same happens with negative exponents and with exemplar timestamps. Invalid input such as 1.123456789garbage is also accepted.
_parse_timestamp() slices the fractional text to nine characters before checking whether the entire timestamp is a plain decimal. The exponent or malformed suffix after those characters is discarded, so the float fallback is never reached.
The OpenMetrics grammar allows scientific notation for timestamps. A possible fix is to use the nanosecond-preserving path only when the whole fractional part contains decimal digits, then leave scientific notation to the existing float fallback. This keeps long plain-decimal timestamps on their current exact path.
Private regression tests cover sample/exemplar timestamps, positive/negative exponents and invalid suffixes. There are 14 failing subcases on unchanged master; the candidate passes the extended native OpenMetrics parser module (61 tests). A wider available unittest selection passes 107 tests. Full tox, optional integrations, lint/type checks and other Python versions have not been run. The same exponent-dropping path is present in all seven current repository branches, including master and release-v0.21.x.
Is this small parser fix welcome as a PR?
Prepared and tested with AI assistance. This is a synthetic parser reproduction, not a claim of a production incident.
Activity