Dashboard Variables and Templating
Now that you've mastered building dashboards and creating visualizations, let's make them truly dynamic and reusable. In this lesson, you'll learn how to use variables and templating to create dashboards that adapt to different contexts, making your monitoring setup more flexible and powerful.
Learning Goals:
- Understand different types of dashboard variables
- Create and configure template variables
- Use variables in queries and panel titles
- Build interactive dashboards with dropdowns and search
- Apply best practices for variable organization
What Are Dashboard Variables?
Dashboard variables are placeholders that allow you to create dynamic, reusable dashboards. Instead of hardcoding values like server names, time ranges, or metric names, you use variables that users can change through dropdown menus, text inputs, or other controls.
Think of variables as parameters that make your dashboards adaptable. For example, you could create a single dashboard that shows metrics for any server, environment, or application by simply selecting from a dropdown.
Variable Types and Use Cases
Grafana supports several types of variables, each serving different purposes:
Query Variables
Query variables fetch their values from your data sources. This is perfect for dynamic lists that change over time, like server names or application instances.
Custom Variables
Custom variables use manually defined values. Use these for static lists that don't change frequently, like environments (dev, staging, prod) or regions.
Text Box Variables
Text box variables allow free-form text input. Ideal for search functionality or when users need to enter specific values.
Constant Variables
Constant variables have fixed values that don't appear in the UI. Use these to simplify complex queries or set default values.
Data Source Variables
Data source variables let users switch between different data sources within the same dashboard.
Creating Your First Template Variable
Let's start by creating a simple custom variable for environments.
- Open any dashboard and click the Dashboard Settings gear icon
- Navigate to Variables in the left menu
- Click Add variable and configure:
Variable Configuration:
- Name:
environment - Type:
Custom - Label:
Environment(this appears in the UI) - Values:
dev,staging,prod
Use descriptive variable names that clearly indicate their purpose. Avoid abbreviations unless they're universally understood in your organization.
Using Variables in Queries
Once you've created variables, you can use them in your panel queries. Variables are referenced using the $varname syntax.
Let's see how to use our environment variable in a Prometheus query:
up{environment="$environment"}
For multiple values (when users can select multiple options), use:
up{environment=~"$environment"}
The =~ operator matches against a regex pattern, which is what Grafana uses when multiple values are selected.
Advanced Variable Configuration
Query Variables with Data Source Integration
Query variables can pull dynamic values from your data sources. Here's how to create a variable that lists all your servers:
SELECT DISTINCT server_name FROM metrics WHERE environment = '$environment' ORDER BY server_name
Or for Prometheus:
label_values(up, instance)
Chained Variables (Dependent Dropdowns)
You can create variables that depend on other variables. For example, first select an environment, then see only the servers in that environment.
First variable (environment):
Name: environment
Type: Custom
Values: dev,staging,prod
Second variable (server):
Name: server
Type: Query
Query: label_values(up{environment="$environment"}, instance)
Regex and Formatting Options
You can apply regex filters to clean up variable values:
.*server=(.+?);.*
Or use formatting options to ensure proper query syntax:
up{instance=~"$server"}
Practical Example: Multi-Server Dashboard
Let's build a complete example showing CPU usage across different servers and environments.
Variable Setup:
# Environment variable
- Name: env
Type: Custom
Values: development,staging,production
# Server variable (depends on environment)
- Name: server
Type: Query
Data source: Prometheus
Query: label_values(node_cpu_seconds_total{environment="$env"}, instance)
Multi-value: true
Include All option: true
CPU Usage Query:
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle",environment="$env",instance=~"$server"}[5m])) * 100)
Panel Title with Variables:
CPU Usage - $env - $server
Time Range Variables
Grafana provides built-in time range variables that you can use in your queries:
$__timeFrom()- Start of time range$__timeTo()- End of time range$__interval- Auto-calculated interval based on time range
rate(http_requests_total{environment="$environment"}[$__interval])
Common Pitfalls
-
Variable Scope Issues: Remember that variables are dashboard-scoped. Changes affect all panels using that variable.
-
Query Performance: Heavy variable queries can slow down dashboard loading. Use caching and consider custom variables for large, static lists.
-
Default Values: Always set sensible default values. An unset variable can break your entire dashboard.
-
Special Characters: Be careful with special characters in variable values. They may need escaping in your queries.
-
Multi-value Confusion: When using multi-value variables, remember to use regex matching (
=~) instead of exact matching (=).
Summary
Dashboard variables transform static dashboards into dynamic, reusable tools. You've learned to:
- Create different types of variables (custom, query, text box)
- Use variables in queries and panel titles
- Build dependent dropdowns with chained variables
- Apply regex and formatting for clean variable values
- Avoid common pitfalls in variable implementation
With templating, you can create powerful dashboards that adapt to different users, environments, and use cases while maintaining a clean, organized interface.
Show quiz
-
What syntax do you use to reference a variable named "server" in a Grafana query? a)
{{server}}b)$serverc)@serverd)&server -
Which variable type is best for a static list of environments (dev, staging, prod)? a) Query Variable b) Custom Variable
c) Text Box Variable d) Data Source Variable -
When using multi-value variables in Prometheus queries, which operator should you use? a)
=b)!=c)=~d)!~ -
What is the main advantage of using chained variables? a) Faster dashboard loading b) Dependent dropdowns that filter options c) Better visual appearance d) Automatic query optimization
Answers:
- b)
$server - b) Custom Variable
- c)
=~(regex match) - b) Dependent dropdowns that filter options