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_monitorconfigure_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_monitorconfigure_azure_monitor()
# Django settings...
FastAPI集成
from fastapi import FastAPI
from azure.monitor.opentelemetry import configure_azure_monitorconfigure_azure_monitor()
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
自定义跟踪
from opentelemetry import trace
from azure.monitor.opentelemetry import configure_azure_monitorconfigure_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_monitorconfigure_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_monitorconfigure_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_NAMEconfigure_azure_monitor(
resource=Resource.create({SERVICE_NAME: "my-service-name"})
)
Azure AD认证
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.identity import DefaultAzureCredentialconfigure_azure_monitor(
credential=DefaultAzureCredential()
)
自动检测库
| 库 | 遥测类型 |
|---|---|
| Flask | 跟踪 |
| Django | 跟踪 |
| FastAPI | 跟踪 |
| Requests | 跟踪 |
| urllib3 | 跟踪 |
| httpx | 跟踪 |
| aiohttp | 跟踪 |
| psycopg2 | 跟踪 |
| pymysql | 跟踪 |
| pymongo | 跟踪 |
| redis | 跟踪 |
配置选项
| 参数 | 描述 | 默认值 |
|---|---|---|
| connection_string | Application Insights连接字符串 | 从环境变量 |
| credential | Azure AD认证凭据 | None |
| sampling_ratio | 采样率(0.0到1.0) | 1.0 |
| resource | OpenTelemetry Resource | 自动检测 |
| instrumentations | 要启用的检测列表 | 全部 |
| enable_live_metrics | 启用实时指标流 | False |
最佳实践
- 尽早调用configure_azure_monitor()- 在导入检测库之前
- 使用环境变量- 在生产环境中使用环境变量配置连接字符串
- 设置云角色名称- 为多服务应用程序设置云角色名称
- 启用采样- 在高流量应用程序中启用采样
- 使用结构化日志- 更好的日志分析查询
- 添加自定义属性- 为Span添加自定义属性以便更好地调试
- 使用AAD认证- 为生产工作负载使用AAD认证