Learn Labs
Grafana

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 change

And 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

label_values(job)every value of a label, across all metricslabel_values(up, job)values of job on one metric (faster)label_values(http_requests_total{job="$job"}, route)chained: depends on $jobmetrics(http_.*)metric names matching a regexquery_result(topk(5, sum by (route) (rate(http_requests_total[1h]))))any query; use regex to extract

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

$__rate_intervalsafe window for rate(): max(4 x scrape interval, step + scrape interval)$__intervalthe step Grafana chose for this panel's width and range$__rangethe whole dashboard range, e.g. 6h, for "over the selected period" stats$__from / $__torange start/end in ms

$__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

TypeUse
CustomA fixed list: 0.5,0.9,0.99 for a $quantile picker
IntervalLets viewers choose a window: 1m,5m,15m,1h
ConstantHidden value, e.g. an SLO target used in several panels
Data sourceSwitch the whole dashboard between Prometheus servers (prod/staging)
Ad hoc filtersViewers add arbitrary label=value filters to every query
Text boxFree 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.

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

  1. Open Demo App · RED, select only /api/search in the Route drop-down, and watch every panel update.
  2. Edit the dashboard and add a Custom variable quantile with values 0.5,0.9,0.99.
  3. Change the p99 stat panel's query to use histogram_quantile($quantile, ...) and its title to p$quantile latency.
  4. Add a data link on the Requests by route panel that opens the same dashboard with var-route set to the clicked series.

On this page