If query statement

Hello!

Do i have any possibility to “manipulate” the values of a metric exept using math operators, [any]_over_time, any funktion that is combining / grouping values…

For example like - if a metric-value > 0 then set metric-value 1

I just ended up that i was able to filter and only passing values > 0

Thank you!
Kind Regards chackl

3 Likes

any luck @chackl ?

You could use the sgn function.

sgn(metric{label="value"}) # returns -1 or 1

With a bit of manipulation, you can return 0 or 1.

sgn(metric{label="value"}) / 2 + 0.5 # returns 0 or 1

If you think about it, that second query is like an “if” statement determining if the value is greater than 0. You can be clever and generalize this, if needed, by shifting the trend up or down.

sgn(metric{label="value"} - 10) / 2 + 0.5 # returns 0 or 1

Basically, this translates to:

if (metric > 10) {
    return 1;
}
return 0;

You can change “10” to any value here.


Just for completeness, if you wanted to only show values above (or below) a certain threshold, you can write that directly in the query too.

metric{label="value"} > 10 # only returns values > 10
1 Like