Course contentsShow
← Cloud and Infrastructure
Lesson 1998 of 2,98942. Observability - MetricsFree lesson

Variables and Templating

Understand how to create dashboard variables for dynamic filtering and building reusable, parameterized dashboards.

Variables and Templating

You'll learn how to make Grafana dashboards flexible and reusable by using variables that let viewers filter and customize what they see without rebuilding queries.

What Are Dashboard Variables?

Think of variables as dropdown menus or input boxes at the top of your dashboard. Instead of hard-coding a specific server name, region, or namespace into every panel's query, you create a variable that users can change. When they select a different value, all panels using that variable automatically update.

For example, instead of creating 10 separate dashboards for 10 different Kubernetes namespaces, you create one dashboard with a $namespace variable. Users select the namespace they want, and all panels instantly filter to show only that namespace's metrics.

How Variables Work

Variables in Grafana are defined at the dashboard level and referenced in queries using the $variable_name syntax. Common variable types include:

  • Query variables: Populated by running a query against your data source (e.g., list all namespaces)
  • Custom variables: Manually defined comma-separated values
  • Interval variables: Time intervals for aggregation
  • Text box variables: Free-form user input

In your PromQL queries, you might write:

rate(http_requests_total{namespace="$namespace"}[5m])

When a user selects production from the dropdown, Grafana substitutes $namespace with production before executing the query.

Why Templating Matters

Templating transforms static dashboards into dynamic tools. You build once, use everywhere. It reduces dashboard sprawl, simplifies maintenance, and empowers viewers to explore data without needing to edit queries or understand PromQL deeply.

Key Takeaway: Variables and templating turn hard-coded dashboards into flexible, reusable tools by letting users dynamically filter metrics through dropdown selections and parameters.