azure-monitor-query-py – skill DevOps & Hạ tầng cho agent AI
SDK Azure Monitor Query cho Python. Dùng để truy vấn Log Analytics workspace và metric của Azure Monitor.
Cài skill azure-monitor-query-py bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-monitor-query-py-95c60.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /azure-monitor-query-py – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-monitor-query-py-95c60.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $azure-monitor-query-py – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-monitor-query-py-95c60.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /azure-monitor-query-py – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-monitor-query-py-95c60.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /azure-monitor-query-py – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Azure Monitor Query SDK for Python
Query logs and metrics from Azure Monitor and Log Analytics workspaces.
Installation
pip install azure-monitor-query azure-identityEnvironment Variables
# Log Analytics
AZURE_LOG_ANALYTICS_WORKSPACE_ID=<workspace-id>
# Metrics
AZURE_METRICS_RESOURCE_URI=/subscriptions/<sub>/resourceGroups/<rg>/providers/<provider>/<type>/<name>Authentication
import os
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=1))
if response.tables:
table = response.tables[0]
df = pd.DataFrame(data=table.rows, columns=table.columns)
print(df.head())Batch Query
from azure.monitor.query import LogsBatchQuery
queries = [
LogsBatchQuery(workspace_id=workspace_id, query="AppRequests | take 5", timespan=timedelta(hours=1)),
LogsBatchQuery(workspace_id=workspace_id, query="AppExceptions | take 5", timespan=timedelta(hours=1))
]
responses = client.query_batch(queries)
for response in responses:
if response.tables:
print(f"Rows: {len(response.tables[0].rows)}")Handle Partial Results
from azure.monitor.query import LogsQueryStatus
response = client.query_workspace(workspace_id, query, timespan=timedelta(hours=24))
if response.status == LogsQueryStatus.PARTIAL:
print(f"Partial results: {response.partial_error}")
elif response.status == LogsQueryStatus.FAILURE:
print(f"Query failed: {response.partial_error}")Metrics Query Client (legacy azure-monitor-query 1.x only)
Version 2.0 removed MetricsQueryClient; do not run the following legacy fragments with the unpinned current install above. For new work use azure-monitor-querymetrics or azure-mgmt-monitor with their documented schemas. See [official migration notice](https://learn.microsoft.com/python/api/overview/azure/monitor-query-readme).
Query Resource Metrics
from azure.monitor.query import MetricsQueryClient
from datetime import timedelta
metrics_client = MetricsQueryClient(credential)
response = metrics_client.query_resource(
resource_uri=os.environ["AZURE_METRICS_RESOURCE_URI"],
metric_names=["Percentage CPU", "Network In Total"],
timespan=timedelta(hours=1),
granularity=timedelta(minutes=5)
)
for metric in response.metrics:
print(f"{metric.name}:")
for time_series in metric.timeseries:
for data in time_series.data:
print(f" {data.timestamp}: {data.average}")Aggregations
from azure.monitor.query import MetricAggregationType
response = metrics_client.query_resource(
resource_uri=resource_uri,
metric_names=["Requests"],
timespan=timedelta(hours=1),
aggregations=[
MetricAggregationType.AVERAGE,
MetricAggregationType.MAXIMUM,
MetricAggregationType.MINIMUM,
MetricAggregationType.COUNT
]
)Filter by Dimension
response = metrics_client.query_resource(
resource_uri=resource_uri,
metric_names=["Requests"],
timespan=timedelta(hours=1),
filter="ApiName eq 'GetBlob'"
)List Metric Definitions
definitions = metrics_client.list_metric_definitions(resource_uri)
for definition in definitions:
print(f"{definition.name}: {definition.unit}")List Metric Namespaces
namespaces = metrics_client.list_metric_namespaces(resource_uri)
for ns in namespaces:
print(ns.fully_qualified_namespace)Async Clients
from azure.monitor.query.aio import LogsQueryClient
from azure.identity.aio import DefaultAzureCredential
async def query_logs():
credential = DefaultAzureCredential()
client = LogsQueryClient(credential)
response = await client.query_workspace(
workspace_id=workspace_id,
query="AppRequests | take 10",
timespan=timedelta(hours=1)
)
await client.close()
await credential.close()
return responseCommon Kusto Queries
// Requests by status code
AppRequests
| summarize count() by ResultCode
| order by count_ desc
// Exceptions over time
AppExceptions
| summarize count() by bin(TimeGenerated, 1h)
// Slow requests
AppRequests
| where DurationMs > 1000
| project TimeGenerated, Name, DurationMs
| order by DurationMs desc
// Top errors
AppExceptions
| summarize count() by ExceptionType
| top 10 by count_Client Types
| Client | Purpose |
|--------|---------|
| LogsQueryClient | Query Log Analytics workspaces |
| MetricsQueryClient | Query Azure Monitor metrics |
Best Practices
1. Use timedelta for relative time ranges
2. Handle partial results for large queries
3. Use batch queries when running multiple queries
4. Set appropriate granularity for metrics to reduce data points
5. Convert to DataFrame for easier data analysis
6. Use aggregations to summarize metric data
7. Filter by dimensions to narrow metric results
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Skill liên quan
- azure-networking – Cấu hình Azure VNet, NSG và Azure Firewall. Triển khai mô hình hub-spoke và private…
- azure-resource-manager-cosmosdb-dotnet – SDK Azure Resource Manager cho Cosmos DB trên .NET.
- azure-resource-manager-durabletask-dotnet – SDK Azure Resource Manager cho Durable Task Scheduler trên .NET.
- azure-resource-manager-mysql-dotnet – SDK Azure MySQL Flexible Server cho .NET. Quản lý cơ sở dữ liệu cho các triển khai MySQL…
- azure-resource-manager-playwright-dotnet – SDK Azure Resource Manager cho Microsoft Playwright Testing trên .NET.
- azure-resource-manager-redis-dotnet – SDK Azure Resource Manager cho Redis trên .NET.
- azure-resource-manager-sql-dotnet – SDK Azure Resource Manager cho Azure SQL trên .NET.
- azure-search-documents-dotnet – SDK Azure AI Search cho .NET (Azure.Search.Documents). Dùng để xây dựng ứng dụng tìm…