Some implications of using
offset instead of
delta() in Prometheus
I previously wrote about how
delta() can be inferior to subtraction
delta() has to
load the entire range of metric points and
offset doesn't. In
light of the issue I ran into recently with stale metrics and
range queries, there turn out to
be some implications and complexities of using
offset in place
delta(), even if it lets you make queries that you couldn't
Let's start with the basics, which is that '
can theoretically be replaced with '
mymetric - mymetric offset 30d'
to get the same result with far fewer metric points having to be
loaded by Prometheus. This is an important issue for us, because
we have some high-cardinality metrics that it turns out we want
to query over long time scales like 30 or 90 days.
The first issue with the
offset replacement is what happens when
a particular set of labels for the metric didn't exist 30 days ago.
Just like PromQL boolean operators (cf),
PromQL math operators on vectors are filters, so you'll ignore all
current metric points for
mymetric that didn't exist 30 days ago.
The fix for this is the inverse of ignoring stale metrics:
(mymetric - mymetric offset 30d) or mymetric
mymetric didn't exist 30 days ago we implicitly take its
starting value as 0 and just consider the delta to be the current
mymetric. Under some circumstances you may want a different
delta value for 'new' metrics, which will require a different
The inverse of the situation is metric labels that existed 30 days
ago but don't exist now. As we saw in an earlier entry, the range query in the
version will include those metrics, so they will flow through to
delta() calculation and be included in your final result set.
sort of claims otherwise, the actual code implementing
reasonably doesn't currently extrapolate samples that start and end
significantly far away from the full time range, so the
result will probably be just the change over the time series points
available. In some cases this will go to zero, but in others it
will be uninteresting and you would rather pretend that the time
series is now 0. Unfortunately, as far as I know there's no good
way to do that.
If you only care about time series (ie label sets) that existed at the start of the time period, I think you can extend the previous case to:
((mymetric - mymetric offset 30d) or mymetric) or -(mymetric offset 30d)
(As before, this assumes that a time series that disappears is implicitly going to zero.)
If you care about time series that existed in the middle of the
time range but not at either the beginning or the end, I think
you're out of luck. The only way to sweep those up is a range query
delta(), which runs the risk of a 'too many metric
points loaded' error.
Unfortunately all of this is increasingly verbose, especially if
you're using label matches restricting
mymetric to only some
values (because then you need to propagate these label restrictions
into at least the
or clauses). It's a pity that PromQL doesn't
have any function to do this for us.
I also have to modify something I said in my first entry on
delta(). Given all of these
issues with appearing and disappearing time series, it's clear that
delta() to not require the entire range is not as
simple as it looks. It would probably require some deep hooks into
the storage engine to say 'we don't need all the points, just the
start and the end points and their timestamps', and that stuff would
only be useful for gauges (since counters already have to load the
entire range set and sweep over it looking for counter resets).
In our current usage we care more about how the current metrics got
there than what the situation was in the past; we are essentially
looking backward to ask what disk space usage grew or shrank. If
some past usage went to zero and disappeared, it's okay to exclude
it entirely. There are some potentially tricky cases that might
cause me to rethink that someday, but for now I'm going to use the
shorter version that only has one
or, partly because Grafana makes
it a relatively large pain to write complicated PromQL queries.