Grafana Chained Template Variables Prompt
Design Grafana dashboard template variables that chain (query → query → panel) so region, cluster, namespace, and pod filters cascade correctly without slow or broken dropdowns.
- Target user
- Dashboard authors and observability engineers building reusable Grafana dashboards
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior observability engineer who designs Grafana dashboard template variables that chain cleanly so one dashboard serves every environment.
I will provide:
- The datasource type (Prometheus, Loki, SQL, CloudWatch)
- The dimensions to filter on and their hierarchy (e.g. region → cluster → namespace → pod)
- Whether the dashboard is single- or multi-select and the default scope
Your job:
1. **Variable hierarchy**: define an ordered set of variables where each `query` filters on the value of the one above using `$upper` interpolation, so selecting a cluster narrows the namespace list, etc.
2. **Variable types**: choose correctly among `query` (datasource-driven), `custom` (static list), `interval` (`$__interval`/`$__rate_interval` tuning), `datasource` (multi-cluster dashboards), `constant`, and `textbox`.
3. **Prometheus queries**: use `label_values(metric{filter=~"$parent"}, label)` — always scope by a metric and the parent variable so the list is small and relevant, not `label_values(label)` across everything.
4. **Multi-select & All**: set `includeAll`, a correct `allValue` (often `.*` for Prometheus regex, empty for exact-match datasources), and `multi` intentionally; wire panel queries with `=~"$var"` so regex/All works.
5. **Interpolation format**: pick the right `${var:regex}`, `${var:pipe}`, `${var:csv}`, or `${var:sqlstring}` format so the value is valid in the target query language.
6. **Performance**: set `refresh` to `On dashboard load` vs `On time range change`, and add a `regex` extract/filter to trim noisy values; avoid re-querying huge label sets on every render.
7. **Sorting & dedup**: enable numeric/alphabetical `sort` and use `regex` capture groups to clean up display values.
8. **Ad hoc filters**: when appropriate, add an `adhoc` variable for free-form label filtering, and note it applies globally to matching datasources.
Explain each choice, then output the dashboard `templating.list` JSON and one example panel query that consumes the variables.
---
Datasource: [DESCRIBE]
Dimensions + hierarchy: [DESCRIBE]
Single/multi-select + defaults: [DESCRIBE]
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Why this prompt works
Chained variables are where reusable dashboards live or die: get the interpolation format or the =~"$var" matcher wrong and every panel returns “templating failed to load” or empty data. This prompt forces parent-scoped label_values queries, deliberate includeAll/allValue/multi choices, and the correct ${var:format} per query language so cascading dropdowns stay fast and correct.
How to use it
- State the exact hierarchy so each child query is scoped by its parent.
- Name the datasource so interpolation format and query syntax are right.
- Decide single vs multi-select up front — it changes
allValueand the panel matcher. - Test the “All” path explicitly; it’s the most common silent bug.
Useful commands
# Preview what a Prometheus variable query returns
curl -s -G "http://prometheus:9090/api/v1/label/namespace/values" \
--data-urlencode 'match[]=kube_pod_info{cluster="prod-eu"}' | jq '.data | length'
# Export a dashboard to inspect templating.list
curl -s -H "Authorization: Bearer $TOKEN" \
http://localhost:3000/api/dashboards/uid/$UID | jq '.dashboard.templating.list'
Example config
templating.list for a region → cluster → namespace chain:
{
"templating": {
"list": [
{
"name": "region",
"type": "query",
"datasource": { "uid": "prometheus-prod" },
"query": "label_values(kube_pod_info, region)",
"refresh": 1,
"sort": 1
},
{
"name": "cluster",
"type": "query",
"datasource": { "uid": "prometheus-prod" },
"query": "label_values(kube_pod_info{region=~\"$region\"}, cluster)",
"refresh": 2,
"includeAll": true,
"multi": true,
"allValue": ".*"
},
{
"name": "namespace",
"type": "query",
"datasource": { "uid": "prometheus-prod" },
"query": "label_values(kube_pod_info{cluster=~\"$cluster\"}, namespace)",
"refresh": 2,
"includeAll": true,
"multi": true,
"allValue": ".*"
}
]
}
}
Example panel query consuming the chain:
sum by (namespace) (
rate(container_cpu_usage_seconds_total{cluster=~"$cluster", namespace=~"$namespace"}[$__rate_interval])
)
Common findings this catches
- Empty “All” → wrong
allValue, or=instead of=~in the panel. - Slow dropdowns → unscoped
label_valuesover high-cardinality labels. - “Templating failed to load values” → child query references a parent that returned nothing, or wrong interpolation format.
- Stale options →
refresh: never. - Duplicate/ugly values → missing
regexcleanup andsort.
When to escalate
- Very high-cardinality label design — metrics/cardinality owner.
- Cross-datasource variable strategy (mixed Prometheus/SQL) — platform team.
- Standardizing a variable convention across many dashboards — observability lead.
Related prompts
-
Grafana Ad-hoc Filters Variable Prompt
Add an ad-hoc filters template variable so viewers can dynamically filter dashboards by any label without editing queries.
-
Grafana Annotations & Region Events Prompt
Design Grafana annotations — native, query-driven, and region annotations — to overlay deploys, incidents, and maintenance windows on dashboards via the API and provisioning.
-
Grafana Dashboard JSON Model Review Prompt
Audit a Grafana dashboard's raw JSON model for portability, performance, and correctness — datasource references, variable wiring, refresh, and hidden anti-patterns.
-
Grafana Loki LogQL Panel Design Prompt
Design Grafana panels backed by Loki LogQL — log volume, error-rate, and extracted-metric panels — that stay fast by filtering on stream labels before parsing.
More Grafana prompts & error guides
Browse every Grafana prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.