PromQL/MetricsQL if value in range divide by constant value or map function

443 views Asked by At

I'm trying to divide the value by 10, if the value is inside an defined range.
Source is VictoriaMetrics and Grafana with Prometheus plugin is used to query the data.

I tested this code but either get an error message or the values to be divided end up as one curve instead of two.

MetricsQL

WITH (
    data = {__name__=~"MyMeasurement_[A-Z].*",unit="°C"} > 0 < 120,
    data1 = {__name__=~"MyMeasurement_[A-Z].*",unit="°C"} > 120 < 1000
)
(data or (data1/10))
({__name__=~"MyMeasurement_[A-Z].*",unit="°C"} if ({__name__=~"MyMeasurement_[A-Z].*",unit="°C"} < 120)) default ({__name__=~"MyMeasurement_[A-Z].*",unit="°C"})`

With Influx this code works fine:

|> map(fn: (r) => ({
      r with _value:
        if r._value >= ${tempSplitLimit} and r._value < 1000 then float(v:r._value) / 10.0
        else float(v:r._value)
    }))

Image:

Original Data

Divided by 10

Update:

The problem seems to be, that the data not added at the same time. So VictoriaMetrics return a table with some undefined entry.

The or operator fill the empty fields with data from another signal.

Compare

My current solution is to make separate queries for each name.

1

There are 1 answers

5
valyala On

Try the following MetricsQL query:

WITH (
    q = {__name__=~"MyMeasurement_[A-Z].*",unit="°C"}
)
(q < 120) or ((q >= 120)/10)