Stage 6 · Operate
Dashboards with Grafana
Template Variables
Creating datasource, query, interval, custom, and ad hoc variables for reusable dashboards.
Variable Types
Template variables make dashboards reusable across services, environments, and clusters. Users select values from dropdowns, and all panels update to reflect the selection. Grafana supports several variable types for different use cases.
| Type | Source | Example |
|---|---|---|
| Query | Prometheus label values | job, instance, endpoint |
| Custom | User-defined list | production, staging, dev |
| Interval | Time intervals | 1m, 5m, 15m, 1h |
| Datasource | Grafana datasources | Prometheus, Loki |
| Text | Free-form input | Custom filter expressions |
Query Variables
Query variables pull values dynamically from Prometheus label values. The query field uses label_values() or query_result() functions. The variable updates automatically as new labels appear in Prometheus.
# All job values
label_values(http_requests_total, job)
# All endpoints for the selected job
label_values(http_requests_total{job="$job"}, endpoint)
# All instances
label_values(up, instance)Custom Variables
Custom variables let you define a fixed list of options. Use them for environment names, severity levels, or any predefined set. Custom variables are static and do not change with data.
{
"name": "environment",
"type": "custom",
"query": "production,staging,development",
"current": {
"selected": true,
"text": "production",
"value": "production"
}
}Interval Variables
Interval variables represent time ranges. They are used in rate() and increase() functions to let users change the aggregation window. The auto option calculates intervals based on the dashboard time range.
# Rate with variable interval
rate(http_requests_total[$interval])
# Increase with variable interval
increase(http_requests_total[$interval])Datasource Variables
Datasource variables let users switch between Prometheus instances. This is useful for comparing metrics across clusters, regions, or environments. The variable resolves to the selected datasource UID.
{
"name": "datasource",
"type": "datasource",
"query": "prometheus",
"current": {
"text": "Prometheus",
"value": "prometheus"
}
}Variable Chaining
Variables can reference other variables. A downstream variable filters its values based on the upstream selection. This creates cascading dropdowns where selecting a job filters the available endpoints.
# Variable: job = label_values(http_requests_total, job)
# Variable: endpoint = label_values(http_requests_total{job="$job"}, endpoint)
# Variable: instance = label_values(http_requests_total{job="$job", endpoint="$endpoint"}, instance)The All option with multi-value queries all label values at once. This can create expensive queries. Use it for overview dashboards but avoid it for detailed drill-down panels where specificity matters.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.