Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase() vs changes() function for counters

Tags:

prometheus

I have a gift-certificates application that increases Prometheus counter whenever someone activates a certificate.

Now I want to put simple number in Grafana board that shows me how many certificates were activated last 24h.

I can do that by increase(gift_certificates_activated_total[24h]) query, but it gives float results, something like 105.01287565915334.

But what I found is if I use changes function, it gives me the exact integer result. Query is: changes(gift_certificates_activated_total[24h]) and it shows exactly 105.

Docs says

For each input time series, changes(v range-vector) returns the number of times its value has changed within the provided time range as an instant vector.

As far as I understood, that it's suitable for gauge metric types to get the number of changes of gauge metric value, not for counter. But it seems to work fine with counters too. It shows the same result as floor(increase(gift_certificates_activated_total[24h])) query.

Is it appropriate usage of changes() function?

like image 927
Nikolay Oskin Avatar asked Jun 24 '26 03:06

Nikolay Oskin


1 Answers

The changes() function in Prometheus can be used instead of increase() function if you are sure that the counter stays the same or is incremented by 1 between scrapes. If the counter is incremented by more than 1, then changes() will return lower results than increase().

The increase() function in Prometheus may return fractional results for integers counters because of interpolation. See more details here.

Note also that both changes(m[d]) and increase(m[d]) in Prometheus may miss the difference between the last samples just before the lookbehind window d and the first point at the window. Both issues mentioned above are resolved in MetricsQL though. (I'm the author of MetricsQL).

like image 124
valyala Avatar answered Jun 27 '26 05:06

valyala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!