multiple values from grafana variable in prometheus query
Asked Answered
N

2

28

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.

Nicolella answered 3/12, 2019 at 13:30 Comment(0)
C
30

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"}
Cloche answered 3/12, 2019 at 20:56 Comment(2)
Thanks for providing the answer, it is working. I think when i was trying i missed "~" part , my bad.Nicolella
or i might have looking with some other job name. I completely missed something when i was testing or before asking here :(Nicolella
J
26

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://mcmap.net/q/489592/-multiple-values-from-grafana-variable-in-prometheus-query

Jd answered 22/3, 2022 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.