Why can't I use a dashboard variable in an alert rule?
I have a dashboard with a variable that selects an environment, and a panel that uses it. I tried to build an alert from the same panel and it refuses, saying template variables are not supported in alert queries.
The query works perfectly on the dashboard, so the data and the syntax are fine. It is specifically the alert path that rejects it.
What is the reasoning, and what is the correct way to alert on something I currently look at with a variable?
@runner_rauf · 3d ago
If you want one alert per environment, write the query with the value hard-coded and create one rule per environment. Tedious by hand, and the reason alert rules should live in configuration files rather than be clicked together — then it is a loop in a template and the tedium disappears.
If you want one alert that covers everything, drop the filter entirely and let the query return a series per environment. Alerting evaluates each returned series separately, so one rule produces one alert per environment automatically, and a new environment is covered the day it appears without anybody editing anything.
The second is almost always what people want once they see it. The variable was there to let a human narrow the view; an alert does not need to narrow, it needs to fire per thing.
The detail that makes it work is putting the identifying label into the alert's labels and message, so the notification says which environment rather than just that something is wrong.
Reply
Report