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 documentation. 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)

3 Likes

Thanks for this… One use case which we’ve found useful, is to define a variable (in this case, group, which is populated from a prometheus query, then filter using regex and net/${group}/.*, to refine the results from a cloudwatch query, and get the AWS NLB id corresponding to the ${group}