hashicorp / go-metrics

A Golang library for exporting performance and runtime metrics to external metrics systems (i.e. statsite, statsd)
MIT License
1.46k stars 177 forks source link

prometheus: prevent panic when incrmenting counter #146

Closed lgfa29 closed 11 months ago

lgfa29 commented 1 year ago

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 https://github.com/hashicorp/nomad/issues/15861, but, as far as I can tell, Nomad only calls IncrCounter and IncrCounterWithLabels using positive values.

tgross commented 1 year ago

@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 commented 1 year ago

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.