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.