Grafana templating: Regex for Prometheus label_values variables

Pieter Moens picture Pieter Moens · Jul 30, 2019 · Viewed 7.8k times · Source

I am trying to setup templating in Grafana using the label_values function. The documentation specifies the possibility to query label_values like:

label_values(metric, label)

In my use case there are two main metric groups with names similar to:

  • app1_current_sensor1
  • app1_current_sensor2
  • app2_current_sensor2
  • app2_current_sensor3

Each of them has a label named 'uid'. I'm looking to use the above query to filter only the user ids of the 'app1' on one dashboard and 'app2' on another dashboard.

I've tried

label_values(app1_current_sensor1, uid)

But if for some reason sensor1 does not send data for a while I won't be seeing any more user ids on the dashboard even though sensor2 is sending data.

Would it be possible to use a regex as input for the metric variable? Something like this would work for me:

label_values(metric=~(app1_[^\s]+), uid)

But I'm not sure if this is possible in Grafana.

Answer

Kamol Hasan picture Kamol Hasan · Jul 30, 2019

The following expression selects all metrics that have a name starting with job_ and have label method="GET"

{__name__=~"job_.*", method="GET"}

To get all metrics whose name start with app1_ use

{__name__=~"app1_.*"}

To get all metrics whose name start with app1_ and uid equal to some specific value, use

 {__name__=~"app1_.*", uid="value"}