31

Hi I want to create a simple alert in grafana to check whether there is no data for the last 5 minutes.

But I get an error

Template variables are not supported in alert queries

Well, according to this issue templates are not supporting in grafana yet. I have two questions:

  1. What is templating?

  2. How can I avoid this error?

enter image description here

Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56

4 Answers4

29

Under the Metrics tab, add new metric that will be hidden in the chart and is used for alerting only. Duplicate the query and remove all template variables (i.e. $somevar) from it. Replace the template variable with a hard-coded value you want to create alert for. Hide the metric by clicking on the “eye” icon.

Source: https://community.grafana.com/t/template-variables-are-not-supported-in-alert-queries-while-setting-up-alert/2514/8

ruhong
  • 1,793
  • 5
  • 28
  • 34
10

Dont Use templating in Grafana while creatig alerts as it doesn't support templating in alerting.

Try to Hardcode the whole formula and then give a try.

In easy language dont use Drop Down or templating variable which you have defined in templating section on top

Templating are for dynamic dashboards when you dont want to use formula again and again. You can repeat the graphs of each value selected in variable of templating

Bilal Ali Jafri
  • 915
  • 1
  • 6
  • 17
  • 1
    What do you mean by "templating" here? – OneTwo Jan 23 '19 at 12:17
  • 3
    It means a variable selected using drop down menu and use it in query. For further Information about templating visit http://docs.grafana.org/reference/templating/ – Bilal Ali Jafri Jan 23 '19 at 13:47
  • 1
    If you usually use template dashboards, would you just create a hardcoded one specifically for altering purposes? Are you aware why this isn't supported? – leeman24 Apr 27 '19 at 00:51
8

regarding your screenshot, you are using the condition

WHEN last() of query(A,5m,now) HAS NO VALUE

so the part with

query(A,5m,now)

is reusing the query from "Metrics" tab, and if you are using a variable inside this query then the alert is reporting this error

look at this simple query:

up{job="node_exporter", instance="$instance"}

here I want to use as instance the user selected VM name from drop-down menu, which is represented by the variable $instance

if I create an alert on this query, then I will get the error Template variables are not supported in alert queries

didlich
  • 119
  • 1
  • 3
7

It means that you have to use hard coded variables inside your queries.

This is bad:

where host =~ /^$host$/

This is good:

where host =~ mymachine.com

Your problem is located inside you metrics.

Marcel Zebrowski
  • 947
  • 10
  • 17
  • Just to add, why it is bad - because you don't want to mistakenly change the variable from the selection dropdown and trigger unnecessary alerts. – Satya Jul 25 '22 at 07:57