Search code examples
prometheuspromql

How to ignore missing datapoints in PromQL query?


i'm trying to execute a complex promql query. basically i want to compare the last 1 hour average, to the same hour at the past 3 days (average):

rate(processing_time_sum[1h]) / rate(processing_time_count[1h]) / (
rate(processing_time_sum[1h] offset 1d) / rate(processing_time_count[1h] offset 1d) +
rate(processing_time_sum[1h] offset 2d) / rate(processing_time_count[1h] offset 2d) +
rate(processing_time_sum[1h] offset 3d) / rate(processing_time_count[1h] offset 3d) +
)/3

My problem is when there is no data at one (or more) of the previous days (Saturday, Sundays and holidays), then i'm getting "No data points found" error on the entire query. I would like to be able to either ignore the missing days, or use the last day that had data points.


Solution

  • In general, when doing something like a + b in PromQL, where b might be missing some of a's label combinations, you can write it as:

    a + (b or a * 0)
    

    Or, in your case, since you're using division and multiple b metrics (as it were):

    a / ((b1 or a) + (b2 or a) + (b3 or a)) * 3
    

    But the more b metrics are missing, the more "normal" your a metric will look like. (E.g. if you have no values for any of the previous 3 days, the result will be 1.)

    Or, if you simply want the last day with data points instead (again, falling back to a if all of the bs are missing):

    a / (b1 or b2 or b3 or a)