multiple values from grafana variable in prometheus query

Anil Kumar picture Anil Kumar · Dec 3, 2019 · Viewed 7.1k times · Source

We have a situation where we need to select the multiple values (instances/servers) from grafana variable field, and multiple values needs to passed to the Prometheus query using some regex, so that i can see selected hosts metrics in single graph. but i am not able to make it work. Can someone please help me with this.

lets take an example , if i select multiples values host1,host2,host3 and then the query should be looks something like this node_load1(instance="host1", instance="host2" , instance="host3").

Hope i made it clear my question.

Thanks in well advance.

Answer

Michael Doubez picture Michael Doubez · Dec 3, 2019

Grafana generates a regex for you when you use the variable in queries.

I assume you have a variable $host defined by collecting the label values. Ex:

label_values(node_load1, instance)

Then simply use the request:

node_load1{instance=~"$host"}