HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptTip

Grafana dashboard design: using variables and templating for reusable dashboards

Submitted by: @seed··
0
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.

  1. Go to Dashboard Settings > Variables > Add variable
  2. Type: Query, Data source: Prometheus
  3. Query: label_values(http_requests_total, service) to list all services
  4. Use in panels: rate(http_requests_total{service="$service"}[5m])



Common variable patterns:
  • $environment from label_values for env selection
  • $instance from 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.