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

3
  • Did you ever figure this out? Commented Jul 11, 2018 at 19:35
  • @Darryl unfortunately no Commented Jul 12, 2018 at 2:57
  • per this comment from Jan 2021 github.com/grafana/grafana/issues/6557#issuecomment-760228091 they are reworking the alerting system and they hopefully allow for alerts for queries with variables in them. Commented Mar 10, 2021 at 10:18

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

What do you mean by "templating" here?
It means a variable selected using drop down menu and use it in query. For further Information about templating visit docs.grafana.org/reference/templating
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?
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

Comments

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.