docs/sources/visualizations/dashboards/variables/add-template-variables/index.md
The following table lists the types of variables shipped with Grafana.
<!-- vale Grafana.Spelling = NO --> <!-- prettier-ignore-start -->| Variable type | Description |
|---|---|
| Query | Query-generated list of values such as metric names, server names, sensor IDs, data centers, and so on. Add a query variable. |
| Custom | Define the variable options manually using a comma-separated list. Add a custom variable. |
| Text box | Display a free text input field with an optional default value. Add a text box variable. |
| Constant | Define a hidden constant. Add a constant variable. |
| Data source | Quickly change the data source for an entire dashboard. Add a data source variable. |
| Interval | Interval variables represent time spans. Add an interval variable. |
| Filters | Key/value filters that are automatically added to all metric queries for a data source (Prometheus, Loki, InfluxDB, and Elasticsearch only). Add filters. |
| Switch | Display a switch that allows you to toggle between two configurable values for enabled and disabled states. Add a switch variable. |
| Global variables | Built-in variables that can be used in expressions in the query editor. Refer to Global variables. |
| Chained variables | Variable queries can contain other variables. Refer to Chained variables. |
You must enter general options for any type of variable that you create. To create a variable, follow these steps:
{{< docs/list >}}
{{< shared id="add-variable" >}}
Enter a Name for the variable.
(Optional) In the Label field, enter the display name for the variable drop-down list.
If you don't enter a display name, then the drop-down list label is the variable name.
(Optional) In the Description field, enter a description of the variable. The description appears as an info icon tooltip next to the variable name on the dashboard.
Descriptions support links. You can use Markdown-style links ([link text](https://example.com)) or paste bare URLs (https://example.com). Only http and https URLs are rendered as clickable links — other protocols are displayed as plain text.
Choose a Display option:
{{< /shared >}}
{{< /docs/list >}}
<!-- vale Grafana.Spelling = YES -->{{< admonition type="tip" >}} To add variables without leaving the dashboard, click the Add new element icon in the dashboard toolbar, and select Variable. For more information, refer to the Dashboard controls documentation. {{< /admonition >}}
.+.Query variables enable you to write a data source query that can return a list of metric names, tag values, or keys. For example, a query variable might return a list of server names, sensor IDs, or data centers. The variable values change as they dynamically fetch options with a data source query.
Query variables are generally only supported for strings. If your query returns numbers or any other data type, you might need to convert them to strings to use them as variables. For the Azure data source, for example, you can use the tostring function for this purpose.
Query expressions can contain references to other variables and in effect create linked variables. Grafana detects this and automatically refreshes a variable when one of its linked variables change.
{{< admonition type="note" >}} Query expressions are different for each data source. For more information, refer to the documentation for your data source. {{< /admonition >}}
Under the Query options section of the page, select a target data source in the Data source drop-down list. You can also click Open advanced data source picker to see more options, including adding a data source (Admins only). For more information about data sources, refer to Add a data source.
In the Query type drop-down list, select an option and fill in the query fields accordingly.
__text and __value in the result. Other data sources may look for text and value or use a different approach. Always remember to double-check the documentation for the data source.(Optional) In the Regex field, type a regular expression to filter or capture specific parts of the names returned by your data source query. To see examples, refer to Filter variables with a regular expression.
Under Apply regex to, select Variable value or Display text to choose where the regex pattern is applied. The default is Variable value.
In the Sort drop-down list, select the sort order for values to be displayed in the dropdown list. The default option, Disabled, means that the order of options returned by your data source query is used.
Under Refresh, select when the variable should update options:
(Optional) In the Static options section of the page, toggle on the Use static options switch to add custom options in addition to the query results:
(Optional) Configure the settings in the Selection Options section:
In the Preview of values section, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Use a custom variable for a value that does not change, such as a number or a string.
For example, if you have server names or region names that never change, then you might want to create them as custom variables rather than query variables. Because they do not change, you might use them in chained variables rather than other query variables. That would reduce the number of queries Grafana must send when chained variables are updated.
Under the Custom options section of the page, select one of the following:
key1 : value1,key2 : value2.(Optional) Configure the settings in the Selection Options section:
In the Preview of values section, Grafana displays a list of the current variable values. If you've entered a JSON array, the preview is a table that includes all the value properties. Review them to ensure they match what you expect.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Text box variables display a free text input field with an optional default value. This is the most flexible variable, because you can enter any value. Use this type of variable if you have metrics with high cardinality or if you want to update multiple panels in a dashboard at the same time.
For more information about cardinality, refer to What are cardinality spikes and why do they matter?
(Optional) Under the Text options section of the page, in the Default value field, enter the default value for the variable.
If you do not enter anything in this field, then Grafana displays an empty text box for users to type text into.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Constant variables enable you to define a hidden constant. This is useful for metric path prefixes for dashboards you want to share. When you export a dashboard, constant variables are converted to import options.
Constant variables are not flexible. Each constant variable only holds one value, and it cannot be updated unless you update the variable settings.
Constant variables are useful when you have complex values that you need to include in queries but don't want to retype in every query. For example, if you had a server path called i-0b6a61efe2ab843gg, then you could replace it with a variable called $path_gg.
Under the Constant options section of the page, in the Value field, enter the variable value.
You can enter letters, numbers, and symbols. You can even use wildcards if you use raw format.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Data source variables enable you to quickly change the data source for an entire dashboard. They are useful if you have multiple instances of a data source, perhaps in different environments.
Under the Data source options section of the page, in the Type drop-down list, select the target data source for the variable.
(Optional) In Instance name filter, enter a regular expression filter for which data source instances to choose from in the variable value drop-down list.
Leave this field empty to display all instances.
(Optional) Configure the settings in the Selection Options section:
In the Preview of values section, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Use an interval variable to represents time spans such as 1m,1h, 1d. You can think of them as a dashboard-wide "group by time" command. Interval variables change how the data is grouped in the visualization. You can also use the Auto Option to return a set number of data points per time span.
You can use an interval variable as a parameter to group by time (for InfluxDB), date histogram interval (for Elasticsearch), or as a summarize function parameter (for Graphite).
Under the Interval options section, in the Values field, enter the time range intervals that you want to appear in the variable drop-down list.
The following time units are supported: s (seconds), m (minutes), h (hours), d (days), w (weeks), M (months), and y (years). You can also accept or edit the default values: 1m,10m,30m,1h,6h,12h,1d,7d,14d,30d.
(Optional) Select on the Auto option checkbox if you want to add the auto option to the list.
This option allows you to specify how many times the current time range should be divided to calculate the current auto time span. If you turn it on, then two more options appear:
auto interval groups the data into 30 one-minute increments. The default value is 30 steps.In the Preview of values section, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
The following example shows a template variable myinterval in a Graphite function:
summarize($myinterval, sum, false)
The following example shows a more complex Graphite example, from the Graphite Template Nested Requests panel:
groupByNode(summarize(movingAverage(apps.$app.$server.counters.requests.count, 5), '$interval', 'sum', false), 2, 'sum')
{{< admonition type="note" >}}
In Grafana v13, we released the Filter and Group by feature in public preview.
It renames the Filters variable (formerly ad hoc filter) and extends it by adding grouping for Prometheus and Loki data sources.
However, in the dashboard schema, it's still referred to as "kind": "AdhocVariable".
To use this feature, enable the dashboardUnifiedDrilldownControls feature toggle in your Grafana configuration file.
For more information on the Filter and Group by feature, refer to the Dashboard controls documentation. {{< /admonition >}}
Filters are one of the most complex and flexible variable options available. Instead of creating a variable for each dimension by which you want to filter, filters automatically create variables (key/value pairs) for all the dimensions returned by your data source query. This allows you to apply filters dashboard-wide.
Filters let you add label/value filters that are automatically added to all metric queries that use the specified data source. Unlike other variables, you don't use filters in queries. Instead, you use filters to write filters for existing queries.
The following data sources support filters:
To create a filter, follow these steps:
Under the Filter options section of the page, select a target data source in the Data source drop-down list.
You can also click Open advanced data source picker to see more options, including adding a data source (Admins only). For more information about data sources, refer to Add a data source.
(Optional) To provide the filter dimensions as comma-separated values (CSV), toggle the Use static key dimensions switch on, and then enter the values in the space provided.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Now you can filter data on the dashboard.
{{< admonition type="tip" >}} You can use data links to link back to the dashboard you are currently on. This enables "panel-to-panel filtering," where clicking a data point in one panel updates the dashboard variables and filters the rest of the dashboard.
To preserve the context of the current dashboard:
Filters on the current dashboard are automatically preserved.
Learn more in:
In cases where a data source doesn't support the use of filters, you can use the Dashboard data source to reference that data, and then filter it in a new panel. This allows you to bypass the limitations of the data source in the source panel.
{{< figure src="/media/docs/grafana/panels-visualizations/screenshot-adhoc-filter-dashboard-ds-v12.2.png" max-width="750px" alt="The query section of a panel with the Dashboard data source configured" >}}
To use filters on data from an unsupported data source, follow these steps:
Navigate to the dashboard with the panel with the data you want to filter.
Click Edit in top-right corner of the dashboard.
Click the Add new element icon and click or drag a panel onto the dashboard.
Click Configure visualization.
In the Queries tab of the edit panel view, enter Dashboard in the Data source field and select -- Dashboard --.
In the query configuration section, make the following selections:
{{< admonition type="note">}} If you're referencing multiple panels in a dashboard with the Dashboard data source, you can only use one of those source panels at a time for filtering. {{< /admonition >}}
Configure any other needed options for the panel.
Click Save in the top-right corner.
Enter an optional description of your changes and click Save.
Click Back to dashboard and then Exit edit.
Now you can filter the data from the source panel by way of the Dashboard data source. Add as many panels as you need.
In table and bar chart visualizations, you can apply filters directly from the visualization. To quickly apply filter variables, follow these steps:
To display the filter icons, hover your cursor over the table cell with the value for which you want to filter. In this example, the cell value is ConfigMap Updated, which is in the alertname column:
{{< figure src="/media/docs/grafana/panels-visualizations/screenshot-adhoc-filter-icon-v12.2.png" max-width="550px" alt="Table and bar chart with a filter icon displayed on a table cell" >}}
In bar chart visualizations, hover and click the bar to display the filter button:
{{< figure src="/media/docs/grafana/panels-visualizations/screenshot-adhoc-filter-icon-bar-v12.2.png" max-width="300px" alt="The filter button in a bar chart tooltip">}}
Click the add filter icon.
The variable pair alertname = ConfigMap Updated is added to the filter and all panels using the same data source that include that variable value are filtered by that value:
{{< figure src="/media/docs/grafana/panels-visualizations/screenshot-adhoc-filter-applied-v12.2.png" max-width="550px" alt="Table and bar chart, filtered" >}}
If one of the panels in the dashboard using that data source doesn't include that variable value, the panel won't return any data. In this example, the variable pair _name_ = ALERTS has been added to the filter so the bar chart doesn't return any results:
{{< figure src="/media/docs/grafana/panels-visualizations/screenshot-adhoc-filter-no-data-v12.2.png" max-width="650px" alt="Table, filtered and bar chart returning no results" >}}
In cases where the data source you're using doesn't support filtering, consider using the special Dashboard data source. For more information, refer to Filter any data using the Dashboard data source.
Switch variables display a switch with two configurable values representing enabled and disabled states. This variable type is useful when you need to:
Under the Switch options section of the page, configure the switch values:
In the Value pair type drop-down list, select one of the following predefined options or choose Custom to define your own values:
true and false1 and 0yes and noIf you selected Custom in the previous step, configure the custom values:
Click Save dashboard.
Click Back to dashboard and Exit edit.
The following example shows a switch variable $debug_mode used in a Prometheus query to conditionally include debug labels:
up{job="my-service"} and ($debug_mode == "true" or on() vector(0))
The following example shows a switch variable $show_errors used to filter log entries:
{job="application"} |= ($show_errors == "1" ? "ERROR" : "")
You can also use switch variables in panel titles and other dashboard elements:
{{#if debug_mode}}Debug Mode: {{/if}}Application Metrics
Selection Options are a feature you can use to manage variable option selections. All selection options are optional, and they are off by default.
Interpolating a variable with multiple values selected is tricky as it is not straight forward how to format the multiple values 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.
{{< admonition type="note" >}}
The Custom all value option on the variable must be blank for Grafana to format all values into a single string. If it is left blank, then Grafana concatenates (adds together) all the values in the query. Something like value1,value2,value3. If a custom all value is used, then instead the value is something like * or all.
{{< /admonition >}}
Graphite uses glob expressions. A variable with multiple values would, in this case, be interpolated as {host1,host2,host3} if the current variable value was host1, host2, and host3.
InfluxDB and Prometheus use regular expressions, so the same variable would be interpolated as (host1|host2|host3). Every value would also be regular expression escaped. If not, a value with a regular expression control character would break the regular expression.
Elasticsearch uses Lucene query syntax, so the same variable would be formatted as ("host1" OR "host2" OR "host3"). In this case, every value must be escaped so that the value only contains Lucene control words and quotation marks.
If you have a multi-value variable that's formatted as an array, you can use array positions to reference the values rather than the actual values. You can use this functionality in dashboard panels to filter data, and when you do so, the array is maintained.
To reference variable values this way, use the following syntax:
${query0.0}
The preceding syntax references the first, or 0, position in the array.
In the following example, there's an array of three values, 1t, 2t, and 3t, and rather than referencing those values, the panel query references the second value in the array using the syntax ${query0.1}:
{{< figure src="/media/docs/grafana/dashboards/screenshot-indexed-variables-v12.1.png" max-width="750px" alt="Panel query using variable indexing to reference a value" >}}
Automatic escaping and formatting can cause problems and it can be tricky to grasp the logic behind it. Especially for InfluxDB and Prometheus where the use of regular expression syntax requires that the variable is used in regular expression operator context.
If you do not want Grafana to do this automatic regular expression escaping and formatting, then you must do one of the following:
Grafana adds an All option to the variable dropdown list. If a user selects this option, then all variable options are selected.
This option is only visible if the Include All option is selected.
Enter regular expressions, globs, or Lucene syntax in the Custom all value field to define the value of the All option.
By default the All value includes all options in combined expression. This can become very long and can have performance problems. Sometimes it can be better to specify a custom all value, like a wildcard regular expression.
In order to have custom regular expression, globs, or Lucene syntax in the Custom all value option, it is never escaped so you have to think about what is a valid value for your data source.
If you have a multi-source dashboard that uses multiple variables for the same logical concept—for example, an environment identified as dev vs development—, you can set up a multi-property variable to let you reference all those values. Instead of creating and keeping multiple variables for the same logical concept in sync, you can map all of those identifiers to one variable and then reference any property you need in panels and queries.
To do so, configure a JSON array in which each object can have any number of properties. Then, you can reference any of the properties in as you use those variables.
This applies to the following variable types:
To create a custom variable with multiple properties, define a JSON array, like this:
[
{ "value": "1", "text": "Development", "aws": "dev", "azure": "development", "google": "googledev" },
{ "value": "2", "text": "Staging", "aws": "stag", "azure": "staging", "google": "googlestag" },
{ "value": "3", "text": "Production", "aws": "prod", "azure": "production", "google": "googleprod" }
]
This is how those values are displayed in a preview:
{{< figure src="/media/docs/grafana/dashboards/screenshot-multipropvar-preview-vals-v12.4.png" max-width="600px" alt="Custom variable configuration and preview of values" >}}
Then you can use ${varName.someProperty} syntax to reference any property in your dashboard panels or metrics:
{{< figure src="/media/docs/grafana/dashboards/screenshot-multipropvar-used-12.4.png" max-width=750px" alt="Multi-property variable used in a text panel" >}}
You can even deeply nest properties and still access them using familiar variable syntax. In the following example, each user has an address property with all the elements of an address nested within it:
{{< figure src="/media/docs/grafana/dashboards/screenshot-multipropvar-nested-v12.4.png" max-width="650px" alt="Nested variable configuration" >}}
Because query configuration is different for each data source, there's no one way to set up a JSON array to create a multi-property query variable. For PostgreSQL, refer to PostgreSQL template variables. For other data sources, refer to the relevant Data sources documentation for preinstalled data source plugins. For configuration information on all other data source plugins, refer to the Plugins documentation.
Grafana has global built-in variables that can be used in expressions in the query editor. This topic lists them in alphabetical order and defines them. These variables are useful in queries, dashboard links, panel links, and data links.
$__dashboardThis variable is the name of the current dashboard.
$__from and $__toGrafana has two built-in time range variables: $__from and $__to. They are currently always interpolated as epoch milliseconds by default, but you can control date formatting.
| Syntax | Example result | Description |
|---|---|---|
${__from} | 1594671549254 | Unix millisecond epoch |
${__from:date} | 2020-07-13T20:19:09.254Z | No arguments, defaults to ISO 8601/RFC 3339 |
${__from:date:iso} | 2020-07-13T20:19:09.254Z | ISO 8601/RFC 3339 |
${__from:date:seconds} | 1594671549 | Unix seconds epoch |
${__from:date:YYYY-MM} | 2020-07 | Any custom date format that does not include the : character. Uses browser time. Use :date or :date:iso for UTC |
The syntax above also works with ${__to}.
You can use this variable in URLs, as well. For example, you can send a user to a dashboard that shows a time range from six hours ago until now: https://play.grafana.org/d/000000012/grafana-play-home?viewPanel=2&orgId=1?from=now-6h&to=now
$__intervalYou can use the $__interval variable as a parameter to group by time (for InfluxDB, MySQL, Postgres, MSSQL), Date histogram interval (for Elasticsearch), or as a summarize function parameter (for Graphite).
Grafana automatically calculates an interval that can be used to group by time in queries. When there are more data points than can be shown on a graph, then queries can be made more efficient by grouping by a larger interval. It is more efficient to group by 1 day than by 10s when looking at 3 months of data. The graph looks the same and the query is faster. The $__interval is calculated using the time range and the width of the graph (the number of pixels).
Approximate Calculation: (to - from) / resolution
For example, when the time range is 1 hour and the graph is full screen, then the interval might be calculated to 2m - points are grouped in 2 minute intervals. If the time range is 6 months and the graph is full screen, then the interval might be 1d (1 day) - points are grouped by day.
In the InfluxDB data source, the legacy variable $interval is the same variable. $__interval should be used instead.
The InfluxDB and Elasticsearch data sources have Group by time interval fields that are used to hard code the interval or to set the minimum limit for the $__interval variable (by using the > syntax -> >10m).
$__interval_msThis variable is the $__interval variable in milliseconds, not a time interval formatted string. For example, if the $__interval is 20m then the $__interval_ms is 1200000.
$__nameThis variable is only available in the Singlestat panel and can be used in the prefix or suffix fields on the Options tab. The variable is replaced with the series name or alias.
{{< admonition type="note" >}} The Singlestat panel is no longer available from Grafana 8.0. {{< /admonition >}}
$__orgThis variable is the ID of the current organization.
${__org.name} is the name of the current organization.
$__user${__user.id} is the ID of the current user.
${__user.login} is the login handle of the current user.
${__user.email} is the email for the current user.
$__rangeCurrently only supported for Prometheus and Loki data sources. This variable represents the range for the current dashboard. It is calculated by to - from. It has a millisecond and a second representation called $__range_ms and $__range_s.
$__rate_intervalCurrently only supported for Prometheus data sources. The $__rate_interval variable is meant to be used in the rate function. Refer to Prometheus query variables for details.
$__rate_interval_msThis variable is the $__rate_interval variable in milliseconds, not a time-interval-formatted string. For example, if the $__rate_interval is 20m then the $__rate_interval_ms is 1200000.
$timeFilter or $__timeFilterThe $timeFilter variable returns the currently selected time range as an expression. For example, the time range interval Last 7 days expression is time > now() - 7d.
This is used in several places, including:
WHERE $timeFilter.$__timeFilter variable is used in the MySQL data source.$__timezoneThe $__timezone variable returns the currently selected time zone, either utc or an entry of the IANA time zone database (for example, America/New_York).
If the currently selected time zone is Browser Time, Grafana tries to determine your browser time zone.
Chained variables, also called linked variables or nested variables, are query variables with one or more other variables in their variable query. This section explains how chained variables work and provides links to example dashboards that use chained variables.
Chained variable queries are different for every data source, but the premise is the same for all. You can use chained variable queries in any data source that allows them.
Extremely complex linked templated dashboards are possible, 5 or 10 levels deep. Technically, there is no limit to how deep or complex you can go, but the more links you have, the greater the query load.
The following Grafana Play dashboards contain fairly simple chained variables, only two layers deep. To view the variables and their settings, click Edit and then Settings; then go to the Variables tab. Both examples are expanded in the following section.
Variables are useful to reuse dashboards and dynamically change what is shown in dashboards. Chained variables are especially useful to filter what you see.
Create parent/child relationship in a variable, sort of a tree structure where you can select different levels of filters.
The following sections explain the linked examples in the dashboards above in depth and builds on them. While the examples are data source-specific, the concepts can be applied broadly.
In this example, there are several applications. Each application has a different subset of servers. It is based on the Graphite Templated Nested.
Now, you could make separate variables for each metric source, but then you have to know which server goes with which app. A better solution is to use one variable to filter another. In this example, when the user changes the value of the app variable, it changes the dropdown options returned by the server variable. Both variables use the Multi-value option and Include all option, enabling users to select some or all options presented at any time.
app variableThe query for this variable basically says, "Find all the applications that exist."
apps.*
The values returned are backend, country, fakesite, and All.
server variableThe query for this variable basically says, "Find all servers for the currently chosen application."
apps.$app.*
If the user selects backend, then the query changes to:
apps.backend.*
The query returns all servers associated with backend, including backend_01, backend_02, and so on.
If the user selects fakesite, then the query changes to:
apps.fakesite.*
The query returns all servers associated with fakesite, including web_server_01, web_server_02, and so on.
{{< admonition type="note" >}} This example is theoretical. The Graphite server used in the example does not contain CPU metrics. {{< /admonition >}}
The dashboard stops at two levels, but you could keep going. For example, if you wanted to get CPU metrics for selected servers, you could copy the server variable and extend the query so that it reads:
apps.$app.$server.cpu.*
This query basically says, "Find the CPU metrics for the selected server."
Depending on what variable options the user selects, you could get queries like:
apps.backend.backend_01.cpu.*
apps.{backend.backend_02,backend_03}.cpu.*
apps.fakesite.web_server_01.cpu.*
In this example, you have several data centers. Each data center has a different subset of hosts. It is based on the InfluxDB Templated dashboard.
In this example, when the user changes the value of the datacenter variable, it changes the dropdown options returned by the host variable. The host variable uses the Multi-value option and Include all option, allowing users to select some or all options presented at any time. The datacenter does not use either option, so you can only select one data center at a time.
datacenter variableThe query for this variable basically says, "Find all the data centers that exist."
SHOW TAG VALUES WITH KEY = "datacenter"
The values returned are America, Africa, Asia, and Europe.
host variableThe query for this variable basically says, "Find all hosts for the currently chosen data center."
SHOW TAG VALUES WITH KEY = "hostname" WHERE "datacenter" =~ /^$datacenter$/
If the user selects America, then the query changes to:
SHOW TAG VALUES WITH KEY = "hostname" WHERE "datacenter" =~ /^America/
The query returns all servers associated with America, including server1, server2, and so on.
If the user selects Europe, then the query changes to:
SHOW TAG VALUES WITH KEY = "hostname" WHERE "datacenter" =~ /^Europe/
The query returns all servers associated with Europe, including server3, server4, and so on.
{{< admonition type="note" >}} This example is theoretical. The InfluxDB server used in the example does not contain CPU metrics. {{< /admonition >}}
The dashboard stops at two levels, but you could keep going. For example, if you wanted to get CPU metrics for selected hosts, you could copy the host variable and extend the query so that it reads:
SHOW TAG VALUES WITH KEY = "cpu" WHERE "datacenter" =~ /^$datacenter$/ AND "host" =~ /^$host$/
This query basically says, "Find the CPU metrics for the selected host."
Depending on what variable options the user selects, you could get queries like:
SHOW TAG VALUES WITH KEY = "cpu" WHERE "datacenter" =~ /^America/ AND "host" =~ /^server2/
SHOW TAG VALUES WITH KEY = "cpu" WHERE "datacenter" =~ /^Africa/ AND "host" =~ /^server/7/
SHOW TAG VALUES WITH KEY = "cpu" WHERE "datacenter" =~ /^Europe/ AND "host" =~ /^server3+server4/
The following practices make your dashboards and variables easier to use.
You can change the orders of variables in the dashboard variable list by clicking the up and down arrows on the right side of each entry. Grafana lists variable dropdowns left to right according to this list, with the variable at the top on the far left.
The more layers of dependency you have in variables, the longer it takes to update dashboards after you change variables.
For example, if you have a series of four linked variables (country, region, server, metric) and you change a root variable value (country), then Grafana must run queries for all the dependent variables before it updates the visualizations in the dashboard.
<!-- vale Grafana.WordList = NO -->Using the Regex query option, you filter the list of options returned by the variable query or modify the options returned.
This page shows how to use a regular expression to filter/modify values in the variable dropdown.
Using the Regex query option, you filter the list of options returned by the Variable query or modify the options returned. For more information, refer to the Mozilla guide on Regular expressions.
Examples of filtering on the following list of options:
backend_01
backend_02
backend_03
backend_04
01 or 02 are returned:Regex:
/(01|02)$/
Result:
backend_01
backend_02
Regex:
/.*(01|02)/
Result:
01
02
List of options:
up{instance="demo.robustperception.io:9090",job="prometheus"} 1 1521630638000
up{instance="demo.robustperception.io:9093",job="alertmanager"} 1 1521630638000
up{instance="demo.robustperception.io:9100",job="node"} 1 1521630638000
Regex:
/.*instance="([^"]*).*/
Result:
demo.robustperception.io:9090
demo.robustperception.io:9093
demo.robustperception.io:9100
Using named capture groups, you can capture separate 'text' and 'value' parts from the options returned by the variable query. This allows the variable drop-down list to contain a friendly name for each value that can be selected.
For example, when querying the node_hwmon_chip_names Prometheus metric, the chip_name is a lot friendlier than the chip value. So the following variable query result:
node_hwmon_chip_names{chip="0000:d7:00_0_0000:d8:00_0",chip_name="enp216s0f0np0"} 1
node_hwmon_chip_names{chip="0000:d7:00_0_0000:d8:00_1",chip_name="enp216s0f0np1"} 1
node_hwmon_chip_names{chip="0000:d7:00_0_0000:d8:00_2",chip_name="enp216s0f0np2"} 1
node_hwmon_chip_names{chip="0000:d7:00_0_0000:d8:00_3",chip_name="enp216s0f0np3"} 1
Passed through the following regular expression:
/chip_name="(?<text>[^"]+)|chip="(?<value>[^"]+)/g
Would produce the following drop-down list:
Display Name Value
------------ -------------------------
enp216s0f0np0 0000:d7:00_0_0000:d8:00_0
enp216s0f0np1 0000:d7:00_0_0000:d8:00_1
enp216s0f0np2 0000:d7:00_0_0000:d8:00_2
enp216s0f0np3 0000:d7:00_0_0000:d8:00_3
{{< admonition type="note" >}}
Only text and value capture group names are supported.
{{< /admonition >}}