Template variables

Instead of hard-coding values for fields like resource group or resource name in your queries, you can use variables in their place to create more interactive, dynamic, and reusable dashboards.

Check out the Templating documentation for an introduction to the templating feature and the different types of template variables.

The Azure Monitor data source provides the following queries you can specify in the Query field in the Variable edit view

NameDescription
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 KQL query that returns a single list of values can also be used in the Query field. For example:

QueryDescription
workspace(“myWorkspace”).Heartbeat | distinct ComputerReturns a list of Virtual Machines
workspace(“$workspace”).Heartbeat | distinct ComputerReturns a list of Virtual Machines with template variable
workspace(“$workspace”).Perf | distinct ObjectNameReturns a list of objects from the Perf table
workspace(“$workspace”).Perf | where ObjectName == “$object” | distinct CounterNameReturns a list of metric names from the Perf table

Example of a time series query using variables:

  1. Perf
  2. | where ObjectName == "$object" and CounterName == "$metric"
  3. | where TimeGenerated >= $__timeFrom() and TimeGenerated <= $__timeTo()
  4. | where $__contains(Computer, $computer)
  5. | summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer
  6. | order by TimeGenerated asc