docs/sources/datasources/azure-monitor/template-variables/index.md
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.
To create a template variable for Azure Monitor:
subscription, resourceGroup, resource).The Azure Monitor data source provides the following query types for template variables:
| Query type | Description |
|---|---|
| Subscriptions | Returns a list of Azure subscriptions accessible to the configured credentials. |
| Resource Groups | Returns resource groups for a specified subscription. Supports multi-value selection. |
| Namespaces | Returns metric namespaces for the specified subscription. If a resource group is specified, returns only namespaces within that group. |
| Regions | Returns Azure regions available for the specified subscription. |
| Resource Names | Returns resource names for a specified subscription, resource group, and namespace. Supports multi-value selection. |
| Metric Names | Returns available metric names for a specified resource. |
| Workspaces | Returns Log Analytics workspaces for the specified subscription. |
| Logs | Executes a KQL query and returns the results as variable values. See Create a Logs variable. |
| Custom Namespaces | Returns custom metric namespaces for a specified resource. |
| Custom Metric Names | Returns custom metric names for a specified resource. |
{{< admonition type="note" >}} Custom metrics cannot be emitted against a subscription or resource group. Select specific resources when retrieving custom metric namespaces or custom metric names. {{< /admonition >}}
Cascading variables (also called dependent or chained variables) allow you to create dropdown menus that filter based on previous selections. This is useful for drilling down from subscription to resource group to specific resource.
Step 1: Create a Subscription variable
subscription.Step 2: Create a Resource Group variable
resourceGroup.$subscription.Step 3: Create a Resource Name variable
resource.$subscription.$resourceGroup.Microsoft.Compute/virtualMachines).Now when you change the subscription, the resource group dropdown updates automatically, and when you change the resource group, the resource name dropdown updates.
The Logs query type lets you use a KQL query to populate variable values. The query must return a single column of values.
To create a Logs variable:
| Query | Returns |
|---|---|
Heartbeat | distinct Computer | List of virtual machine names |
Perf | distinct ObjectName | List of performance object names |
AzureActivity | distinct ResourceGroup | List of resource groups with activity |
AppRequests | distinct Name | List of application request names |
You can reference other variables in your Logs query:
workspace("$workspace").Heartbeat | distinct Computer
workspace("$workspace").Perf
| where ObjectName == "$object"
| distinct CounterName
Control when your variables refresh by setting the Refresh option:
| Option | Behavior |
|---|---|
| On dashboard load | Variables refresh each time the dashboard loads. Best for data that changes infrequently. |
| On time range change | Variables refresh when the dashboard time range changes. Use for time-sensitive queries. |
For dashboards with many variables or complex queries, use On dashboard load to improve performance.
After you create template variables, you can use them in your Azure Monitor queries by referencing them with the $ prefix.
In a Metrics query, select your variables in the resource picker fields:
$subscription$resourceGroup$resourceReference variables directly in your KQL queries:
Perf
| 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
You can enable Multi-value selection for Resource Groups and Resource Names variables. When using multi-value variables in a Metrics query, all selected resources must:
{{< admonition type="note" >}} When a multi-value variable is used as a parameter in another variable query (for example, to retrieve metric names), only the first selected value is used. Ensure the first resource group and resource name combination is valid. {{< /admonition >}}
If you encounter issues with template variables, try the following solutions.
$__contains() macro to handle multi-value variables properly.