Skip to content

OpenMetrics parser drops timestamp exponents after nine fractional digits #1257

Open

Description

@Rajatendu1

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:

from prometheus_client.openmetrics.parser import text_string_to_metric_families

text = '''# TYPE a gauge
a 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions