The following PromQL query returns per-hour increase for http_requests
metric:
last_over_time(increase(http_requests[1h])[1h:1h])
This query uses subqueries functionality for wrapping increase()
function into last_over_time() function.
The returned numbers are shifted by one hour in the past, e.g. it shows counter increase for 10:00 - 11:00
during the next hour - 11:00 - 12:00
. This time shift can be removed by adding offset -1h
to the query:
last_over_time(increase(http_requests[1h] offset -1h)[1h:1h])
Prometheus doesn't support negative offsets by default, so this query returns negative offset is disabled, use --enable-feature=promql-negative-offset to enable it
error unless Prometheus runs with --enable-feature=promql-negative-offset
command-line flag (btw, other Prometheus-like systems such as VictoriaMetrics support negative offsets out of the box).
Note also that Prometheus has the following issues with increase()
function:
- The
increase()
over integer counter can return fractional results because of extrapolation. See this issue for details.
- The
increase(http_requests[1h])
doesn't take into account counter increase between the last raw sample on the previous hour and the first raw sample on the current hour. See this article and this comment for details. This may result in lower than expected increase()
results over slow-moving counters.
Both issues are going to be fixed in Prometheus according to this design doc. In the mean time other Prometheus-like systems such as VictoriaMetrics may be used - they are free from these issues.