Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prometheus: prevent panic when incrmenting counter #146

Merged
merged 1 commit into from
Nov 27, 2023

Conversation

lgfa29
Copy link
Contributor

@lgfa29 lgfa29 commented Jan 26, 2023

The Prometheus Counter.Add() method panics if the value being added is negative. Even if care is taken by consumers to never pass a negative value the panic could still happen due to float conversion or overflow.

This change prevents go-metrics from causing consumers to crash.

A panic like this has been reported in hashicorp/nomad#15861, but, as far as I can tell, Nomad only calls IncrCounter and IncrCounterWithLabels using positive values.

Copy link
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tgross
Copy link
Member

tgross commented May 8, 2023

@lgfa29 as an alternative because this seems like such a weird case and a programmer error rather than a runtime error, what would you think about shipping a version of Nomad pinned to this branch to see if we can get reports on the error to fix the underlying cause?

@lgfa29
Copy link
Contributor Author

lgfa29 commented May 9, 2023

Ah good idea. I will open a PR with this dependency update on Nomad tomorrow. I will probably rebase this as branch to v0.4.1 to make sure this is the only code change.

The Prometheus Counter.Add() method panics if the value being added is
negative. Even if care is taken by consumers to never pass a negative
value the panic could still happen due to float conversion or overflow.

This change prevents go-metrics from causing consumers to crash.
Copy link
Member

@banks banks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming this can occur in unexpected ways especially since we are working with floats here!

I think if we've had real-world pain caused, it makes sense to be more defensive here.

@lgfa29 lgfa29 merged commit 9d55fb2 into master Nov 27, 2023
@lgfa29 lgfa29 deleted the fix-prometheus-panic branch November 27, 2023 21:45
lgfa29 added a commit to hashicorp/nomad that referenced this pull request Nov 28, 2023
lgfa29 added a commit to hashicorp/nomad that referenced this pull request Nov 28, 2023
lgfa29 added a commit to hashicorp/nomad that referenced this pull request Nov 28, 2023
lgfa29 added a commit to hashicorp/nomad that referenced this pull request Nov 28, 2023
lgfa29 added a commit to hashicorp/nomad that referenced this pull request Nov 28, 2023
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.

3 participants