Maifee Ul Asad
Maifee Ul Asad

Reputation: 4627

Running multi-select variable from grafana to prometheus query

I've a prometheus query looking like this:

api_latency

And it gives me all kinds of cool stuff, all I want to know. To mention, it is a custom metric.

And I can make it fetch data with some condition, like this:

api_latency{uri="/ping"}
api_latency{uri="/pong"}

I can even join them like this:

api_latency{uri=~"/ping|/pong"}

But what I want is, from the grafana dashboard, I will pickup a variable, and based on that I will execute this query. This dowpdown in grafana is a multi-select dropdown.

How can I do so?

Upvotes: 1

Views: 1637

Answers (1)

markalex
markalex

Reputation: 13414

You can do it bu introducing dashboard variable with Multi-value option

Dashboard settings > Variables > + New variable

  • Select variable type: Query
  • Name: as you want, in this example it will be uri
  • Query: label_values(api_latency, uri)
  • Selection options: enable Multi-value (you can also enable Include All option if needed)

Now you'll see preview of values above the button Apply. And when you save changes and come back to panel, you'll see drop-down with variable in the left upper corner of dashboard.

Now you can simple use this variable in your queries, like this:

api_latency{uri=~"$uri"}

$uri will be automatically substituted by Grafana with /ping|/pong if you select those two as variables. Note that this substitution depends on data source, and it could be substituted with other format value for other data sources, more about it here.

Upvotes: 1

Related Questions