grafana/docs/sources/reference/templating.md

176 lines
10 KiB
Markdown
Raw Normal View History

Squashed commit of the following: commit 4113ed00782590187d19dd2f8cbae683f164142c Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Fri Oct 21 10:47:35 2016 +0200 docs(): minor fix for docs index page commit 45478d120571519462fac905aadeab5954696690 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Thu Oct 20 14:28:21 2016 +0200 updated commit c9c1c1d5a462f85e8001a45218f24102583d7aee Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Thu Oct 20 14:27:34 2016 +0200 docs(): updated image refs commit 5fa0f27963968d77bbbbb7edd973847ea72135e3 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Wed Oct 19 14:33:04 2016 +0200 docs is almost done commit 6b988c90cb08563e2c27212338c2947e69f6fcc3 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 20:45:28 2016 +0200 almost done commit 17757c21ed813892ddb1f289f2e52613caf7bcef Author: Torkel Ödegaard <torkel@grafana.org> Date: Tue Oct 18 16:45:48 2016 +0200 making progress on docs commit f06c815991dd81b8893912a6da04a17b80b63fda Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 14:30:55 2016 +0200 progress on new docs commit 5197237426252623de2d8b9cc22ddbbdbb240763 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 13:25:26 2016 +0200 making progress commit 33e2b6b617f16f65878141cf11e54b2817a2ea96 Merge: de385e5 ecb4a99 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 12:17:58 2016 +0200 Merge branch 'new-docs' of github.com:grafana/grafana into new-docs commit ecb4a99dd54aa39f17d9bf4d7e132a389812f8fd Author: Torkel Ödegaard <torkel@grafana.org> Date: Tue Oct 18 10:39:59 2016 +0200 keep urls for now commit de385e56d86b071cf2150ccf9f1aa06ec5187277 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 17:56:18 2016 +0200 more progress commit 8469ebc239ab2316cbbc01862e5026737f272f00 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 17:08:06 2016 +0200 progress on new docs commit e755f656b12534cfcb23bad11f1fa696e43f7428 Merge: 4644a35 35cce3b Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 12:21:27 2016 +0200 Merge branch 'new-docs' of github.com:grafana/grafana into new-docs commit 4644a35102555204787e91041b50dcbfe0f7a213 Merge: 25c4bef 977cdd5 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 12:21:11 2016 +0200 Merge branch 'master' of github.com:grafana/grafana into new-docs commit 35cce3bef802fac020a53693b02c112a91a096fc Author: Torkel Ödegaard <torkel@grafana.org> Date: Mon Oct 17 09:31:17 2016 +0200 progress on #6170 commit 25c4bef629de7d77218ba95cc3bd25fb50bb19d6 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Sun Oct 16 15:05:30 2016 +0200 fix for osx commit 6c6b52f975630be87da23dd51beafaecb820cec2 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sun Oct 16 12:56:39 2016 +0200 progress on new docs commit 6a09633981ab26191d5542dbe3a788846ac6b73d Author: Torkel Ödegaard <torkel@grafana.org> Date: Sat Oct 15 11:39:49 2016 +0200 progress on new docs commit 7d533fbc23e41b86fd1ba9c955dd17ae1ad977e5 Author: Torkel Ödegaard <torkel@grafana.org> Date: Mon Oct 10 09:25:12 2016 +0200 making progress on new docs site commit 50392ce98edfa201977716e92e4a86f9d48ae386 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sun Oct 9 20:02:28 2016 +0200 feat(new docs): progress on new docs site commit 81a3d3d0952d3a5d790412e90d01ac5a247e0686 Author: Torkel Ödegaard <torkel@grafana.org> Date: Fri Oct 7 07:52:11 2016 +0200 new docs site experiment commit 704adc98664665b3624087ab38ce29fc9da005f1 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sat Oct 1 20:46:53 2016 +0200 new docs poc
2016-10-21 04:01:34 -05:00
+++
title = "Templating"
keywords = ["grafana", "templating", "documentation", "guide"]
type = "docs"
[menu.docs]
name = "Templating"
parent = "dashboard_features"
weight = 1
+++
2015-03-10 02:55:42 -05:00
Squashed commit of the following: commit 4113ed00782590187d19dd2f8cbae683f164142c Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Fri Oct 21 10:47:35 2016 +0200 docs(): minor fix for docs index page commit 45478d120571519462fac905aadeab5954696690 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Thu Oct 20 14:28:21 2016 +0200 updated commit c9c1c1d5a462f85e8001a45218f24102583d7aee Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Thu Oct 20 14:27:34 2016 +0200 docs(): updated image refs commit 5fa0f27963968d77bbbbb7edd973847ea72135e3 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Wed Oct 19 14:33:04 2016 +0200 docs is almost done commit 6b988c90cb08563e2c27212338c2947e69f6fcc3 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 20:45:28 2016 +0200 almost done commit 17757c21ed813892ddb1f289f2e52613caf7bcef Author: Torkel Ödegaard <torkel@grafana.org> Date: Tue Oct 18 16:45:48 2016 +0200 making progress on docs commit f06c815991dd81b8893912a6da04a17b80b63fda Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 14:30:55 2016 +0200 progress on new docs commit 5197237426252623de2d8b9cc22ddbbdbb240763 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 13:25:26 2016 +0200 making progress commit 33e2b6b617f16f65878141cf11e54b2817a2ea96 Merge: de385e5 ecb4a99 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Tue Oct 18 12:17:58 2016 +0200 Merge branch 'new-docs' of github.com:grafana/grafana into new-docs commit ecb4a99dd54aa39f17d9bf4d7e132a389812f8fd Author: Torkel Ödegaard <torkel@grafana.org> Date: Tue Oct 18 10:39:59 2016 +0200 keep urls for now commit de385e56d86b071cf2150ccf9f1aa06ec5187277 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 17:56:18 2016 +0200 more progress commit 8469ebc239ab2316cbbc01862e5026737f272f00 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 17:08:06 2016 +0200 progress on new docs commit e755f656b12534cfcb23bad11f1fa696e43f7428 Merge: 4644a35 35cce3b Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 12:21:27 2016 +0200 Merge branch 'new-docs' of github.com:grafana/grafana into new-docs commit 4644a35102555204787e91041b50dcbfe0f7a213 Merge: 25c4bef 977cdd5 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Mon Oct 17 12:21:11 2016 +0200 Merge branch 'master' of github.com:grafana/grafana into new-docs commit 35cce3bef802fac020a53693b02c112a91a096fc Author: Torkel Ödegaard <torkel@grafana.org> Date: Mon Oct 17 09:31:17 2016 +0200 progress on #6170 commit 25c4bef629de7d77218ba95cc3bd25fb50bb19d6 Author: Torkel Ödegaard <torkel.odegaard@gmail.com> Date: Sun Oct 16 15:05:30 2016 +0200 fix for osx commit 6c6b52f975630be87da23dd51beafaecb820cec2 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sun Oct 16 12:56:39 2016 +0200 progress on new docs commit 6a09633981ab26191d5542dbe3a788846ac6b73d Author: Torkel Ödegaard <torkel@grafana.org> Date: Sat Oct 15 11:39:49 2016 +0200 progress on new docs commit 7d533fbc23e41b86fd1ba9c955dd17ae1ad977e5 Author: Torkel Ödegaard <torkel@grafana.org> Date: Mon Oct 10 09:25:12 2016 +0200 making progress on new docs site commit 50392ce98edfa201977716e92e4a86f9d48ae386 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sun Oct 9 20:02:28 2016 +0200 feat(new docs): progress on new docs site commit 81a3d3d0952d3a5d790412e90d01ac5a247e0686 Author: Torkel Ödegaard <torkel@grafana.org> Date: Fri Oct 7 07:52:11 2016 +0200 new docs site experiment commit 704adc98664665b3624087ab38ce29fc9da005f1 Author: Torkel Ödegaard <torkel@grafana.org> Date: Sat Oct 1 20:46:53 2016 +0200 new docs poc
2016-10-21 04:01:34 -05:00
# Templating
2015-03-10 02:55:42 -05:00
2017-05-03 08:50:38 -05:00
Instead of hard-coding things like server, application and sensor name in you metric queries you can use variables in their place.
Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns makes it easy to change the data
being displayed in your dashboard.
2017-05-02 09:22:31 -05:00
<img class="no-shadow" src="/img/docs/v4/templated_dash.png">
Templating allows for more interactive and dynamic dashboards. Instead of hard-coding things like server, application
and sensor name in you metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of
2017-05-03 09:34:51 -05:00
the dashboard. These dropdowns make it easy to change the data being displayed in your dashboard.
2015-07-20 19:05:56 -05:00
2017-05-02 09:22:31 -05:00
## What is a variable?
2017-05-02 09:22:31 -05:00
A variable is a placeholder for a value. You can use variables in metric queries and in panel titles. So when you change
the value, using the dropdown at the top of the dashboard, your panel's metric queries will change to reflect the new value.
2017-05-02 09:22:31 -05:00
### Interpolation
2015-07-20 19:05:56 -05:00
2017-05-02 09:22:31 -05:00
Panel titles and metric queries can refer to variables using two different syntaxes:
- `$<varname>` Example: apps.frontend.$server.requests.count
- `[[varname]]` Example: apps.frontend.[[server]].requests.count
Why two ways? The first syntax is easier to read and write but does not allow you to use a variable in the middle of word. Use
the second syntax in expressions like `my.server[[serverNumber]].count`.
2017-05-02 09:22:31 -05:00
Before queries are sent to your data source the query is **interpolated**, meaning the variable is replaced with its current value. During
2017-05-03 07:09:04 -05:00
interpolation the variable value might be **escaped** in order to conform to the syntax of the query language and where it is used.
2017-05-03 09:34:51 -05:00
For example, a variable used in a regex expression in an InfluxDB or Prometheus query will be regex escaped. Read the data source specific
2017-05-03 07:09:04 -05:00
documentation article for details on value escaping during interpolation.
2017-05-03 07:09:04 -05:00
### Variable options
A variable is presented as a dropdown select box at the top of the dashboards. It has a current value and a set of **options**. The **options**
2017-05-03 09:34:51 -05:00
is the set of values you can choose from.
2017-05-03 07:09:04 -05:00
## Adding a variable
2015-07-20 19:05:56 -05:00
2017-05-02 09:22:31 -05:00
<img class="no-shadow" src="/img/docs/v4/templating_var_list.png">
2017-05-03 07:09:04 -05:00
You add variables via Dashboard cogs menu > Templating. This opens up a list of variables and a `New` button to create a new variable.
### Basic variable options
Option | Description
------- | --------
*Name* | The name of the variable, this is the name you use when you refer to your variable in your metric queries. Must be unique and contain no white-spaces.
*Label* | The name of the dropdown for this variable.
*Hide* | Options to hide the dropdown select box.
*Type* | Defines the variable type.
### Variable types
Type | Description
------- | --------
2017-05-03 09:34:51 -05:00
*Query* | This variable type allows you to write a data source query that usually returns a list of metric names, tag values or keys. For example, a query that returns a list of server names, sensor ids or data centers.
*Interval* | This variable can represent time spans. Instead of hard-coding a group by time or date histogram interval, use a variable of this type.
*Datasource* | This type allows you to quickly change the data source for an entire Dashboard. Useful if you have multiple instances of a data source in for example different environments.
*Custom* | Define the variable options manually using a comma separated list.
*Constant* | Define a hidden constant. Useful for metric path prefixes for dashboards you want to share. During dashboard export, constant variables will be made into an import option.
2017-05-03 07:09:04 -05:00
*Ad hoc filters* | Very special kind of variable that only works with some data sources, InfluxDB & Elasticsearch currently. It allows you to add key/value filters that will automatically be added to all metric queries that use the specified data source.
2017-05-03 07:09:04 -05:00
### Query options
2017-05-03 09:34:51 -05:00
This variable type is the most powerful and complex as it can dynamically fetch its options using a data source query.
2017-05-03 07:09:04 -05:00
Option | Description
------- | --------
*Data source* | The data source target for the query.
2017-05-03 09:34:51 -05:00
*Refresh* | Controls when to update the variable option list (values in the dropdown). **On Dashboard Load** will slow down dashboard load as the variable query needs to be completed before dashboard can be initialized. Set this only to **On Time Range Change** if your variable options query contains a time range filter or is dependent on dashboard time range.
2017-05-03 07:09:04 -05:00
*Query* | The data source specific query expression.
*Regex* | Regex to filter or capture specific parts of the names return by your data source query. Optional.
*Sort* | Define sort order for options in dropdown. **Disabled** means that the order of options returned by your data source query will be used.
2017-05-03 07:09:04 -05:00
### Query expressions
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
The query expressions are different for each data source.
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
- [Graphite templating queries]({{< relref "features/datasources/graphite.md#templating" >}})
- [Elasticsearch templating queries]({{< relref "features/datasources/elasticsearch.md#templating" >}})
- [InfluxDB templating queries]({{< relref "features/datasources/influxdb.md#templating" >}})
- [Prometheus templating queries]({{< relref "features/datasources/prometheus.md#templating" >}})
- [OpenTSDB templating queries]({{< relref "features/datasources/prometheus.md#templating" >}})
2017-05-03 07:09:04 -05:00
One thing to note is that query expressions can contain references to other variables and in effect create depend & nested
variables. Grafana will detect this and automatically refresh a variable when one of it's containing variables change.
2017-05-03 07:09:04 -05:00
## Selection Options
2017-05-03 07:09:04 -05:00
Option | Description
------- | --------
*Mulit-value* | If enabled, the variable will support the selection of multiple options at the same time.
*Include All option* | Add a special `All` option whose value includes all options.
*Custom all value* | By default the `All` value will include all options in combined expression. This can become very long and can have performance problems. Many times it can be better to specify a custom all value, like a wildcard regex. To make it possible to have custom regex, globs or lucene syntax in the **Custom all value** option it is never escaped so you will have to think avbout what is a valid value for your data source.
2017-05-02 09:22:31 -05:00
2017-05-03 07:09:04 -05:00
### Formating multiple values
2017-05-02 09:22:31 -05:00
2017-05-03 07:09:04 -05:00
Interpolating a variable with multiple values selected is tricky as it is not straight forward how to format the multiple values to into a string that
is valid in the given context where the variable is used. Grafana tries to solve this by allowing each data source plugin to
inform the templating interpolation engine what format to use for multiple values.
2017-05-02 09:22:31 -05:00
2017-05-03 09:34:51 -05:00
**Graphite**, for example, uses glob expressions. A variable with multiple values would, in this case, be interpolated as `{host1,host2,host3}` if
2017-05-03 07:09:04 -05:00
the current variable value was *host1*, *host2* and *host3*.
2017-05-03 07:09:04 -05:00
**InfluxDB and Prometheus** uses regex expressions, so the same variable
2017-05-03 09:34:51 -05:00
would be interpolated as `(host1|host2|host3)`. Every value would also be regex escaped if not, a value with a regex control character would
2017-05-03 07:09:04 -05:00
break the regex expression.
2017-05-03 09:34:51 -05:00
**Elasticsearch** uses lucene query syntax, so the same variable would, in this case, be formatted as `("host1" OR "host2" OR "host3")`. In this case every value
2017-05-03 07:09:04 -05:00
needs to be escaped so that the value can contain lucene control words and quotation marks.
2017-05-03 07:09:04 -05:00
#### Formating troubles
2017-05-03 09:34:51 -05:00
Automatic escaping & formatting can cause problems and it can be tricky to grasp the logic is behind it.
2017-05-03 07:09:04 -05:00
Especially for InfluxDB and Prometheus where the use of regex syntax requires that the variable is used in regex operator context.
2017-05-03 09:34:51 -05:00
If you do not want Grafana to do this automatic regex escaping and formatting your only option is to disable the *Multi-value* or *Include All option*
2017-05-03 07:09:04 -05:00
options.
2017-05-03 07:09:04 -05:00
### Value groups/tags
2017-05-03 07:09:04 -05:00
If you have a lot of options in the dropdown for a multi-value variable. You can use this feature to group the values into selectable tags.
2017-05-03 07:09:04 -05:00
Option | Description
------- | --------
*Tags query* | Data source query that should return a list of tags
*Tag values query* | Data source query that should return a list of values for a specified tag key. Use `$tag` in the query to refer the currently selected tag.
2017-05-03 07:09:04 -05:00
![](/img/docs/v4/variable_dropdown_tags.png)
2017-05-03 07:09:04 -05:00
### Interval variables
2015-07-20 19:05:56 -05:00
2017-05-03 09:34:51 -05:00
Use the `Interval` type to create a variable that represents a time span (eg. `1m`,`1h`, `1d`). There is also a special `auto` option that will change depending on the current time range. You can specify how many times the current time range should be divided to calculate the current `auto` timespan.
2017-05-03 09:34:51 -05:00
This variable type is useful as a parameter to group by time (for InfluxDB), Date histogram interval (for Elasticsearch) or as a *summarize* function parameter (for Graphite).
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
## Repeating Panels
2017-05-03 07:09:04 -05:00
Template variables can be very useful to dynamically change your queries across a whole dashboard. If you want
Grafana to dynamically create new panels or rows based on what values you have selected you can use the *Repeat* feature.
2017-05-03 07:09:04 -05:00
If you have a variable with `Multi-value` or `Include all value` options enabled you can choose one panel or one row and have Grafana repeat that row
for every selected value. You find this option under the General tab in panel edit mode. You select the variable to repeat by, and a `min span`.
The `min span` controls how small Grafana will make the panels (if you have many values selected). Grafana will automatically adjust the width of
2017-05-03 09:34:51 -05:00
each repeated panel so that the whole row is filled. Currently, you cannot mix other panels on a row with a repeated panel.
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
Only make changes to the first panel (the original template). To have the changes take effect on all panels you need to trigger a dynamic dashboard re-build.
You can do this by either changing the variable value (that is the basis for the repeat) or reload the dashboard.
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
## Repeating Rows
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
This option requires you to open the row options view. Hover over the row left side to trigger the row menu, in this menu click `Row Options`. This
opens the row options view. Here you find a *Repeat* dropdown where you can select the variable to repeat by.
2015-07-20 19:05:56 -05:00
2017-05-03 07:09:04 -05:00
### URL state
2015-03-10 02:55:42 -05:00
2017-05-03 07:09:04 -05:00
Variable values are always synced to the URL using the syntax `var-<varname>=value`.
2015-07-20 19:05:56 -05:00
2017-05-04 03:19:00 -05:00
### Examples
- [Graphite Templated Dashboard](http://play.grafana.org/dashboard/db/graphite-templated-nested)
- [Elasticsearch Templated Dashboard](http://play.grafana.org/dashboard/db/elasticsearch-templated)
- [InfluxDB Templated Dashboard](http://play.grafana.org/dashboard/db/influxdb-templated-queries)