Skip to content

Reject NaN increments on Counter - #1256

Open
WatchTree-19 wants to merge 1 commit into
prometheus:masterfrom
WatchTree-19:fix/counter-reject-nan
Open

WatchTree-19 wants to merge 1 commit into
prometheus:masterfrom
WatchTree-19:fix/counter-reject-nan

Conversation

@WatchTree-19

Copy link
Copy Markdown

Counter.inc rejects negative amounts but accepts float('nan'), since nan < 0 is false. After one NaN increment the counter's total is NaN for the rest of the process:

c = Counter('c', 'c')
c.inc(2)
c.inc(float('nan'))
# c_total NaN

OpenMetrics says a Counter's Total "is a non-NaN and MUST be monotonically non-decreasing over time". This raises ValueError for a NaN amount, the same way a negative amount is handled, and leaves the total unchanged. Histogram NaN handling (#1225) is untouched.

test_nan_increment_raises covers it and fails on master. tests/ passes apart from the modules needing optional extras I did not install (aiohttp, asgi, django, twisted), and flake8 is clean on both files.

Counter.inc rejected negative amounts but accepted NaN, which left the
counter's total at NaN from then on. OpenMetrics requires a counter's
total to be non-NaN and monotonically non-decreasing, so a NaN increment
now raises ValueError and the total is left unchanged.

Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.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