Search code examples
prometheusgrafanapromql

Prometheus Query for 24 hour snapshots


is there a way to query prometheus and return 24hour snapshots of a metric over the course of a week?

Details about the metric:

  • Name: foo
  • Type: Gauge
  • Value: 1

The metric is scraped every minute and the value will vary like so

enter image description here

I want to turn this into something that looks like

enter image description here

Which is taking a rollup of all the values within a 24hour period and creating a single sum'd value for each day.

Is this possible?

Thanks!


Solution

  • This is possible with Prometheus subqueries. For example, the following query should return per-day averages for the metric:

    last_over_time(
      avg_over_time(metric[1d])[1d:1d]
    )
    

    Note that the returned results will be shifted 1 day forward because Prometheus performs calculations over lookbehind windows in square brackets. This can be fixed by adding offset -1d inside the query:

    last_over_time(
      avg_over_time(metric[1d] offset -1d)[1d:1d]
    )
    

    Unfortunately the given query with negative offset doesn't work in Prometheus older than v2.33.0. But this query works perfectly in all the versions of VictoriaMetrics - the Prometheus-like monitoring system I work on.