This is documentation for the next version of Grafana documentation. For the latest stable release, go to the latest version.
OpenTSDB template variables
Instead of hard-coding server, application, and sensor names in your metric queries, you can use template variables. Variables appear as drop-down menus at the top of the dashboard, making it easy to change the data being displayed without editing queries.
For an introduction to template variables, refer to the Variables documentation.
Query variable
The OpenTSDB data source supports query-type template variables that fetch values directly from OpenTSDB. These variables dynamically populate based on data in your OpenTSDB database.
Supported query functions
Note
The
tag_namesandtag_valuesfunctions use the OpenTSDB lookup API, which requires metrics to exist in your database. Thesuggest_tagkandsuggest_tagvfunctions use the suggest API, which searches across all metrics.
Create a query variable
To create a query variable:
- Navigate to Dashboard settings > Variables.
- Click Add variable.
- Enter a Name for your variable (for example,
host). - Select Query as the variable type.
- Select the OpenTSDB data source.
- Enter your query using one of the supported query functions.
- Optionally configure Multi-value to allow selecting multiple values.
- Optionally configure Include All option to add an “All” option.
- Click Apply.
Query variable examples
List all metrics:
metrics()Returns all metric names in your OpenTSDB database. Useful for creating a metric selector.
List metrics with a prefix:
metrics(sys.cpu)Returns metrics starting with sys.cpu, such as sys.cpu.user, sys.cpu.system, sys.cpu.idle.
List tag keys for a metric:
tag_names(sys.cpu.user)Returns tag keys like host, env, datacenter that exist on the sys.cpu.user metric.
List tag values for a metric and tag key:
tag_values(sys.cpu.user, host)Returns all host values for the sys.cpu.user metric, such as webserver01, webserver02, dbserver01.
Search for tag keys by prefix:
suggest_tagk(host)Returns tag keys matching host across all metrics, such as host, hostname, host_id.
Search for tag values by prefix:
suggest_tagv(web)Returns tag values matching web across all metrics, such as webserver01, webserver02, web-prod-01.
If template variables aren’t populating in the Preview of values section, refer to Troubleshooting.
Nested template variables
You can use one template variable to filter values for another. This creates cascading filters, such as selecting a data center first, then showing only hosts in that data center.
Filter syntax
The tag_values function accepts additional tag filters after the tag key:
tag_values(metric, tagkey, tag1=value1, tag2=value2, ...)Use template variables as filter values to create dynamic dependencies:
tag_values(metric, tagkey, tag1=$variable1, tag2=$variable2)Nested variable examples
Create cascading filters
To create a hierarchy of dependent variables:
Create the parent variable:
- Name:
datacenter - Query:
tag_values(sys.cpu.user, datacenter)
- Name:
Create the child variable:
- Name:
host - Query:
tag_values(sys.cpu.user, host, datacenter=$datacenter)
- Name:
Create additional levels as needed:
- Name:
cpu - Query:
tag_values(sys.cpu.user, cpu, datacenter=$datacenter, host=$host)
- Name:
When users change the data center selection, the host variable automatically refreshes to show only hosts in that data center.
Use variables in queries
Reference variables in your queries using the $variablename or ${variablename} syntax. Grafana replaces the variable with its current value when the query executes.
Where to use variables
Variables can be used in these query editor fields:
Variable syntax options
Multi-value variables
When you enable Multi-value for a variable, users can select multiple values simultaneously. The OpenTSDB data source handles multi-value variables using pipe (|) separation, which is compatible with OpenTSDB’s literal_or filter type.
Configure multi-value variables
- When creating the variable, enable Multi-value.
- Optionally enable Include All option to add an “All” selection.
- Use the variable in a filter with the
literal_orfilter type.
Multi-value example
With a host variable configured as multi-value:
If the user selects webserver01, webserver02, and webserver03, the filter value becomes webserver01|webserver02|webserver03.
All value behavior
When the user selects “All”, Grafana sends all available values pipe-separated. For large value sets, consider using a wildcard filter instead:
Interval and auto-interval variables
Grafana provides built-in interval variables that are useful with OpenTSDB downsampling:
Use these in the downsample interval field for automatic interval adjustment:
Next steps
- Build queries using your template variables.
- Set up alerting with templated queries.
- Troubleshoot issues if variables aren’t populating.



