Skip to content

Compare mixed Timestamp and float timestamps in the OpenMetrics parser - #1266

Open
amdadulbari wants to merge 1 commit into
prometheus:masterfrom
amdadulbari:fix/om-parser-mixed-timestamp-types
Open

amdadulbari wants to merge 1 commit into
prometheus:masterfrom
amdadulbari:fix/om-parser-mixed-timestamp-types

Conversation

@amdadulbari

Copy link
Copy Markdown
Contributor

@csmarchbanks

_parse_timestamp returns a Timestamp for fixed-point timestamps (1, 2.5) and a float for exponent notation (1.5e0), both of which are valid OpenMetrics timestamps. When a group contains both forms, the "Timestamps went backwards" check compares them with Timestamp.__gt__/__lt__, which read .sec/.nsec from the float and raise AttributeError:

# TYPE a gauge
a 1 1
a 2 1.5e0
# EOF
AttributeError: 'float' object has no attribute 'sec'

Input whose timestamps really go backwards (a 0 2 then a 0 1.5e0) also raises AttributeError instead of the expected ValueError.

This change keeps the exact comparison when both values are Timestamps and compares float values when either side is a float.

Tests: added test_mixed_timestamp_formats (ascending mixed-format timestamps parse) and two backwards mixed-format cases in test_invalid_input. Both fail with AttributeError without the fix; the full test suite, flake8, isort and mypy pass.

_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 <amdadulbari@gmail.com>

This branch has not been deployed

No deployments
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.

1 participant