mirror of
https://github.com/grafana/grafana.git
synced 2025-09-21 08:53:02 +08:00

* Docs: Revise data source index * Docs: Consolidate data source administration docs * Docs: Revise panels docs related to data sources * Docs: Revise Alertmanager data source * Docs: Reorganize AWS CloudWatch data source docs * Docs: Reorganize Azure Monitor data source docs * Docs: Move azuremonitor to azure-monitor * Docs: Revise Elasticsearch docs * Docs: Move Elasticsearch index into bundle * Docs: Revise GCM docs * Docs: Revise Graphite docs * Docs: Move Graphite index into bundle * Docs: Revise InfluxDB docs * Docs: Revise Jaeger docs * Docs: Move Jaeger index into bundle * Docs: Revise Loki docs * Docs: Move Loki index into bundle * Docs: Revise MS SQL docs * Docs: Move MS SQL index into bundle * Docs: Revise Prometheus docs * Docs: Move Prometheus index into bundle * Docs: Revise Tempo docs * Docs: Move Tempo index into bundle * Docs: Revise TestData DB docs * Docs: Move TestData DB index into bundle * Docs: Revise Zipkin docs * Docs: Move Zipkin index into bundle * Docs: Move other data sources' index pages into bundles * Docs: Revise frontmatter * Fixing hugo markdown errors * Docs: Add query editor and template var sections to overview doc * Docs: Remove CTAs across data source docs * Docs: Remove CTA * Docs: Remove CTA * Docs: Fix links, images, typos, and usage consistency. * Docs: Fix typos * Docs: Fix CI issues * Update docs/sources/datasources/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Update docs/sources/datasources/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Docs: Fix query editor links * Update docs/sources/panels-visualizations/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Update docs/sources/panels-visualizations/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Docs: Rebundle child pages per writers' toolkit * Docs: Fix prettier for CI * Docs: Fix relrefs from outside data sources docs * Docs: Fix broken relrefs within datasources * Docs: Fix relrefs to data sources docs * Fixed some more refs Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
86 lines
3.4 KiB
Markdown
86 lines
3.4 KiB
Markdown
---
|
|
aliases:
|
|
- /docs/grafana/latest/datasources/influxdb/influxdb-templates/
|
|
- /docs/grafana/latest/data-sources/influxdb/influxdb-templates/
|
|
- /docs/grafana/latest/data-sources/influxdb/template-variables/
|
|
description: Guide for template variables in InfluxDB
|
|
keywords:
|
|
- grafana
|
|
- influxdb
|
|
- queries
|
|
- template
|
|
- variable
|
|
menuTitle: Template variables
|
|
title: InfluxDB template variables
|
|
weight: 300
|
|
---
|
|
|
|
# InfluxDB template variables
|
|
|
|
Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
|
|
Grafana lists these variables in dropdown 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 an introduction to templating and template variables, refer to the [Templating]({{< relref "../../../dashboards/variables" >}}) and [Add and manage variables]({{< relref "../../../dashboards/variables/add-template-variables" >}}) documentation.
|
|
|
|
## Use query variables
|
|
|
|
If you add a query template variable, you can write an InfluxDB exploration (metadata) query.
|
|
These queries can return results like measurement names, key names, or key values.
|
|
|
|
For more information, refer to [Add query variable]({{< relref "../../../dashboards/variables/add-template-variables#add-a-query-variable" >}}).
|
|
|
|
For example, to create a variable that contains all values for tag `hostname`, specify a query like this in the query variable **Query**:
|
|
|
|
```sql
|
|
SHOW TAG VALUES WITH KEY = "hostname"
|
|
```
|
|
|
|
### Chain or nest variables
|
|
|
|
You can also create nested variables, sometimes called [chained variables]({{< relref "../../../dashboards/variables/add-template-variables#chained-variables" >}}).
|
|
|
|
For example, if you had a variable called `region`, you could have the `hosts` variable show only hosts from the selected region with a query like:
|
|
|
|
```sql
|
|
SHOW TAG VALUES WITH KEY = "hostname" WHERE region = '$region'
|
|
```
|
|
|
|
You can fetch key names for a given measurement:
|
|
|
|
```sql
|
|
SHOW TAG KEYS [FROM <measurement_name>]
|
|
```
|
|
|
|
If you have a variable with key names, you can use this variable in a group-by clause.
|
|
This helps you change group-by using the variable list at the top of the dashboard.
|
|
|
|
### Use ad hoc filters
|
|
|
|
InfluxDB supports the special **Ad hoc filters** variable type.
|
|
You can use this variable type to specify any number of key/value filters, and Grafana applies them automatically to all of your InfluxDB queries.
|
|
|
|
For more information, refer to [Add ad hoc filters]({{< relref "../../../dashboards/variables/add-template-variables#add-ad-hoc-filters" >}}).
|
|
|
|
## Choose a variable syntax
|
|
|
|
The InfluxDB data source supports two variable syntaxes for use in the **Query** field:
|
|
|
|
- `$<varname>`, which is easier to read and write but does not allow you to use a variable in the middle of a word.
|
|
|
|
```sql
|
|
SELECT mean("value") FROM "logins" WHERE "hostname" =~ /^$host$/ AND $timeFilter GROUP BY time($__interval), "hostname"
|
|
```
|
|
|
|
- `${varname}`
|
|
|
|
```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
|
|
|
|
To view an example templated dashboard, refer to [InfluxDB Templated Dashboard](https://play.grafana.org/dashboard/db/influxdb-templated).
|