阿里云Python探针自带插件支持对常见的Python框架进行自动埋点,但如果您想要收集更多的数据信息,可以自定义扩展埋点插件。本文介绍如何编写埋点插件并安装。
前提条件
Python 探针版本大于等于1.7.0。
操作步骤
1. (可选)卸载存量ARMS插件
如果您不想使用ARMS自带的插件而期望使用自己编写的插件对框架进行自定义埋点,可以先卸载ARMS的对应插件。
-
通过以下命令查看已经安装的ARMS相关插件。
pip3 list | grep aliyunPackage Version ---------------------------------------------------- ----------- aliyun-bootstrap 1.0.4 aliyun-instrumentation-dashscope 1.0.0 aliyun-instrumentation-dify 1.1.0 aliyun-instrumentation-langchain 1.1.0 aliyun-instrumentation-llama-index 1.0.3.dev0 aliyun-instrumentation-mcp 1.0.0 aliyun-instrumentation-openai 1.0.1 aliyun-instrumentation-sglang 0.1.0 aliyun-instrumentation-vllm 0.1.0 aliyun-opentelemetry-exporter-otlp-proto-common 1.25.0 aliyun-opentelemetry-exporter-otlp-proto-http 1.25.0 aliyun-opentelemetry-instrumentation 0.46b0.dev0 aliyun-opentelemetry-instrumentation-aiohttp-client 0.46b0.dev0 aliyun-opentelemetry-instrumentation-asgi 0.46b0.dev0 aliyun-opentelemetry-instrumentation-django 0.46b0.dev0 aliyun-opentelemetry-instrumentation-fastapi 0.46b0.dev0 aliyun-opentelemetry-instrumentation-flask 0.46b0.dev0 aliyun-opentelemetry-instrumentation-httpx 0.46b0.dev0 aliyun-opentelemetry-instrumentation-logging 0.46b0.dev0 aliyun-opentelemetry-instrumentation-redis 0.46b0.dev0 aliyun-opentelemetry-instrumentation-requests 0.46b0.dev0 aliyun-opentelemetry-instrumentation-sqlalchemy 0.46b0.dev0 aliyun-opentelemetry-instrumentation-wsgi 0.46b0.dev0 aliyun-opentelemetry-proto 1.25.0 aliyun-opentelemetry-sdk 1.25.0 aliyun-opentelemetry-semantic-conventions 0.46b0.dev0 aliyun-opentelemetry-util-http 0.46b0.dev0 aliyun-sdk-extension-arms 1.0.0 aliyun-semantic-conventions 1.2.0 -
卸载指定插件。
pip3 uninstall ${插件名称} # 示例:pip3 uninstall aliyun-instrumentation-dify
2. 安装自定义扩展
-
以开源侧的插件为例,选择您需要的插件并安装。
pip3 install ${插件名称} # 示例:pip3 install pip3 install opentelemetry-instrumentation-pymongo==0.51b0开源侧的 OpenTelemetry Python 插件可在 GitHub 仓库
open-telemetry/opentelemetry-python-contrib的 instrumentation 目录下浏览,其中包含opentelemetry-instrumentation-aiohttp-server、opentelemetry-instrumentation-django、opentelemetry-instrumentation-celery、opentelemetry-instrumentation-cassandra等大量框架对应的 instrumentation 子包,可根据业务依赖按需选择。您也可以参考以下示例自行编写埋点插件。
-
参考容器服务 ACK 和容器计算服务 ACS 通过 ack-onepilot 组件安装 Python 探针 或 手动安装Python探针 文档安装Python探针。
-
通过以下命令查看aliyun-opentelemetry-instrumentation包是否被正确安装。
pip3 list | grep aliyun-opentelemetry-instrumentation若安装成功,预期输出中应包含
aliyun-opentelemetry-instrumentation主包及各框架插桩包,示例输出:aliyun-opentelemetry-instrumentation 0.46b0.dev0 aliyun-opentelemetry-instrumentation-aiohttp-client 0.46b0.dev0 aliyun-opentelemetry-instrumentation-asgi 0.46b0.dev0 aliyun-opentelemetry-instrumentation-django 0.46b0.dev0 aliyun-opentelemetry-instrumentation-fastapi 0.46b0.dev0 aliyun-opentelemetry-instrumentation-flask 0.46b0.dev0 aliyun-opentelemetry-instrumentation-httpx 0.46b0.dev0 aliyun-opentelemetry-instrumentation-logging 0.46b0.dev0 aliyun-opentelemetry-instrumentation-redis 0.46b0.dev0 aliyun-opentelemetry-instrumentation-requests 0.46b0.dev0 aliyun-opentelemetry-instrumentation-sqlalchemy 0.46b0.dev0 aliyun-opentelemetry-instrumentation-wsgi 0.46b0.dev0
效果展示
ARMS自研探针产生的Span和自定义扩展产生的Span可以正常串联:
在 Trace详情 页面的 Span 树中,可以看到根 Span mongo/(SERVER GET 200,耗时 11.38s)与自定义扩展产生的子 Span(demo_db.insert insert、demo_db.find find、demo_db.aggregate aggregate、admin.endSessions endSessions)正常串联。右侧 Span详情 的 附加信息 中展示了 MongoDB 属性,包括 db.mongodb.collection=demo_collection、db.name=demo_db、db.statement=insert、db.system=mongodb 等。