Add a query variable
Query variables allow 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 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.
Query expressions are different for each data source. For more information, refer to the documentation for your data source.
Enter General options
- Navigate to the dashboard you want to make a variable for and then click the Dashboard settings (gear) icon at the top of the page.
- On the Variables tab, click New.
- Enter a Name for your variable.
- In the Type list, select Query.
- (optional) In Label, enter the display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
- Choose a Hide option:
- No selection (blank) - The variable dropdown displays the variable Name or Label value. This is the default.
- Label - The variable dropdown only displays the selected variable value and a down arrow.
- Variable - No variable dropdown is displayed on the dashboard.
Enter Query Options
- In the Data source list, select the target data source for the query. For more information about data sources, refer to Add a data source.
- In the Refresh list, select when the variable should update options.
- On Dashboard Load - Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized.
- On Time Range Change - Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
- In the Query field, enter a query.
- The query field varies according to your data source. Some data sources have custom query editors.
- If you need more room in a single input field query editor, then hover your cursor over the lines in the lower right corner of the field and drag downward to expand.
- (optional) In the Regex field, type a regex expression to filter or capture specific parts of the names returned by your data source query. To see examples, refer to Filter variables with regex.
- In the Sort 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 will be used.
- (optional) Enter Selection Options.
- In Preview of values, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.
- Click Add to add the variable to the dashboard.
Related Grafana resources
GrafanaCONline 2021 is coming June 7-17
Be the first to learn about exciting next-generation features in Grafana 8.0, be inspired by what community members are building, and attend expert-led sessions and workshops on Grafana, Prometheus, Loki logs, and more.
Unify your data with Grafana plugins: Splunk, MongoDB, Datadog, and more
Show how Grafana can be used to take data from multiple different sources and unify it, without disrupting the investments that are working today.
Demo: Getting started with Grafana Enterprise and observability
Join the Grafana Labs team for a 30-minute demo of how to get started with the Grafana Stack, so you can go from zero to observability in just a few minutes.