Define reusable, global template variable from query result that is updated on dashboard refresh (metrics pull)

  • What Grafana version and what operating system are you using?
    Grafana 8.2.1 Docker in Linux container
    Prometheus is data source for metrics

  • What are you trying to achieve?
    I’m trying to define a reusable, global template variable from a query result that is updated on dashboard refresh. Similar to a template that can be defined for a given dashboard under settings (and potentially can be hidden), with two key differences:

(1) the value of the var needs to be updated on every dashboard refresh, when new metrics are pulled from Prometheus
(2) it should allow to run operations/processing on it, same as in the query field in a panel

Simplified example:
$my_var = sum (rate(node_cpu_seconds_total{job=“node_exporter”}[$__rate_interval]))

Reasons:
(a) the query in the simplified example above is used in various panels
(b) centralized code, much easier to update
(c) it would be more efficient to just query ONCE and process it and then just reuse the end result

  • How are you trying to achieve it?
    Well, you tell me, that’s why I’m here :wink:

But, tried to…

(I) use a template variable for that. It can be hidden, so that’s good, but it does not meet requirement (1) from above, it can only be updated on dashboard load or time range change, and it also does not meet requirement (2), it does not allow operations/processing.

(II) use shared query results between panels. Unfortunately, and I’m not sure why, the use case scenarios for these are VERY limited, as a query from another panel cannot be used as a subquery in another panel… which would be the main use case scenario for this.

  • What happened?
    Both approaches do not meet the requirements stated above.

  • What did you expect to happen?
    Was hoping that template vars would have config options to meet requirements (1) and (2) from above.

Lmk please if there currently is a way to achieve this.

Thanks.