How to use last_over_time?

I’m using Grafana Cloud.
I have a Prometheus metric which is collected on irregular intervals, sometimes leaving large gaps with no data. I want to display a stat panel with the most recent value. I can’t use “instant” query because it’s possible there is no data the end of the time range. I thought I should be able to use “range” query with last_over_time like this:

last_over_time(qalif_uniformity_minimum{instance="$qalif_ip"}[$__interval])

Format: time series
Type: range

But last_over_time has no effect here; it still returns a series with multiple samples. What did I do wrong?

Try to use the max_over_time function to extract the most recent value from that range. Maybe this?

max_over_time(last_over_time(qalif_uniformity_minimum{instance="$qalif_ip"}[$__range])[$__interval:])

This works by:

  1. Using last_over_time with $__range to consider your entire time range
  2. Then applying max_over_time with [$__interval:] to get the most recent value

I found this as well which may help: How to make Grafana/Prometheus summarize values by max or peak when zooming out? - #3 by tobia

Thanks for your suggestion. I tried that but it produces the results as before.

Hi,
AFAIK the last_over_time function will shift the last point before no data (as long as the last point is in the range). It will return time series where each point will represent the last visible point (I’ll share some prepared screenshots, sorry for lack of queries but I’m using some custom metric and data privacy :man_shrugging:)
Regular data returned by expression metric:

The data returned with expression last_over_time(metric[$__range]) (notice I’m using $__range and not $__interval):

The last point was shifted to “now”.

What I think you need is to use last_over_time with $__range (or e.g. 1h) lookbehind window (not $__interval since it might be too small range for your case) and use instant query. Notice that if the metric wasn’t updated (returns no data) for more than the range in lookbehind window, it will still return no data - you can’t just ask for the last point and expect it to forever be (your metric could have changed for example and it’s not even there).

This works, thank you! I was under the impression that “instant” queries always use the end of the current time range, which would produce “no data” sometimes. But it looks like you’re right, last_over_time makes the “instant” query show the most recent sample within the time range.

Yeah I like to think that last_over_time “shifts” or “carries on” the last valid point to be the most recent point (as long as it’s in the range), so instant query will take the last point (which is copied).