6

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"

Stratus3D
  • 4,648
  • 4
  • 35
  • 67

1 Answers1

7

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_)
Michael Doubez
  • 5,937
  • 25
  • 39
  • 1
    It looks like my syntax was wrong. I was doing `metrics(/mysql_*/)` because I thought it required regex, but `metrics(mysql_)` actually does what I want. Thanks! – Stratus3D Mar 27 '20 at 12:21