Grafana templating: Regex for Prometheus label_values variables
Asked Answered
P

1

6

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.

Preceptive answered 30/7, 2019 at 7:51 Comment(0)
A
15

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"}
Algebraist answered 30/7, 2019 at 10:47 Comment(4)
Awesome! _name_ was exactly what I was looking for :) Thank you Using this with label_values as follows: label_values({__name__=~"app1_.*"}, uid)Preceptive
I'd also suggest looking at robustperception.io/extracting-labels-from-legacy-metric-namesWheelsman
Hi, what is name here? is it a predefined keyword?Flite
Yes, __name__ is a internal label in Prometheus: prometheus.io/docs/prometheus/latest/querying/basicsGlennaglennie

© 2022 - 2024 — McMap. All rights reserved.