| name | azure-monitor-query-py |
| description | Azure Monitor Query SDK for Python. Use for querying Log Analytics workspaces and Azure Monitor metrics. |
| type | skill |
| created | 2026-02-27T00:00:00.000Z |
| domain | cloud-infrastructure |
| category | azure |
| risk | unknown |
| source | community |
| tags | ["skill","cloud-infrastructure","azure","monitor","query"] |
Azure Monitor Query SDK for Python
Query logs and metrics from Azure Monitor and Log Analytics workspaces.
Installation
pip install azure-monitor-query
Environment Variables
AZURE_LOG_ANALYTICS_WORKSPACE_ID=<workspace-id>
AZURE_METRICS_RESOURCE_URI=/subscriptions/<sub>/resourceGroups/<rg>/providers/<provider>/<type>/<name>
Authentication
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
Logs Query Client
Basic Query
from azure.monitor.query import LogsQueryClient
from datetime import timedelta
client = LogsQueryClient(credential)
query = """
AppRequests
| where TimeGenerated > ago(1h)
| summarize count() by bin(TimeGenerated, 5m), ResultCode
| order by TimeGenerated desc
"""
response = client.query_workspace(
workspace_id=os.environ["AZURE_LOG_ANALYTICS_WORKSPACE_ID"],
query=query,
timespan=timedelta(hours=1)
)
for table in response.tables:
for row in table.rows:
print(row)
Query with Time Range
from datetime import datetime, timezone
response = client.query_workspace(
workspace_id=workspace_id,
query="AppRequests | take 10",
timespan=(
datetime(2024, 1, 1, tzinfo=timezone.utc),
datetime(2024, 1, 2, tzinfo=timezone.utc)
)
)
Convert to DataFrame
import pandas as pd
response = client.query_workspace(workspace_id, query, timespan=timedelta(hours=))
response.tables:
table = response.tables[]
df = pd.DataFrame(data=table.rows, columns=[col.name col table.columns])
(df.head())