If you need obtaining the increase of some counter metric m
over a time range (t-d .. t]
, then the following PromQL query can be used:
increase(m[d] @ t)
If the metric is a gauge, then just substitute increase()
with delta()
:
delta(m[d] @ t)
These queries use @ modifier for fixing the end of the time range at t
. The start of the time range - t-d
- is adjusted with the d
lookbehind window in square brackets.
Prometheus provides two endpoints for querying:
- /api/v1/query aka
instant query
. This endpoint accepts the timestamp for the query at time
argument. So the query can be simplified to just increase(m[d])
when passed to this endpoint.
- /api/v1/query_range aka
range query
. This endpoint accepts start
and end
query args, which specify the time range for the query, alongside step
query arg, which specifies the interval between returned query results. This endpoint returns independently calculated query results at the following timestamps: start
, start+step
, start+2*step
, ... , end
. If increase(m[d])
is passed to this endpoint, then this query is executed independently per each timestamp t
mentioned above. Grafana queries this endpoint for building graphs. That's why every point on the graph would contain the m
increase over the lookbehind window d
ending at this point.
If m
returns multiple time series, then both increase(m[d])
and delta(m[d])
would return individual results per each input time series. These results can be summed by wrapping the query into sum():
sum(increase(m[d]))
If you want obtaining the graph, which always starts from zero on the left side and grows with the m
's increase over time on the selected time range, then Prometheus doesn't provide such a functionality. But this task can be solved with VictoriaMetrics - this is Prometheus-like monitoring system I work on:
running_sum(increase(m))
This query uses the following features of VictoriaMetrics:
increase()
without lookbehind window in square brackets. VictoriaMetrics automatically sets the lookbehind window to the interval between points shown on the graph in this case. See these docs for more details.
- running_sum() function for calculating the running sum over the per-point
increase()
results on the graph.