It is OK if counter is reset to zero on service restart, since Prometheus provides increase and rate functions, which remove counter resets before performing actual calculations. Usually Prometheus counters must be wrapped into these functions in order to get meaningful results. For example:
increase(news_read_counter2[24h])
returns the number of news reads for the last 24 hours
rate(news_read_counter2[1h])
returns the average per-second news read rate for the last hour
If you need obtaining an absolute counter value after counter resets' removal, then this can be done with increase(news_read_counter2[10y])
. This query returns the total number of news reads for the last 10 years. Prometheus calculates the specified query independently per each point displayed on the graph. So the query would display non-decreasing graph with an absolute number of news reads since the first new read for the last 10 years. Note that the increase()
query with too big lookbehind window in square brackets may work slowly, since it needs to process all the raw samples stored in Prometheus for time series with news_read_counter2
name.
Note that increase()
function in Prometheus has some issues:
- It may return fractional results over integer counters because of extrapolation. See this issue for details.
- It misses potential counter increase between the last raw sample before the lookbehind window in square brackets and the first raw sample inside the lookbehind window.
- It misses the initial counter increase if time series starts from non-zero sample.
These issues should be fixed eventually according to this design doc. In the mean time you can try VictoriaMetrics - Prometheus-like monitoring system I work on. It supports PromQL-like query language - MetricsQL with increase()
function, which is free from issues mentioned above.
P.S. If you need drawing non-increasing graph, which starts from zero at the left side and shows cumulative counter increase on any selected time range, then Prometheus cannot help with this case :( But VictoriaMetrics can help. For example, the following MetricsQL query returns cumulative counter increase on any selected time range:
running_sum(increase(news_read_counter2))
The query uses running_sum function.
The query also uses VictoriaMetrics feature, which allows skipping lookbehind window in square brackets for increase()
function (and any other rollup functions). In this case it automatically uses the interval between points on the graph (aka step
) as lookbehind window, so all the raw samples are taken into account by the query.