Variables & Templating
One dashboard for every service, route, and instance
Without variables, you'd build one dashboard per service, then one per environment, and keep them in sync forever. Variables turn a dashboard into a template: drop-downs at the top whose values are substituted into every query.
Query variables
The most common kind fills its options from Prometheus. The RED
dashboard defines $route:
Name: route
Type: Query
Data source: Prometheus
Query: label_values(http_requests_total{job="demo-app"}, route)
Multi-value: on
Include All: on
Refresh: On time range changeAnd every panel uses it with a regex matcher:
sum(rate(http_requests_total{job="demo-app", route=~"$route"}[$__rate_interval]))Use =~ not =. With multi-value or "All" selected, Grafana
substitutes a regex alternation like (/api/orders|/api/search), which
only works with a regex matcher.
Prometheus variable functions
Chained variables
Put variables in dependency order and reference the earlier ones in later queries:
$job label_values(up, job)
$instance label_values(up{job="$job"}, instance)
$route label_values(http_requests_total{job="$job", instance=~"$instance"}, route)Choose a job, and the instance list narrows to that job's instances. This is how one dashboard serves fifty services.
Built-in variables
$__range is useful for stat panels that should cover exactly the
selected period:
# availability over the selected time range
sum(increase(http_requests_total{job="demo-app", status!~"5.."}[$__range]))
/ sum(increase(http_requests_total{job="demo-app"}[$__range]))Set that panel's query type to Instant, or it recomputes the full range at every step.
Other variable types
| Type | Use |
|---|---|
| Custom | A fixed list: 0.5,0.9,0.99 for a $quantile picker |
| Interval | Lets viewers choose a window: 1m,5m,15m,1h |
| Constant | Hidden value, e.g. an SLO target used in several panels |
| Data source | Switch the whole dashboard between Prometheus servers (prod/staging) |
| Ad hoc filters | Viewers add arbitrary label=value filters to every query |
| Text box | Free input, e.g. a pod name substring |
A data source variable is the professional move for multi-environment
setups: one dashboard, and a drop-down that switches between the
prometheus-prod and prometheus-staging data sources.
Repeating panels and rows
Set Repeat by variable on a panel and Grafana clones it once per selected value. Select three routes and you get three latency panels, one each. Rows can repeat too, giving you a full block of panels per instance.
Repeating over a variable with hundreds of values, like every instance in a large fleet with "All" selected, renders hundreds of panels, each running its own queries. The dashboard becomes unusable and puts real load on Prometheus. Repeat only over small, bounded sets.
Links and drill-down
Variables make drill-down possible. On a panel, add a data link:
/d/demo-app-red/demo-app-red?var-route=${__field.labels.route}&${__url_time_range}Clicking a series on an overview panel then opens the detailed dashboard, filtered to that route, over the same time range. Build overview → service → instance chains like this and on-call engineers never have to retype a filter.
Lab
- Open Demo App · RED, select only
/api/searchin the Route drop-down, and watch every panel update. - Edit the dashboard and add a Custom variable
quantilewith values0.5,0.9,0.99. - Change the p99 stat panel's query to use
histogram_quantile($quantile, ...)and its title top$quantile latency. - Add a data link on the Requests by route panel that opens the same
dashboard with
var-routeset to the clicked series.