2022-11-01 23:22:06 +08:00
---
aliases:
2022-12-10 00:36:04 +08:00
- ../../data-sources/influxdb/influxdb-templates/
- ../../data-sources/influxdb/template-variables/
- influxdb-templates/
2022-11-01 23:22:06 +08:00
description: Guide for template variables in InfluxDB
keywords:
- grafana
- influxdb
- queries
- template
- variable
Explicitly set all front matter labels in the source files (#71548)
* Set every page to have defaults of 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration pages to have of 'Cloud', 'Enterprise', and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/enterprise-licensing pages to have 'Enterprise' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/organization-management pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/provisioning pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/recorded-queries pages to have labels cloud,enterprise
* Set administration/roles-and-permissions/access-control pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/stats-and-license pages to have labels cloud,enterprise
* Set alerting pages to have labels cloud,enterprise,oss
* Set breaking-changes pages to have labels cloud,enterprise,oss
* Set dashboards pages to have labels cloud,enterprise,oss
* Set datasources pages to have labels cloud,enterprise,oss
* Set explore pages to have labels cloud,enterprise,oss
* Set fundamentals pages to have labels cloud,enterprise,oss
* Set introduction/grafana-cloud pages to have labels cloud
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Fix introduction pages products
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set panels-visualizations pages to have labels cloud,enterprise,oss
* Set release-notes pages to have labels cloud,enterprise,oss
* Set search pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/audit-grafana pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-authentication pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/configure-authentication/enhanced-ldap pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-authentication/saml pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-database-encryption/encrypt-secrets-using-hashicorp-key-vault pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-request-security pages to have labels cloud,enterprise,oss
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-team-sync pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/export-logs pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set troubleshooting pages to have labels cloud,enterprise,oss
* Set whatsnew pages to have labels cloud,enterprise,oss
* Apply updated labels from review
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
---------
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
2023-07-18 16:10:12 +08:00
labels:
products:
- cloud
- enterprise
- oss
2022-11-01 23:22:06 +08:00
menuTitle: Template variables
title: InfluxDB template variables
2024-11-22 03:37:03 +08:00
weight: 600
2024-05-28 22:17:49 +08:00
refs:
add-template-variables-chained-variables:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#chained-variables
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#chained-variables
variables:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/
add-template-variables:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/
add-template-variables-add-ad-hoc-filters:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#add-ad-hoc-filters
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#add-ad-hoc-filters
2024-11-22 03:37:03 +08:00
add-template-variables-add-a-query-variable:
2024-05-28 22:17:49 +08:00
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#add-a-query-variable
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#add-a-query-variable
2024-11-22 03:37:03 +08:00
variable-best-practices:
- pattern: /docs/grafana/
2025-07-26 03:14:39 +08:00
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/#variable-best-practices
2024-11-22 03:37:03 +08:00
- pattern: /docs/grafana-cloud/
2025-07-26 03:14:39 +08:00
destination: /docs/grafana-cloud/visualizations/dashboards/variables/add-template-variables/#variable-best-practices
2022-11-01 23:22:06 +08:00
---
# InfluxDB template variables
2024-11-22 03:37:03 +08:00
Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables. Grafana displays these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard. Grafana refers to such variables as template variables.
For additional information using variables and templates, refer to the following documentation:
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
- [Variables ](ref:variables )
- [Templates ](ref:variables )
- [Add and manage variables ](ref:add-template-variables )
- [Variable best practices ](ref:variable-best-practices )
2022-11-01 23:22:06 +08:00
## Use query variables
2024-11-22 03:37:03 +08:00
By adding a query template variable, you can write an InfluxDB metadata exploration query. These queries return results such as measurement names, key names, and key values.
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
For more information, refer to [Add a query variable ](ref:add-template-variables-add-a-query-variable ).
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
To create a variable containing all values for the `hostname` tag, use the following query format in the **Query** variable:
2022-11-01 23:22:06 +08:00
```sql
SHOW TAG VALUES WITH KEY = "hostname"
```
2024-11-22 03:37:03 +08:00
## Chain or nest variables
2022-11-01 23:22:06 +08:00
2024-05-28 22:17:49 +08:00
You can also create nested variables, sometimes called [chained variables ](ref:add-template-variables-chained-variables ).
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
For example, if you have a variable named `region` , you can configure the `hosts` variable to display only hosts from the selected region using the following query:
2022-11-01 23:22:06 +08:00
```sql
SHOW TAG VALUES WITH KEY = "hostname" WHERE region = '$region'
```
2024-11-22 03:37:03 +08:00
You can also fetch key names for a given measurement:
2022-11-01 23:22:06 +08:00
```sql
SHOW TAG KEYS [FROM < measurement_name > ]
```
2024-11-22 03:37:03 +08:00
If you have a variable containing key names, you can use it in a **GROUP BY** clause. This allows you to adjust the grouping by selecting from the variable list at the top of the dashboard
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
## Use ad hoc filters
2022-11-01 23:22:06 +08:00
2024-11-22 03:37:03 +08:00
InfluxDB supports the **Ad hoc filters** variable type. This variable type allows you to define multiple key/value filters, which Grafana then automatically applies to all your InfluxDB queries.
2022-11-01 23:22:06 +08:00
2024-05-28 22:17:49 +08:00
For more information, refer to [Add ad hoc filters ](ref:add-template-variables-add-ad-hoc-filters ).
2022-11-01 23:22:06 +08:00
## Choose a variable syntax
The InfluxDB data source supports two variable syntaxes for use in the **Query** field:
2024-11-22 03:37:03 +08:00
- `$<varname>` - This syntax is easy to read and write but does not allow you to use a variable in the middle of a word or expression.
2022-11-01 23:22:06 +08:00
```sql
SELECT mean("value") FROM "logins" WHERE "hostname" =~ /^$host$/ AND $timeFilter GROUP BY time($__interval), "hostname"
```
2024-11-22 03:37:03 +08:00
- `${varname}` - Use this syntax when you want to interpolate a variable in the middle of an expression.
2022-11-01 23:22:06 +08:00
```sql
SELECT mean("value") FROM "logins" WHERE "hostname" =~ /^[[host]]$/ AND $timeFilter GROUP BY time($__interval), "hostname"
```
When you enable the **Multi-value** or **Include all value** options, Grafana converts the labels from plain text to a regex-compatible string, so you must use `=~` instead of `=` .
### Templated dashboard example
2024-11-22 03:37:03 +08:00
To view an example of a templated dashboard, refer to this [InfluxDB example dashboard ](https://play.grafana.org/d/f62a0410-5abb-4dd8-9dfc-caddfc3e2ffd/eccb2445-b0a2-5e83-8e0f-6d5ea53ad575 ).