Is there any way to perform an instant vector operation on a range vector?
For instance, if we have count_containers and we want to count the fraction of the time where we have at least one, we might try to do:
avg(avg_over_time(count_containers[1h] > 0))
But this can't be done because count_containers[1h] is a range vector. Is there any sort of map(...) operation that can be performed on instant vectors in range vectors? Or a way to defer building a range vector from count_containers?
No. Prometheus doesn't allow anything like this.
But you can apply range selector over something other than vector selector using subquery syntax. So in your example it would be something like.
Notice that in this case you must place
:in the range selector to indicate usage of subquery.And for this example I used resolution
15s, to indicate that result of the query should be calculated for each 15 seconds window. But you might want to adjust this to your needs, depending on needed precision, scrape interval, etc. Also, resolution can be omitted (while preserving:, like[1h:]): in that case value ofevaluation_intervalwill be used.Your attempt of the query, even if it were supported, would not produced what you wanted. Rather it would calculate average number of containers when number of containers was positive.
To calculate percentage of the time when number of containers was positive use following
Expression
count_containers > bool 0will return 1 if number of containers is positive, and 0 otherwise.