Grafana templating: Regex for Prometheus label_values variables
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"}
Related videos on Youtube
Pieter Moens
Updated on June 04, 2022Comments
-
Pieter Moens about 2 years
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.
-
Pieter Moens almost 5 yearsAwesome! _name_ was exactly what I was looking for :) Thank you Using this with label_values as follows:
label_values({__name__=~"app1_.*"}, uid)
-
brian-brazil almost 5 yearsI'd also suggest looking at robustperception.io/extracting-labels-from-legacy-metric-namโโes
-
Jananath Banuka almost 4 yearsHi, what is
name
here? is it a predefined keyword? -
Erik Finnman about 3 yearsYes,
__name__
is a internal label in Prometheus: prometheus.io/docs/prometheus/latest/querying/basics