3
votes

I would like a Grafana variable that contains all the Prometheus metric names with a given prefix. I would like to do this so I can control what graphs are displayed with a drop down menu. I'd like to be able to display all the metrics matching the prefix without having to create a query for each one. In the Grafana documentation under the Prometheus data source I see:

metrics(metric) Returns a list of metrics matching the specified metric regex.

-- Using Prometheus in Grafana

I tried creating a variable in Grafana using this metrics function but it didn't work. See the screenshot for the variable settings I have:

settingsenter image description here

As you can see the "Preview of values" only shows "None"

1

1 Answers

3
votes

In promql, you can select metrics by name by using the internal __name__ label:

{__name__=~"mysql_.*"}

And then, you can reuse it to extract the metrics name using query label_values():

label_values({__name__=~"mysql_.*"},__name__)

This will populate your variable with metrics name starting with mysql_.

You can get the same result using metrics(); I don't know why it doesn't work for you (it should also works with prefix):

metrics(mysql_)