How to change Grafana query dynamically based on given variable?

  • What Grafana version and what operating system are you using?

  • What are you trying to achieve?

I’d like to change the queries in the panels, dynamically based on given variable (user input).

  • How are you trying to achieve it?

I have defined a single “custom” Variable called “app”. My “Custom Options” is like: “foo : bar, bar : baz, baz : qux” with “Label and value” type.

So that I can use that label:

avg(avg_over_time(my_custom_metric_exposed_by_foo{job="${app}"}[$__rate_interval])) * 100

But I’d like to change the metric based on given label. Imagine my_custom_metric_exposed_by_foo is not exposed by bar and baz.

For example, if I choose the bar, i’d like to adjust to query to use my_custom_metric_exposed_by_bar.

  • What happened?

How can I do that?

  • What did you expect to happen?

  • Can you copy/paste the configuration(s) that you are having problems with?

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

  • Did you follow any online instructions? If so, what is the URL?

Yes, but no luck. Its outdated and for SQL queries:

Hi,

What’s changing in your query? Only name of the metric? If so, you can also filter that in

{__name__=~"${var}"}

way, so your query could look like:

avg(avg_over_time({__name__=~"my_custom_metric_exposed_by_${app}", job="${app}"}[$__rate_interval])) * 100

If you need to change an entire query, you can map those to custom variable (given that the key value pairs are not used), e.g.:
foo : sum(rate(go_gc_cycles_automatic_gc_cycles_total[5m])), bar : vector(0)
and then just use the variables like that:

1 Like

Thank you!

Only the name of the metric is changing the query, right. And yes, I’d like to change the entire query but per panel. I could pass the query as a custom value but imagine there are N panels and want to replace for each one.

Any thoughts on this? I’ve check the issues but couldn’t find anything.

I don’t think I understand tbh. Let’s say you have a dashboard with two panels - panelA and panelB. Also, you have a variable there with possible values foo, bar, and baz (I know you have the key-value pairs, but for simplicity let’s omit those).
panelA has query: sum(rate(metric_foo)) and
panelB has query avg(rate(metric_foo))

Now I change foo to bar in the variable list. What would you like to happen? Also please tag me in your response, cause I don’t get notifications and otherwise I’m not going to see your response :sweat_smile:

Hey @dawiddebowski,

I have a dashboard with ONE panel. I have a variable with N possible values. I want to change the entire query for the entire panel by given query.

With your variable you could do something like this then:

Then, upon changing the variable, the query would also change:

Thanks @dawiddebowski, but what if i have multiple panels and want to change on each based on provided variable? It seems this way would only support for one panel. (Since we can only pass one query per one variable)

You could always create one hidden variable per one panel. But I fail to understand why would you want to change all the queries. What’s your use-case for that? I mean I would find it pretty confusing.