Search code examples
prometheuspromql

Use result of binary expression as an instant vector


I have a binary expression like the following:

up{instance=~"^.*:.*"} unless up{instance=~"^.*:10000$"}

It's just an example. I know that I could write regular expression so that it'll be just one. But it's a simplified example. Real dataset contains lot of series out of which I need to extract a subset and then filter out certain items.

I need to use the result of this expression (instant vector, right?) as one would use a typical instant vector - grab a range and feed to something like rate function for example. And it turns out that it's either not possible or I misunderstand some fundamental concept about PromQL.

How would one take a rate of the result of this expression?

UPDATE:

The following works:

rate(up{instance=~"^.*:.*"}[5m]) unless rate(up{instance=~"^.*:10000$"}[5m])

However, I was looking for a way to filter first and rate second, since otherwise a lot of rate processing will be done on series that will simply be dropped later.


Solution

  • Prometheus allows specifying multiple filters for the same label in a single series selector, then apply rate function to it - see this answer for details.

    Prometheus allows passing arbitrary query results to functions, which accept e.g. range vectors, by using subqueries. For example, the following query would calculate arbitrary query q with 30-seconds interval between calculated points and then apply rate with 5 minutes lookbehind window over the calculated results:

    rate((q)[5m:30s])
    

    In general subqueries aren't recommended to use because they are hard to construct properly and because they may take too much cpu and memory to execute when improperly constructed.