Variables in Query Regex Expression

Hello everyone!

I have a dashboard with these variables:
first
second
third
From my prometheus datasource, my metric have these labels:
001:first:label:x
001:first:label:y
002:second:label:x
002:second:label:y
003:third:label:x
003:third:label:y

In my dashboard panel query, I would like to get the metric values from the labels per variable.
I use the following query with a regex expression:
metric{label=~"(.+$variable.+)"}

This does not work, as the $variable is interpreted as a part of the regex string instead of inserting the value. (Doing metric{label=~"(.+first.+)"} works for getting the two first labels)

So how can I insert the actual variable value into the regex expression?

hello no this is not working

Check out Variable syntax | Grafana Labs. Specifically the two points:

  • $varname This syntax is easy to read, but it does not allow you to use a variable in the middle of a word. Example: apps.frontend.$server.requests.count
  • ${var_name} Use this syntax when you want to interpolate a variable in the middle of an expression.

Try to use the latter syntax (with curly braces) and see if that helps.

(Disclaimer: I don’t use Prometheus)