patternjavascriptTip
Grafana dashboard design: using variables and templating for reusable dashboards
Viewed 0 times
Grafana ^10.x
grafana variablestemplate variableslabel_valuesdashboard provisioning$__rate_intervalprometheus querydashboard as code
Problem
Grafana dashboards are hardcoded for a single environment or service instance. When the same dashboard is needed for staging, production, or multiple service instances, teams create duplicate dashboards that drift out of sync over time.
Solution
Use Grafana dashboard variables (also called template variables) to parameterize queries. Variables appear as dropdowns at the top of the dashboard.
Common variable patterns:
Store dashboards as JSON in version control and deploy via Grafana provisioning or the API.
- Go to Dashboard Settings > Variables > Add variable
- Type: Query, Data source: Prometheus
- Query:
label_values(http_requests_total, service)to list all services - Use in panels:
rate(http_requests_total{service="$service"}[5m])
Common variable patterns:
$environmentfrom label_values for env selection$instancefrom label_values for per-pod drilldown$__rate_interval(built-in) instead of hardcoded[5m]for rate windows
Store dashboards as JSON in version control and deploy via Grafana provisioning or the API.
Why
Templated dashboards reduce maintenance burden from O(environments * services) to O(1). They also enable on-call engineers to quickly scope a dashboard to a specific instance without context-switching.
Gotchas
- $__rate_interval adapts to the scrape interval automatically — always prefer it over hardcoded intervals
- Variables with 'All' option use a regex join by default which can break some queries — test explicitly
- Dashboard JSON in version control should be exported without the 'id' field to avoid conflicts on import
- Use repeat panels (not repeat rows) when you want each variable value to have its own panel
Context
Building Grafana dashboards that work across multiple environments or service instances
Revisions (0)
No revisions yet.