Skip to content

metric: disallow calls to Counter.Dec, use Gauge instead#21903

Merged
nvb merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/counterGauge
Jan 30, 2018
Merged

metric: disallow calls to Counter.Dec, use Gauge instead#21903
nvb merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/counterGauge

Conversation

@nvb
Copy link
Copy Markdown
Contributor

@nvb nvb commented Jan 30, 2018

https://prometheus.io/docs/concepts/metric_types/#counter says that:

Counters should not be used to expose current counts of items
whose number can also go down, e.g. the number of currently
running goroutines. Use gauges for this use case.

We were ignoring this in a few places. This change prevents the
use of Counter.Dec and turns all previous users into Gauges.

The change prevents use of Counter.Dec by overriding the method and
making it panic with a message to "use a Gauge instead". An alternate
approach would be to unembedded metrics.Counter and no longer expose
a Dec method. That approach would also be ok, but I think this way
does a better job documenting the issue, preventing regressions, and
avoiding boilerplate required if metrics.Counter was unexported.

Release note: None

@nvb nvb requested review from a team January 30, 2018 02:36
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@nvb nvb force-pushed the nvanbenschoten/counterGauge branch 2 times, most recently from 62829b2 to ef60700 Compare January 30, 2018 13:28
https://prometheus.io/docs/concepts/metric_types/#counter says that:
> Counters should not be used to expose current counts of items
> whose number can also go down, e.g. the number of currently
> running goroutines. Use gauges for this use case.

We were ignoring this in a few places. This change prevents the
use of `Counter.Dec` and turns all previous users into Gauges.

The change prevents use of `Counter.Dec` by overriding the method and
making it panic with a message to "use a Gauge instead". An alternate
approach would be to unembedded `metrics.Counter` and no longer expose
a `Dec` method. That approach would also be ok, but I think this way
does a better job documenting the issue, preventing regressions, and
avoiding boilerplate required if `metrics.Counter` was unexported.

Release note: None
@nvb nvb force-pushed the nvanbenschoten/counterGauge branch from ef60700 to 74d6242 Compare January 30, 2018 14:15
Copy link
Copy Markdown
Contributor

@a-robinson a-robinson left a comment

Choose a reason for hiding this comment

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

LGTM. I should have checked for this a while ago.

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Jan 30, 2018

TFTR!

@nvb nvb merged commit bfbc66d into cockroachdb:master Jan 30, 2018
@nvb nvb deleted the nvanbenschoten/counterGauge branch January 30, 2018 14:57
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