How to create Grafana alerts with InfluxDB and the Flux query language

There have not been a lot of easy-to-follow examples using Flux for creating Grafana Alerts, so with the kind permission of the Grafana Labs team, I put together 5 examples that use Flux queries which can be used with Grafana Alerting. Hope this helps others who might be just getting started with Flux and Grafana Alerting.

Please reply here if there is any feedback or questions.

2 Likes

Awesome! One thing I can’t get to work is alerts for future values, how would you do that? I have a set of forecasted values and would like to alert based on values exceeding the threshold in there. Alerts don’t seem to allow values into the future.

@francisbehnen

I agree that it seems the Alerts time selector window does not allow future times.

I also could not write the time directly into the |> range(start:

Wow! Thank you for sharing!

So it’s not possible at all? That’s a big bummer

Hi @grant2! Thank you so much for this tutorial.

I have a question. I was able to set up some alerts with influxv2 and Grafana as you demonstrated.

Something I am having trouble with is configuring the notification message. I’d like to add a template annotation and label with variables outside of the alert like shown in this tutorial:

I am not sure how to bring in other variables into the annotation.

For example, my alert flux query is for variable “disp_st” in measurement “display” but when the alert goes off I want to the message to display another variable from another measurement.
Are you aware of how to do this?

Thank you.

Hi @celeste

I believe Example 2 would be a good starting point since the alert message template is built from two different buckets, each with different measurements. I no longer have that test data around, so I cannot do a quick test at the moment, but yes, I believe it would / should be possible. Will try to get a better answer later.

1 Like

Hi Grant! Did you get a chance to look into this? I am still working on getting this to work.
I added two queries to my alert. A grabs a display state and B grabs a device serial number that I need in the alert. I am still not able to get the serial number to print out on the alert.

I added this to the custom annotation Serial Number = {{ $values.B.Value }} but I get [no value].

I am not sure what information you need from me to help trouble shoot this. Thank you so much!

Hi @celeste Sorry, not yet, but I will try to get at it soon.

Thanks Grant! I have a specific question regarding this.

I made a functional alert and now I am adding a second query to provide more information to the alert.

I have been able to get an output for my second query in “B”
It is in this table form (see picture).

Do I need to make a separate “reducer” or can I put it directly in my custom annotations like something like this:

Serial Number = {{printf $values.B.sn }}

My main two issues when I do this above is it stops displaying other information from query in “A.”
And displays this:
Serial Number = {{printf $values.B.sn }} instead of the actual number.