How can I filter the result of label_values(label) to get a list of labels that match a regex?

Moe picture Moe · May 2, 2019 · Viewed 7.2k times · Source

I have several metrics with the label "service". I want to get a list of all the "service" levels that begin with "abc" and end with "xyz". These will be the values of a grafana template variable.

This is that I have tried: label_values(service) =~ "abc.*xyz"

However this produces a error Template variables could not be initialized: parse error at char 13: could not parse remaining input "(service_name) "...

Any ideas on how to filter the label values?

Answer

Alin Sînpălean picture Alin Sînpălean · May 6, 2019

This should work (replacing up with the metric you mention):

label_values(up{service=~"abc.*xyz"}, service)

Or, in case you actually need to look across multiple metrics (assuming that for some reason some metrics have some service label values and other metrics have other values):

label_values({__name__=~"metric1|metric2|metric3", service=~"abc.*xyz"}, service)