Datadog 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 drop-down lists 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 templates and variables, refer to the following topics:
To add a Datadog query variable, refer to Add a query variable. Set Select variable type to Query, select your Datadog data source, and enter one of the following queries in the Query field:
After you create a variable, use it in Datadog queries with $variable or ${variable} syntax. For more information, refer to
Variable syntax.
- If the variable lists host names from
all-hosts, filter withhost:$host. - If the variable lists full tags from
all-tagsor a keyword query, filter with$tag(for example,avg:system.cpu.user{$region}).
Examples
The following examples show how to create variables and use them in queries.
Filter panels by host
Create a drop-down that filters every panel by host:
- In Dashboard settings > Variables, click Add variable.
- Set Select variable type to Query and select your Datadog data source.
- In the Query field, enter
all-hosts. - Name the variable
hostand click Apply.
Use the variable as a tag filter. For example, in a Raw Query, enter avg:system.cpu.user{host:$host}. In a Query type query, add host:$host in From. When you select a host from the drop-down, the panel updates to show only that host.
List the tags for a metric
Create a variable whose values are the tags associated with a specific metric:
- Add a Query variable that uses your Datadog data source.
- In the Query field, enter
<metric>:all-tags, such assystem.cpu.user:all-tags. - Name the variable and click Apply.
The variable’s drop-down lists tags reported for that metric, such as host:web-01 or env:prod. Use a selected value as a filter, for example avg:system.cpu.user{$tag} in a Raw Query.
Ad hoc filters
The
Ad hoc filters variable type is included with Grafana. With a Datadog data source, selected key:value tag pairs are applied to Query type metric queries (the visual builder From tags). To create this variable, complete the following steps:
- Open your dashboard and go to Dashboard settings.
- Click Variables.
- Click Add variable.
- Set Select variable type to Ad hoc filters.
- Enter general options.
- Select your Datadog data source.
Note
Ad hoc filters apply to the Query metric query type. An Arithmetic query also inherits these filters through the Query queries it references. Filters aren’t applied directly to Raw Query, Logs, RUM, APM Spans, Monitor, Events, or SLO queries.
Instead of a fixed list of values, this variable lets dashboard users add and remove tag filters. Filters you apply this way affect every Query type panel that uses that Datadog data source on the dashboard.


