☰
【计算机工具类-监控和日志Skills】azure-monitor-opentelemetry-py 技能
2026/10/12 5:33:52 网站建设 项目流程

Azure Monitor OpenTelemetry Distro for Python. Use for one-line Application Insights setup with auto-instrumentation.

技能概述

azure-monitor-opentelemetry-py 技能用于通过一行代码设置Application Insights,并提供OpenTelemetry自动检测功能。支持Flask、Django、FastAPI等主流Python框架的自动检测,无需修改应用程序代码即可实现分布式跟踪、指标收集和日志记录,为Python应用程序提供全面的可观测性解决方案。

下载地址:agentic-awesome-skills/skills/azure-monitor-opentelemetry-py at main · sickn33/agentic-awesome-skills · GitHub

主要功能

  • 一行代码设置:快速配置Application Insights
  • 自动检测:自动检测主流框架和库
  • 分布式跟踪:自动收集请求跟踪数据
  • 指标收集:支持自定义指标和计数器
  • 日志记录:自动将Python日志发送到Application Insights
  • 实时指标:支持实时指标流
  • Azure AD认证:支持Azure Active Directory身份验证

安装

pip install azure-monitor-opentelemetry

环境变量

APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/

快速开始

from azure.monitor.opentelemetry import configure_azure_monitor

# One-line setup - reads connection string from environment
configure_azure_monitor()

# Your application code...

框架集成

Flask集成

from flask import Flask
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

app = Flask(__name__)

@app.route("/")
def hello():
return "Hello, World!"

if __name__ == "__main__":
app.run()

Django集成

# settings.py
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

# Django settings...

FastAPI集成

from fastapi import FastAPI
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

app = FastAPI()

@app.get("/")
async def root():
return {"message": "Hello World"}

自定义跟踪

from opentelemetry import trace
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("my-operation") as span:
span.set_attribute("custom.attribute", "value")
# Do work...

自定义指标

from opentelemetry import metrics
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

meter = metrics.get_meter(__name__)

counter = meter.create_counter("my_counter")
counter.add(1, {"dimension": "value"})

自定义日志

import logging
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

logger.info("This will appear in Application Insights")
logger.error("Errors are captured too", exc_info=True)

采样配置

from azure.monitor.opentelemetry import configure_azure_monitor

# Sample 10% of requests
configure_azure_monitor(
sampling_ratio=0.1
)

云角色名称

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME

configure_azure_monitor(
resource=Resource.create({SERVICE_NAME: "my-service-name"})
)

Azure AD认证

from azure.monitor.opentelemetry import configure_azure_monitor
from azure.identity import DefaultAzureCredential

configure_azure_monitor(
credential=DefaultAzureCredential()
)

自动检测库

库遥测类型
Flask跟踪
Django跟踪
FastAPI跟踪
Requests跟踪
urllib3跟踪
httpx跟踪
aiohttp跟踪
psycopg2跟踪
pymysql跟踪
pymongo跟踪
redis跟踪

配置选项

参数描述默认值
connection_stringApplication Insights连接字符串从环境变量
credentialAzure AD认证凭据None
sampling_ratio采样率(0.0到1.0)1.0
resourceOpenTelemetry Resource自动检测
instrumentations要启用的检测列表全部
enable_live_metrics启用实时指标流False

最佳实践

  1. 尽早调用configure_azure_monitor()- 在导入检测库之前
  2. 使用环境变量- 在生产环境中使用环境变量配置连接字符串
  3. 设置云角色名称- 为多服务应用程序设置云角色名称
  4. 启用采样- 在高流量应用程序中启用采样
  5. 使用结构化日志- 更好的日志分析查询
  6. 添加自定义属性- 为Span添加自定义属性以便更好地调试
  7. 使用AAD认证- 为生产工作负载使用AAD认证

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询