How do I set a panel's metric dynamically using variables?

I have two metrics that have the same information but they have a different prefix.

  • default_http_seconds (which maps to version1)
  • version2_http_seconds (which maps to version2)

All I want is to very simply update the metrics used in a dashboards individual panels based off of a dropdown variable. So given the user selecting either, “v1” or “v2” for their version, they would see the appropriate metrics displayed in the dashboard. This is exceedingly difficult to try to do with the UI.

I have tried using a custom variable, but when I use those variables in the query, it doesn’t parse it as the metric name but instead seems to parse it as a string.
e.g. ${version}_http_seconds doesn’t work in the panel query because it seems to interpret it as “version2_http_seconds” instead of the actual metric.

I have tried a query variable type where I have a regex that matches those specific prefixes so both show up in the dropdown. However, I want the dropdown to show as “v1” and “v2”. The query variable type doesn’t allow me to modify the display value of that metric.

Constant variables work but they don’t show up on the dashboard.

How am I supposed to accomplish this? It seems like it should be so simple.

1 Like

Hi @pepiopee and welcome to the Grafana forum.

Using the tools you have explored, I think the regex method (where you get “default” and “version2” instead of “v1” and v2") is the only way to get close to what you want. For others’ reference, here is the doc.

Perhaps this plugin will do what you want?