Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Enterprise Open source

Azure Monitor template variables

Instead of hard-coding details such as resource group or resource name values in metric queries, you can use variables. This helps you create more interactive, dynamic, and reusable dashboards. Grafana refers to such variables as template variables.

For an introduction to templating and template variables, refer to the Templating and Add and manage variables documentation.

Use query variables

You can specify these Azure Monitor data source queries in the Variable edit view’s Query Type field.

SubscriptionsReturns subscriptions.
Resource GroupsReturns resource groups for a specified subscription.
NamespacesReturns metric namespaces for the specified subscription and resource group.
Resource NamesReturns a list of resource names for a specified subscription, resource group and namespace.
Metric NamesReturns a list of metric names for a resource.
WorkspacesReturns a list of workspaces for the specified subscription.
LogsUse a KQL query to return values.
Resource GraphUse an ARG query to return values.

Any Log Analytics Kusto Query Language (KQL) query that returns a single list of values can also be used in the Query field. For example:

QueryList of values returned
workspace("myWorkspace").Heartbeat | distinct ComputerVirtual machines
workspace("$workspace").Heartbeat | distinct ComputerVirtual machines with template variable
workspace("$workspace").Perf | distinct ObjectNameObjects from the Perf table
workspace("$workspace").Perf | where ObjectName == "$object" | distinct CounterNameMetric names from the Perf table

Query variable example

This time series query uses query variables:

| where ObjectName == "$object" and CounterName == "$metric"
| where TimeGenerated >= $__timeFrom() and TimeGenerated <= $__timeTo()
| where  $__contains(Computer, $computer)
| summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer
| order by TimeGenerated asc