自定义扩展Python探针

更新时间:
复制 MD 格式

阿里云Python探针自带插件支持对常见的Python框架进行自动埋点,但如果您想要收集更多的数据信息,可以自定义扩展埋点插件。本文介绍如何编写埋点插件并安装。

前提条件

Python 探针版本大于等于1.7.0。

操作步骤

1. (可选)卸载存量ARMS插件

如果您不想使用ARMS自带的插件而期望使用自己编写的插件对框架进行自定义埋点,可以先卸载ARMS的对应插件。

  1. 通过以下命令查看已经安装的ARMS相关插件。

    pip3 list | grep aliyun
    Package                                              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
  2. 卸载指定插件。

    pip3 uninstall ${插件名称}
    # 示例:pip3 uninstall aliyun-instrumentation-dify

2. 安装自定义扩展

  1. 以开源侧的插件为例,选择您需要的插件并安装。

    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 子包,可根据业务依赖按需选择。

    您也可以参考以下示例自行编写埋点插件。

    展开查看示例代码

    可以通过继承 BaseInstrumentor 类,编写 Instrumentor 以实现埋点插桩逻辑。

    假设应用使用Dashscope SDK进行大模型调用,可以参考如下示例代码对Dashscope库中BaseApi类的call方法进行埋点插桩:

    from typing import Any, Collection
    from aliyun.instrumentation.dashscope._wrapper import DashscopeRequestWrapper
    from aliyun.instrumentation.dashscope.package import _instruments
    from aliyun.instrumentation.dashscope.version import __version__
    from aliyun.semconv.logger import getLogger
    from wrapt import wrap_function_wrapper
    from opentelemetry import trace as trace_api
    from opentelemetry.instrumentation.instrumentor import BaseInstrumentor  # type: ignore
    logger = getLogger(__name__)
    _MODULE = "dashscope.client.base_api"
    __all__ = ["AliyunDashScopeInstrumentor"]
    class AliyunDashScopeInstrumentor(BaseInstrumentor):  # type: ignore
        __slots__ = (
            "_original_call",
        )
        # 该函数定义了应该对哪个依赖里的代码进行插桩
        # 使用wrapt对目标函数进行插桩增强
        def instrumentation_dependencies(self) -> Collection[str]:
            return ("dashscope >= 1.0.0",)
        # 在_instrument函数中执行实际的插桩逻辑
        def _instrument(self, **kwargs: Any) -> None:
            if not (tracer_provider := kwargs.get("tracer_provider")):
                tracer_provider = trace_api.get_tracer_provider()
            tracer = trace_api.get_tracer(__name__, __version__, tracer_provider)
            wrap_function_wrapper(
                module=_MODULE,
                name="BaseApi.call",
                wrapper=DashscopeRequestWrapper(tracer=tracer),
            )
        # 该函数定义了取消插桩时应该执行的动作,如无需定义请忽略
        def _uninstrument(self, **kwargs: Any) -> None:
            pass
                  
  2. 参考容器服务 ACK 和容器计算服务 ACS 通过 ack-onepilot 组件安装 Python 探针 或 手动安装Python探针 文档安装Python探针。

  3. 通过以下命令查看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 等。