This is documentation for the next version of Grafana documentation. For the latest stable release, go to the latest version.

Grafana Cloud 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.

Before you begin

Create a template variable

To create a template variable for Azure Monitor:

  1. Open the dashboard where you want to add the variable.
  2. Click Dashboard settings (gear icon) in the top navigation.
  3. Select Variables in the left menu.
  4. Click Add variable.
  5. Enter a Name for your variable (e.g., subscription, resourceGroup, resource).
  6. In the Type dropdown, select Query.
  7. In the Data source dropdown, select your Azure Monitor data source.
  8. In the Query Type dropdown, select the appropriate query type (see Available query types).
  9. Configure any additional fields required by the selected query type.
  10. Click Run query to preview the variable values.
  11. Configure display options such as Multi-value or Include All option as needed.
  12. Click Apply to save the variable.

Available query types

The Azure Monitor data source provides the following query types for template variables:

Query typeDescription
SubscriptionsReturns a list of Azure subscriptions accessible to the configured credentials.
Resource GroupsReturns resource groups for a specified subscription. Supports multi-value selection.
NamespacesReturns metric namespaces for the specified subscription. If a resource group is specified, returns only namespaces within that group.
RegionsReturns Azure regions available for the specified subscription.
Resource NamesReturns resource names for a specified subscription, resource group, and namespace. Supports multi-value selection.
Metric NamesReturns available metric names for a specified resource.
WorkspacesReturns Log Analytics workspaces for the specified subscription.
LogsExecutes a KQL query and returns the results as variable values. See Create a Logs variable.
Custom NamespacesReturns custom metric namespaces for a specified resource.
Custom Metric NamesReturns custom metric names for a specified resource.

Note

Custom metrics cannot be emitted against a subscription or resource group. Select specific resources when retrieving custom metric namespaces or custom metric names.

Create cascading variables

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.

Example: Subscription → Resource Group → Resource Name

Step 1: Create a Subscription variable

  1. Create a variable named subscription.
  2. Set Query Type to Subscriptions.

Step 2: Create a Resource Group variable

  1. Create a variable named resourceGroup.
  2. Set Query Type to Resource Groups.
  3. In the Subscription field, select $subscription.

Step 3: Create a Resource Name variable

  1. Create a variable named resource.
  2. Set Query Type to Resource Names.
  3. In the Subscription field, select $subscription.
  4. In the Resource Group field, select $resourceGroup.
  5. Select the appropriate Namespace for your resources (e.g., 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.

Create a Logs variable

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:

  1. Create a new variable with Query Type set to Logs.
  2. Select a Resource (Log Analytics workspace or Application Insights resource).
  3. Enter a KQL query that returns a single column.

Logs variable query examples

QueryReturns
Heartbeat | distinct ComputerList of virtual machine names
Perf | distinct ObjectNameList of performance object names
AzureActivity | distinct ResourceGroupList of resource groups with activity
AppRequests | distinct NameList of application request names

You can reference other variables in your Logs query:

kusto
workspace("$workspace").Heartbeat | distinct Computer
kusto
workspace("$workspace").Perf
| where ObjectName == "$object"
| distinct CounterName

Variable refresh options

Control when your variables refresh by setting the Refresh option:

OptionBehavior
On dashboard loadVariables refresh each time the dashboard loads. Best for data that changes infrequently.
On time range changeVariables 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.

Use variables in queries

After you create template variables, you can use them in your Azure Monitor queries by referencing them with the $ prefix.

Metrics query example

In a Metrics query, select your variables in the resource picker fields:

  • Subscription: $subscription
  • Resource Group: $resourceGroup
  • Resource Name: $resource

Logs query example

Reference variables directly in your KQL queries:

kusto
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

Multi-value variables

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:

  • Belong to the same subscription
  • Be in the same Azure region
  • Be of the same resource type (namespace)

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.

Troubleshoot template variables

If you encounter issues with template variables, try the following solutions.

Variable returns no values

  • Verify the Azure Monitor data source is configured correctly and can connect to Azure.
  • Check that the credentials have appropriate permissions to list the requested resources.
  • For cascading variables, ensure parent variables have valid selections.

Variable values are outdated

  • Check the Refresh setting and adjust if needed.
  • Click the refresh icon next to the variable dropdown to manually refresh.

Multi-value selection not working in queries

  • Ensure the resources meet the requirements (same subscription, region, and type).
  • For Logs queries, use the $__contains() macro to handle multi-value variables properly.