Python 无代码检测

Python 中的自动检测使用一个可附加到任何 Python 应用程序的 Python 代理。该代理主要使用 猴子补丁(monkey patching)在运行时修改库函数,从而捕获许多流行库和框架的遥测数据。

设置

运行以下命令安装相应的包。

pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install

opentelemetry-distro 包安装了 API、SDK 以及 opentelemetry-bootstrapopentelemetry-instrument 工具。

opentelemetry-bootstrap -a install 命令会读取您活动 site-packages 文件夹中已安装的包列表,并安装相应库的检测库(如果适用)。例如,如果您已安装 flask 包,运行 opentelemetry-bootstrap -a install 将为您安装 opentelemetry-instrumentation-flask。OpenTelemetry Python 代理将在运行时使用猴子补丁修改这些库中的函数。

不带参数运行 opentelemetry-bootstrap 会列出建议安装的检测库。有关更多信息,请参阅 opentelemetry-bootstrap

配置代理

该代理高度可配置。

一种配置代理的方法是通过 CLI 中的配置属性

opentelemetry-instrument \
    --traces_exporter console,otlp \
    --metrics_exporter console \
    --service_name your-service-name \
    --exporter_otlp_endpoint 0.0.0.0:4317 \
    python myapp.py

或者,您可以使用环境变量来配置代理

OTEL_SERVICE_NAME=your-service-name \
OTEL_TRACES_EXPORTER=console,otlp \
OTEL_METRICS_EXPORTER=console \
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=0.0.0.0:4317
opentelemetry-instrument \
    python myapp.py

要查看完整的配置选项,请参阅 代理配置

支持的库和框架

许多流行的 Python 库都支持自动检测,包括 FlaskDjango。有关完整列表,请参阅 注册表

故障排除

有关通用故障排除步骤和特定问题的解决方案,请参阅 故障排除