0
0
Azurecloud~5 mins

Metrics for resource performance in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run apps or services in the cloud, you want to know how well they are working. Metrics help you see if your resources like virtual machines or databases are healthy and fast. This helps you fix problems before users notice.
When you want to check if your virtual machine is using too much CPU or memory.
When you need to see how many requests your web app is handling per second.
When you want to monitor the disk space or network traffic of your cloud resources.
When you want to set alerts to get notified if a resource is not performing well.
When you want to analyze trends over time to plan for scaling your resources.
Commands
This command fetches the CPU usage percentage metric for the virtual machine named 'myVM' every 1 minute. It helps you see how busy the CPU is.
Terminal
az monitor metrics list --resource /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM --metric Percentage CPU --interval PT1M
Expected OutputExpected
{ "cost": 0, "timeseries": [ { "data": [ { "timeStamp": "2024-06-01T12:00:00Z", "average": 15.3 }, { "timeStamp": "2024-06-01T12:01:00Z", "average": 18.7 } ] } ] }
--resource - Specifies the exact resource to get metrics from
--metric - Chooses which performance metric to retrieve
--interval - Sets how often to collect metric data
This command lists all available metrics you can monitor for the virtual machine 'myVM'. It helps you know what performance data you can collect.
Terminal
az monitor metrics list-definitions --resource /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM
Expected OutputExpected
{ "value": [ { "name": { "value": "Percentage CPU", "localizedValue": "Percentage CPU" }, "unit": "Percent" }, { "name": { "value": "Network In", "localizedValue": "Network In" }, "unit": "Bytes" } ] }
--resource - Specifies the resource to check available metrics
This command creates an alert named 'HighCPUAlert' that triggers when the average CPU usage of 'myVM' goes above 80%. Alerts help you react quickly to performance issues.
Terminal
az monitor metrics alert create --name HighCPUAlert --resource-group myResourceGroup --scopes /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM --condition "avg Percentage CPU > 80" --description "Alert when CPU usage is over 80%"
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/microsoft.insights/metricAlerts/HighCPUAlert", "name": "HighCPUAlert", "type": "microsoft.insights/metricalerts", "location": "global", "properties": { "description": "Alert when CPU usage is over 80%", "enabled": true } }
--name - Names the alert rule
--scopes - Defines which resource the alert monitors
--condition - Sets the condition that triggers the alert
This command gets the number of requests received by the web app 'myWebApp' every 5 minutes. It helps you understand how much traffic your app is getting.
Terminal
az monitor metrics list --resource /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/myWebApp --metric Requests --interval PT5M
Expected OutputExpected
{ "timeseries": [ { "data": [ { "timeStamp": "2024-06-01T12:00:00Z", "total": 120 }, { "timeStamp": "2024-06-01T12:05:00Z", "total": 135 } ] } ] }
--resource - Specifies the web app resource
--metric - Chooses the metric to retrieve
--interval - Sets the data collection frequency
Key Concept

If you remember nothing else from this pattern, remember: metrics show how your cloud resources are performing so you can keep them healthy and fast.

Common Mistakes
Using incorrect resource ID format in the command
The command fails because Azure cannot find the resource without the full correct ID
Always use the full resource ID starting with /subscriptions/ and including resource group and provider
Requesting a metric that is not available for the resource
The command returns an error or empty data because the metric does not exist for that resource type
Use 'az monitor metrics list-definitions' to check available metrics before querying
Not specifying the interval flag or using an invalid interval format
The command may return data points that are too sparse or the command fails due to wrong interval syntax
Use ISO 8601 duration format like PT1M for 1 minute or PT5M for 5 minutes
Summary
Use 'az monitor metrics list' with the full resource ID to get performance data like CPU or requests.
Check available metrics for a resource with 'az monitor metrics list-definitions' before querying.
Create alerts with 'az monitor metrics alert create' to get notified when performance crosses thresholds.