In promQL, how to max clamp the values of an instant vector with another instant vector

881 Views Asked by At

Is there a way to achieve that max clamping by passing an instant vector containing corresponding max values for each series?

In promQL, using the function clamp_max, it is possible to clamp the values of an instant vector with a scalar value, but if an instant vector is passed instead of the scalar, you get an error

Using function scalar() on that instant vector only works if the instant vector only contains one single value

3

There are 3 best solutions below

0
valyala On BEST ANSWER

The simplest solution for clamping v time series with max_v series is the following:

(v < max_v) or max_v

This query works in the following way:

  1. The v < max_v leaves only values smaller than max_v.
  2. The (...) or max_v fills gaps from the step 1 with max_v values.
0
Dixan M On

Given instant vectors v and max_v with the same labels, this makes the trick

(v <= bool max_v) * v + (v > bool max_v) * max_v

When using comparison operators followed by bool, their expressions will return 0 (false) or 1 (true) for each series accordingly.

Considering the whole expression, for each series, only one term, (v <= bool max_v) or (v > bool max_v) can be true, so for each series it'll return either the value on v or the maximum on max_v.

0
markalex On

Less hacky approach:

Consider v as initial metric, max_v - max clamping vector.
You can use combination of and and or operators to achieve clamping with instant vector:

v and (v < max_v) or max_v

Here for every time slot, value from v will be taken it its value less than value from max_v, otherwise will be taken value from max_v.