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

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

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
  • 1
    What do you mean by "templating" here?
    – OneTwo
    Jan 23, 2019 at 12:17
  • 3
    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 Jan 23, 2019 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, 2019 at 0: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

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
  • 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, 2022 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.