Search code examples
prometheuspromqlprometheus-alertmanager

Filtering values in promql's predict_linear


I would like to filter out certain values from the metric that predict_linear relies on.

Take the following example: Ex: predict_linear(some_metric[4h], 4 * 60 * 60) > 1024

The some_metric in this case occasionally reports -1 values, which I would like to filter out. (For reference, I am not in control of some_metric, this is reported by an external service)

I tried:

predict_linear((some_metric > 0)[4h], 4 * 60 * 60) > 1024

but this reports back:

Error executing query: 1:53: parse error: ranges only allowed for vector selectors

Solution

  • The reason for this is exactly as satiated in error mesage: ranges only allowed for vector selectors.

    But you cab use subquery syntax, to workaround this issue.

    predict_linear((some_metric > 0)[4h : ], 4 * 60 * 60) > 1024
    

    Notice here [4h:] means range of 4 hours with minimal possible resolution. Since : is used it is recognised as subquery and can be applied not only to the vector selectors, but to the results of comparison (also, to functions and results of other binary operators)