azure-monitor-ingestion-py – skill DevOps & Hạ tầng cho agent AI
SDK Azure Monitor Ingestion cho Python. Dùng để gửi log tùy chỉnh tới Log Analytics workspace qua Logs Ingestion API.
Cài skill azure-monitor-ingestion-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-ingestion-py-365d7.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /azure-monitor-ingestion-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-ingestion-py-365d7.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $azure-monitor-ingestion-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-ingestion-py-365d7.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /azure-monitor-ingestion-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-ingestion-py-365d7.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /azure-monitor-ingestion-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 Ingestion SDK for Python
Send custom logs to Azure Monitor Log Analytics workspace using the Logs Ingestion API.
Installation
pip install azure-monitor-ingestion
pip install azure-identityEnvironment Variables
# Data Collection Endpoint (DCE)
AZURE_DCE_ENDPOINT=https://<dce-name>.<region>.ingest.monitor.azure.com
# Data Collection Rule (DCR) immutable ID
AZURE_DCR_RULE_ID=dcr-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Stream name from DCR
AZURE_DCR_STREAM_NAME=Custom-MyTable_CLPrerequisites
Before using this SDK, you need:
1. Log Analytics Workspace — Target for your logs
2. Data Collection Endpoint (DCE) — Ingestion endpoint
3. Data Collection Rule (DCR) — Defines schema and destination
4. Custom Table — In Log Analytics (created via DCR or manually)
Authentication
from azure.monitor.ingestion import LogsIngestionClient
from azure.identity import DefaultAzureCredential
import os
client = LogsIngestionClient(
endpoint=os.environ["AZURE_DCE_ENDPOINT"],
credential=DefaultAzureCredential()
)Use an authorized endpoint/DCR/stream and schema, with scoped identity permissions; DCE versus DCR ingestion endpoints depend on setup. The 2024 timestamps below are historical sample data, not a current live-ingestion probe. Use approved synthetic current timestamps for validation, redact PII/secrets before upload and confirm retention/charges. Close both client and credential, including failure paths; no logs should be uploaded simply to validate this guide.
Upload Custom Logs
from azure.monitor.ingestion import LogsIngestionClient
from azure.identity import DefaultAzureCredential
import os
client = LogsIngestionClient(
endpoint=os.environ["AZURE_DCE_ENDPOINT"],
credential=DefaultAzureCredential()
)
rule_id = os.environ["AZURE_DCR_RULE_ID"]
stream_name = os.environ["AZURE_DCR_STREAM_NAME"]
logs = [
{"TimeGenerated": "2024-01-15T10:00:00Z", "Computer": "server1", "Message": "Application started"},
{"TimeGenerated": "2024-01-15T10:01:00Z", "Computer": "server1", "Message": "Processing request"},
{"TimeGenerated": "2024-01-15T10:02:00Z", "Computer": "server2", "Message": "Connection established"}
]
client.upload(rule_id=rule_id, stream_name=stream_name, logs=logs)Upload from JSON File
import json
with open("logs.json", "r") as f:
logs = json.load(f)
client.upload(rule_id=rule_id, stream_name=stream_name, logs=logs)Custom Error Handling
Handle partial failures with a callback:
failed_logs = []
def on_error(error):
print(f"Upload failed: {error.error}")
failed_logs.extend(error.failed_logs)
client.upload(
rule_id=rule_id,
stream_name=stream_name,
logs=logs,
on_error=on_error
)
# Retry failed logs
if failed_logs:
print(f"Retrying {len(failed_logs)} failed logs...")
client.upload(rule_id=rule_id, stream_name=stream_name, logs=failed_logs)Incomplete uploads
An on_error callback can suppress the default exception. Persist a bounded, protected failure summary and report the upload as partial/failed; never silently discard failed logs or label the whole batch successful. Retry only classified transient failures with bounded backoff, reconciling ambiguous outcomes and duplicate-ingestion impact.
Async Client
import asyncio
from azure.monitor.ingestion.aio import LogsIngestionClient
from azure.identity.aio import DefaultAzureCredential
async def upload_logs():
async with LogsIngestionClient(
endpoint=endpoint,
credential=DefaultAzureCredential()
) as client:
await client.upload(
rule_id=rule_id,
stream_name=stream_name,
logs=logs
)
asyncio.run(upload_logs())Sovereign Clouds
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
from azure.monitor.ingestion import LogsIngestionClient
# Azure Government
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
client = LogsIngestionClient(
endpoint="https://example.ingest.monitor.azure.us",
credential=credential,
credential_scopes=["https://monitor.azure.us/.default"]
)Batching Behavior
The SDK automatically:
- Splits logs into chunks of 1MB or less
- Compresses each chunk with gzip
- Uploads chunks in parallel
SDK chunking does not bound the application's input memory or total ingestion cost. Limit input bytes/rows, confirm the installed version's batching/concurrency and handle partial success.
Client Types
| Client | Purpose |
|--------|---------|
| LogsIngestionClient | Sync client for uploading logs |
| LogsIngestionClient (aio) | Async client for uploading logs |
Key Concepts
| Concept | Description |
|---------|-------------|
| DCE | Data Collection Endpoint — ingestion URL |
| DCR | Data Collection Rule — defines schema, transformations, destination |
| Stream | Named data flow within a DCR |
| Custom Table | Target table in Log Analytics (ends with _CL) |
DCR Stream Name Format
Stream names follow patterns:
Custom-<TableName>_CL— For custom tablesMicrosoft-<TableName>— For built-in tables
Best Practices
1. Use DefaultAzureCredential for authentication
2. Handle errors gracefully — use on_error callback for partial failures
3. Include TimeGenerated — Required field for all logs
4. Match DCR schema — Log fields must match DCR column definitions
5. Use async client for high-throughput scenarios
6. Batch uploads — SDK handles batching, but send reasonable chunks
7. Monitor ingestion — Check Log Analytics for ingestion status
8. Use context manager — Ensures proper client cleanup
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-monitor-opentelemetry-exporter-java – Azure Monitor OpenTelemetry Exporter cho Java. Xuất traces, metrics và logs của…
- azure-monitor-opentelemetry-exporter-py – Azure Monitor OpenTelemetry Exporter cho Python. Dùng để xuất OpenTelemetry ở mức thấp…
- azure-monitor-opentelemetry-py – Azure Monitor OpenTelemetry Distro cho Python. Dùng để thiết lập Application Insights…
- azure-monitor-opentelemetry-ts – Tự động instrument ứng dụng Node.js với distributed tracing, metrics và logs.
- azure-monitor-query-java – SDK Azure Monitor Query cho Java. Thực thi truy vấn Kusto trên Log Analytics workspace…
- azure-monitor-query-py – SDK Azure Monitor Query cho Python. Dùng để truy vấn Log Analytics workspace và metric…
- 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.