23

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.

Anil Kumar
  • 381
  • 1
  • 2
  • 9

2 Answers2

25

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"}
Michael Doubez
  • 5,937
  • 25
  • 39
  • 2
    Thanks for providing the answer, it is working. I think when i was trying i missed "~" part , my bad. – Anil Kumar Dec 04 '19 at 07:34
  • or i might have looking with some other job name. I completely missed something when i was testing or before asking here :( – Anil Kumar Dec 04 '19 at 09:21
14

Note that node_load1{instance="host1",instance="host2",instance="host3"} won't return any results, since the label filters are applied with and operation, e.g. the query means:

select time series with node_load1 name and with instance="host1" label and with instance="host2" label and with instance="host2" label

You need to use or operation for multiple instance label values. This can be done with the following query, which uses regexp filter for instance label:

node_load1{instance=~"host1|host2|host3"}

See PromQL tutorial for more details.

Grafana automatically constructs the value1|...|valueN regexp for $var_name template when multiple label values are selected for this variable. So just use node_load1{instance=~"$host"} in queries in Grafana as suggested at https://stackoverflow.com/a/59165219/274937

valyala
  • 11,669
  • 1
  • 59
  • 62