Grafana Query using nested variables


I’m trying to pull some metrics from a Prometheus data source using a query which requires nested variables.

I have defined three Custom Variables in my dashboard:

site = siteA,siteB
siteAhosts = server1,server2,server3
siteBhosts = server4,server5,server6

My panel query is simply calculating the cpu usage for the hosts selected:

(100 - cpu_usage_idle{host=~“$host”})

Note that this is just an indication of how I was using this query in the past when I had a single variable defined called “host”. Now I want to use nested variables in this query.

If siteA is selected as the “site”, then the query needs to be…

(100 - cpu_usage_idle{host=~“$siteAhosts”})

With the variable expanding to “server1,server2,server3”

If siteB is selected, then then the query needs to be…

(100 - cpu_usage_idle{host=~“$siteBhosts”})

With the variable expanding to “server4,server5,server6”

I’ve tried something like this…

(100 - cpu_usage_idel{host=~“${site}hosts”})

Which expands the site variable to siteA, but it fails to then expand the resulting siteAhosts variable and just treats it as a value.

Any ideas how I can achieve this? It’s driving me nuts :slight_smile: