Azure Monitor Management Client Library
The Microsoft Azure Monitor Client Library for Python allows you to manage Azure Monitor resources, including metric definitions, metrics, activity logs, diagnostic settings, and alerts. It's part of the Azure SDK for Python, which follows a predictable release cadence with frequent updates across its many client libraries.
Warnings
- breaking Major API changes from 'Track 1' (older) to 'Track 2' (current) versions. This includes different client constructors, model structures, and method signatures.
- gotcha Authentication requires a credential object. Relying solely on older methods (e.g., hardcoded service principal credentials) is discouraged.
- gotcha Many Azure Monitor operations require full Azure Resource IDs (ARNs) rather than just resource names or GUIDs.
- gotcha Asynchronous clients are available for all management libraries, often denoted by an `Async` suffix (e.g., `MonitorManagementClientAsync`). Mixing synchronous and asynchronous code can lead to issues.
Install
-
pip install azure-mgmt-monitor azure-identity
Imports
- MonitorManagementClient
from azure.mgmt.monitor import MonitorManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- MetricDefinition
from azure.mgmt.monitor.models import MetricDefinition
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
# Set your Azure Subscription ID as an environment variable or replace 'YOUR_SUBSCRIPTION_ID'
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
print("WARNING: Please set the AZURE_SUBSCRIPTION_ID environment variable ")
print(" or replace 'YOUR_SUBSCRIPTION_ID' in the code.")
# Attempt to proceed for demonstration, but subsequent calls will likely fail.
try:
# Authenticate using DefaultAzureCredential. This will try various methods
# (environment variables, managed identity, Azure CLI, VS Code, etc.)
credential = DefaultAzureCredential()
# Create a Monitor Management client
monitor_client = MonitorManagementClient(credential, subscription_id)
print(f"Listing diagnostic settings categories for subscription '{subscription_id}'...")
# The resource_uri can be a subscription, resource group, or specific resource.
# For listing categories, the subscription-level URI is sufficient.
resource_uri = f"/subscriptions/{subscription_id}"
categories = monitor_client.diagnostic_settings_categories.list(resource_uri=resource_uri)
found_categories = False
for category in categories:
print(f" - Category Name: {category.name}, Type: {category.category_type}")
found_categories = True
if not found_categories:
print("No diagnostic settings categories found (or subscription ID/permissions are invalid).")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you are authenticated (e.g., by running `az login`) ")
print("and that your AZURE_SUBSCRIPTION_ID environment variable is correctly set.")