2022-05-26 23:06:25 +08:00
---
aliases:
2022-12-10 00:36:04 +08:00
- ../../reference/templating/
- ../../variables/advanced-variable-format-options/
- ../../variables/syntax/
2022-05-26 23:06:25 +08:00
keywords:
- grafana
- templating
- documentation
- guide
- template
- variable
2022-09-09 21:38:17 +08:00
title: Variable syntax
weight: 300
2022-05-26 23:06:25 +08:00
---
2020-05-01 07:24:59 +08:00
2022-09-09 21:38:17 +08:00
# Variable syntax
Panel titles and metric queries can refer to variables using two different syntaxes:
- `$varname`
This syntax is easy to read, but it does not allow you to use a variable in the middle of a word.
**Example:** apps.frontend.$server.requests.count
- `${var_name}` Use this syntax when you want to interpolate a variable in the middle of an expression.
2023-04-28 00:02:06 +08:00
- `${var_name:<format>}` This format gives you more control over how Grafana interpolates values. Refer to [Advanced variable format options ]({{< relref "#advanced-variable-format-options" >}} ) for more detail on all the formatting types.
2022-09-09 21:38:17 +08:00
- `[[varname]]` Do not use. Deprecated old syntax, will be removed in a future release.
Before queries are sent to your data source the query is _interpolated_ , meaning the variable is replaced with its current value. During
interpolation, the variable value might be _escaped_ in order to conform to the syntax of the query language and where it is used.
For example, a variable used in a regex expression in an InfluxDB or Prometheus query will be regex escaped. Read the data source specific
documentation topic for details on value escaping during interpolation.
2023-04-28 00:02:06 +08:00
For advanced syntax to override data source default formatting, refer to [Advanced variable format options ]({{< relref "#advanced-variable-format-options" >}} ).
2022-09-09 21:38:17 +08:00
## Advanced variable format options
2020-05-01 07:24:59 +08:00
2020-11-10 04:26:49 +08:00
The formatting of the variable interpolation depends on the data source, but there are some situations where you might want to change the default formatting.
2020-05-01 07:24:59 +08:00
For example, the default for the MySql data source is to join multiple values as comma-separated with quotes: `'server01','server02'` . In some cases, you might want to have a comma-separated string without quotes: `server01,server02` . You can make that happen with advanced variable formatting options listed below.
2022-09-09 21:38:17 +08:00
### General syntax
2020-05-01 07:24:59 +08:00
Syntax: `${var_name:option}`
Test the formatting options on the [Grafana Play site ](https://play.grafana.org/d/cJtIfcWiz/template-variable-formatting-options?orgId=1 ).
If any invalid formatting option is specified, then `glob` is the default/fallback option.
An alternative syntax (that might be deprecated in the future) is `[[var_name:option]]` .
2022-09-09 21:38:17 +08:00
### CSV
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values as a comma-separated string.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:csv}'
Interpolation result: 'test1,test2'
```
2022-09-09 21:38:17 +08:00
### Distributed - OpenTSDB
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values in custom format for OpenTSDB.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:distributed}'
Interpolation result: 'test1,servers=test2'
```
2022-09-09 21:38:17 +08:00
### Doublequote
2020-05-01 07:24:59 +08:00
Formats single- and multi-valued variables into a comma-separated string, escapes `"` in each value by `\"` and quotes each value with `"` .
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:doublequote}'
Interpolation result: '"test1","test2"'
```
2022-09-09 21:38:17 +08:00
### Glob - Graphite
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values into a glob (for Graphite queries).
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:glob}'
Interpolation result: '{test1,test2}'
```
2022-09-09 21:38:17 +08:00
### JSON
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values as a comma-separated string.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:json}'
Interpolation result: '["test1", "test2"]'
```
2022-09-09 21:38:17 +08:00
### Lucene - Elasticsearch
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values in Lucene format for Elasticsearch.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:lucene}'
Interpolation result: '("test1" OR "test2")'
```
2022-09-09 21:38:17 +08:00
### Percentencode
2020-05-01 07:24:59 +08:00
Formats single and multi valued variables for use in URL parameters.
```bash
servers = ['foo()bar BAZ', 'test2']
String to interpolate: '${servers:percentencode}'
Interpolation result: 'foo%28%29bar%20BAZ%2Ctest2'
```
2022-09-09 21:38:17 +08:00
### Pipe
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values into a pipe-separated string.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1.', 'test2']
String to interpolate: '${servers:pipe}'
Interpolation result: 'test1.|test2'
```
2022-09-09 21:38:17 +08:00
### Raw
2020-05-06 05:37:34 +08:00
Turns off data source-specific formatting, such as single quotes in an SQL query.
```bash
2020-12-21 23:54:01 +08:00
servers = ['test.1', 'test2']
2020-05-06 05:37:34 +08:00
String to interpolate: '${var_name:raw}'
2020-12-21 23:54:01 +08:00
Interpolation result: 'test.1,test2'
2020-05-06 05:37:34 +08:00
```
2022-09-09 21:38:17 +08:00
### Regex
2020-05-01 07:24:59 +08:00
2020-05-07 00:43:52 +08:00
Formats variables with multiple values into a regex string.
2020-05-01 07:24:59 +08:00
```bash
servers = ['test1.', 'test2']
String to interpolate: '${servers:regex}'
Interpolation result: '(test1\.|test2)'
```
2022-09-09 21:38:17 +08:00
### Singlequote
2020-05-01 07:24:59 +08:00
Formats single- and multi-valued variables into a comma-separated string, escapes `'` in each value by `\'` and quotes each value with `'` .
```bash
servers = ['test1', 'test2']
String to interpolate: '${servers:singlequote}'
Interpolation result: "'test1','test2'"
```
2022-09-09 21:38:17 +08:00
### Sqlstring
2020-05-01 07:24:59 +08:00
Formats single- and multi-valued variables into a comma-separated string, escapes `'` in each value by `''` and quotes each value with `'` .
```bash
servers = ["test'1", "test2"]
String to interpolate: '${servers:sqlstring}'
Interpolation result: "'test''1','test2'"
```
2020-09-14 14:05:51 +08:00
2022-09-09 21:38:17 +08:00
### Text
2020-09-14 14:05:51 +08:00
Formats single- and multi-valued variables into their text representation. For a single variable it will just return the text representation. For multi-valued variables it will return the text representation combined with `+` .
```bash
servers = ["test1", "test2"]
String to interpolate: '${servers:text}'
Interpolation result: "test1 + test2"
2020-12-21 23:54:01 +08:00
```
2021-02-05 14:16:06 +08:00
2022-09-09 21:38:17 +08:00
### Query parameters
2021-02-05 14:16:06 +08:00
Formats single- and multi-valued variables into their query parameter representation. Example: `var-foo=value1&var-foo=value2`
```bash
servers = ["test1", "test2"]
String to interpolate: '${servers:queryparam}'
Interpolation result: "var-servers=test1& var-servers=test2"
```