We have several different metrics which show bytes total (which is a counter) for all sorts of "cars" and their "types". For example
bytes_total_incoming{"car_name": "A", type: "1" 234234523452345.0}
bytes_total_incoming{"car_name": "A", type: "5" 23423452345.0}
bytes_total_incoming{"car_name": "B", type: "1" 6734234523452345.0}
bytes_total_incoming{"car_name": "B", type: "6" 56783452345.0}
bytes_total_incoming{"car_name": "B", type: "5" 45.0}
How would I calculate usage per day and group by car name and type? Do I have to use the irate function for this or do I just use some kind of sum function?
You need to use increase() function for calculating bytes usage over the last 24 hours:
increase(bytes_total_incoming[24h])
This query is executed independently per each point on the graph and per each matching time series. So every point on the graph shows bytes usage for the last 24 hours ending at that point. If you need showing per-day bytes usage on the graph, then the following options exist:
min interval
in Grafana graph panel settings to 24h
. See these docs. In this case Grafana would query only a single point per day per each matching time series. Every point will show bytes usage over the previous day ending at the point.increase()
calculations to per-day boundaries:last_over_time(
increase(bytes_total_incoming[1d])[2d:1d]
)
This query returns a graph with per-day steps showing bytes usage over the previois day.
If you need grouping the results by a paricular label such as car_name
or type
, then just wrap the query into sum(...) by (car_name)
or sum(...) by (type)
. For example, the following query returns per-day bytes usage grouped by type
:
sum(
increase(bytes_total_incoming[24h])
) by (type)
Tye sum() is an aggregate function, which sums series values individually per each point and groups the reaulting sums by label values specified in the optional by (...)
modifier.
P.s. the above queries return per-day bytes usage shifted by one day in the future. For example, they return bytes usage for August 10 at August 11. This shift can be removed with negative offset modifier:
sum(
increase(bytes_total_incoming[24h] offset -24h)
)
P.p.s. increase()
function in Prometheus may return fractional results when applied to integer counters. This is because of extrapolation - see this issue. If you want obtaining the exact increase over integer counter, then try the project I work on - VictoriaMetrics. It implements increase()
function in the expected way without using extrapolation. See this article for details.