Prometheus range vector default value

Viewed 722

I am attempting to write a query to find instances where the value has been the same for a set amount of time. This type of query is for the most part just useful for "metadata" metrics which report a value of 0 or 1.

In my instance I am interested if a metric has had the value 1 for a period of 20 minutes. My approach was to get the min value over time, as if the time series had a single 0 value within the 20 minute window it would report as a 0.

min_over_time(meta_metric[20m]) > 0

This works well for the most parts with the exception of instances where a new time series is created. In those cases if the time series starts with the value 1 it would result as if the time series has had the value 1 for the full 20 minutes as all other values in the window is null.

Is there any method of defaulting null values to 0 or ignoring windows where values are missing?

1 Answers

If you know the interval between raw samples in every time series (aka scrape_interval in Prometheus world), then the following query can be used:

min_over_time(meta_metric[20m]) > 0 and count_over_time(meta_metric[20m]) > N

Where N must be substituted with the minimum number of raw samples per each time series in 20 minute lookbehind window, which must be taken into account. For example, if the interval between raw samples is 1 minute, then N can be substituted with a number close to 20.

Related