Alerting in InfluxDB 2.0 using Flux

Is there a way to write a little more advance alerting task using Flux in Influx 2, something similar to Kapacitor? As far as I understand, we can only set a constant threshold using the UI.

Edit: I have a time series, and I want to know if series A exceeds B? I don’t have a constant threshold. How can I handle this?

Hello @Mert,
Absolutely you can craft custom tasks, checks, and notification rules. Have you seen InfluxDB’s Checks and Notifications System | InfluxData? Please give it a read and let me know if you still have any questions.

To write a check see whether field A exceeds field B would look something like:

package main 
import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"

data = from(bucket: "telegraf")
|> range(start: -5s)
|> filter(fn: (r) =>(r["_measurement"] == "my_measurement"))
|> filter(fn: (r) =>(r["tag"] == "my_tag"))
|> filter(fn: (r) =>(r["_field"] == "A" and "r["_field"] == "B"))

option task = {name: "Comparison Check", every: 5s, offset: 0s}
check = {_check_id: "074bba32a48c3000", 
  _check_name: "Comparison Check",
  _type: "threshold",
 tags: {},}

crit = (r) =>r["A"] > r["B"]
messageFn = (r) =>("Check: ${ r._check_name } is: ${ r._level }")

|> v1["fieldsAsCols"]()
|> monitor["check"](data: check, messageFn: messageFn, crit: crit)

@Mert Please let me know if that meets your needs. Or if you have trouble with it, but also please consider giving that blog post a look. Thank you.

@Anaisdg Thanks,

Your example works fine generally. I’ve some questions if you don’t mind.

  • Is there a difference between monitor["check"]() and monitor.check()? Both seems working?
  • In your example, we didn’t use |> last() or |> mean(), so there may be more than one observation.
    How is the crit function works in that case? I assume it’ll return the result for the last row?

Edit: I read the blog post. I wish I had found the blog post this morning. It’s would be a good start. :smiley:

1 Like

Hello @Mert,
No that’s just different syntax for the same thing.
So the predicate functions messagfn() and crit(), ok(), info(), etc depending on what the user inclues are passed into the monitor.check() function. The monitor.check function takes the input and assigns a level and message for each row using the predicate functions.

I’m glad the blog was useful. Please let me know if there are points of confusion or anything I need to append/explain further.