dify-plugin-daemon接入监控

更新时间:
复制 MD 格式

dify-plugin-daemon 是 Dify 非常重要的组件,负责插件的生命周期管理、插件的请求转发等,它的稳定性直接决定 Dify 的插件运行的状态。接下来介绍如何将 dify-plugin-daemon 接入可观测。

操作步骤

一、编译镜像

修改 Dockerfile 文件重新编译对应镜像,local.dockerfile 修改示例:

FROM golang:1.23-alpine AS builder
ARG VERSION=unknown
# copy project
COPY . /app
# set working directory
WORKDIR /app
# using goproxy if you have network issues
# ENV GOPROXY=https://goproxy.cn,direct
# download arms instgo
RUN wget "http://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/instgo/instgo-linux-amd64" -O instgo
RUN chmod 777 instgo
# instgo build
RUN INSTGO_EXTRA_RULES="dify_python" ./instgo go build \
    -ldflags "\
    -X 'github.com/langgenius/dify-plugin-daemon/internal/manifest.VersionX=${VERSION}' \
    -X 'github.com/langgenius/dify-plugin-daemon/internal/manifest.BuildTimeX=$(date -u +%Y-%m-%dT%H:%M:%S%z)'" \
    -o /app/main cmd/server/main.go
# copy entrypoint.sh
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
FROM ubuntu:24.04
WORKDIR /app
# check build args
ARG PLATFORM=local
# Install python3.12 if PLATFORM is local
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl python3.12 python3.12-venv python3.12-dev python3-pip ffmpeg build-essential \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1;
# preload tiktoken
ENV TIKTOKEN_CACHE_DIR=/app/.tiktoken
# Install dify_plugin to speedup the environment setup, test uv and preload tiktoken
RUN mv /usr/lib/python3.12/EXTERNALLY-MANAGED /usr/lib/python3.12/EXTERNALLY-MANAGED.bk \
    && python3 -m pip install uv \
    && uv pip install --system dify_plugin \
    && python3 -c "from uv._find_uv import find_uv_bin;print(find_uv_bin());" \
    && python3 -c "import tiktoken; encodings = ['o200k_base', 'cl100k_base', 'p50k_base', 'r50k_base', 'p50k_edit', 'gpt2']; [tiktoken.get_encoding(encoding).special_tokens_set for encoding in encodings]"
ENV UV_PATH=/usr/local/bin/uv
ENV PLATFORM=$PLATFORM
ENV GIN_MODE=release
COPY --from=builder /app/main /app/entrypoint.sh /app/
# run the server, using sh as the entrypoint to avoid process being the root process
# and using bash to recycle resources
CMD ["/bin/bash", "-c", "/app/entrypoint.sh"]

说明

注意 INSTGO_EXTRA_RULES 选项会开启 Plugin 运行时自动监测功能,如果不需要在 Plugin-Daemon 启动时拉起对 Plugin 探针,可以去除编译文件中的 INSTGO_EXTRA_RULES="dify_python"

只需要在编译 dify-plugin-daemon 的时候,添加以下步骤:

# download arms instgo
RUN wget "http://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/instgo/instgo-linux-amd64" -O instgo
RUN chmod 777 instgo

go build 前添加:

INSTGO_EXTRA_RULES="dify_python" ./instgo

基于开源版本和上述Dockerfile修改编译的0.5.3的镜像,可供使用。

registry.cn-hangzhou.aliyuncs.com/private-mesh/hellob:dify-plugin-daemon-0.5.3-local

二、接入可观测

编译完成后,需要在运行时配置 ARMS 相关的环境变量,以便探针能正确上报监控数据到 ARMS 控制台。

1. 获取 License Key

您可以在列出LicenseKey文档页面单击调试,选择地域后单击发起调用即可获取LicenseKey。

2. 配置环境变量

在启动 dify-plugin-daemon 容器时,需要配置以下环境变量:

环境变量

说明

是否必填

示例值

ARMS_ENABLE

是否启用 ARMS 探针

true

ARMS_APP_NAME

应用名称,在 ARMS 控制台中展示

dify-plugin-daemon

ARMS_REGION_ID

接入的地域 ID

cn-hangzhou

ARMS_LICENSE_KEY

ARMS License Key

your-license-key

ARMS_WORKSPACE

ARMS 工作空间 ID(云监控 2.0 场景)

your-workspace-id

3. Docker Compose 配置示例

docker-compose.yaml 中为 plugin_daemon 服务添加环境变量配置:

services:
  plugin_daemon:
    image: your-registry/dify-plugin-daemon:latest  # 使用第一步编译的镜像
    environment:
      ARMS_ENABLE: "true"
      ARMS_APP_NAME: "dify-plugin-daemon"
      ARMS_REGION_ID: "cn-hangzhou"
      ARMS_LICENSE_KEY: "${ARMS_LICENSE_KEY}"
      # 如果使用云监控 2.0,需要配置 ARMS_WORKSPACE
      # ARMS_WORKSPACE: "${ARMS_WORKSPACE}"
说明:建议将 ARMS_LICENSE_KEY 等敏感信息通过 .env 文件或密钥管理服务注入,避免硬编码在配置文件中。

4. 验证接入

启动容器后,可通过以下方式验证探针是否正常工作:

  1. 查看容器日志,确认无 ARMS 相关的报错信息:

    docker logs plugin_daemon 2>&1 | grep -i arms
  2. 约 1 分钟后登录 ARMS 控制台,在 应用监控 > 应用列表 中查看是否出现名为 dify-plugin-daemon 的应用。

三、查看监控

接入成功后,可在 ARMS 控制台查看 dify-plugin-daemon 的各项监控数据。

1. 应用总览

登录 ARMS 控制台,在左侧导航栏选择 应用监控 > 应用列表,点击 dify-plugin-daemon 进入应用总览页面,可查看:

  • 应用的总请求量、平均响应时间、错误数等核心指标。

  • 应用实例的系统监控信息(CPU、内存等)。image.png

2. 应用拓扑

在应用详情页面点击 应用拓扑 标签,可以查看:

  • dify-plugin-daemon 与上下游服务(如 Dify API、数据库、Redis 等)的调用关系拓扑图。

  • 各链路的请求量、响应时间和错误率。

3. 接口调用

在应用详情页面点击 接口调用 标签,可以查看:

  • 各 API 接口的调用量、响应时间、错误率。

  • 支持按接口名称搜索和筛选,快速定位慢接口或异常接口。

4. 链路追踪

在应用详情页面点击 调用链路查询,可以:

  • 按 TraceId、接口名称、响应时间等条件筛选调用链。

  • 查看完整的调用链路详情,包含每个 Span 的耗时和状态。

  • 对慢请求或异常请求进行根因分析。链路详情页顶部展示调用链统计信息(应用数、接口数、异常数),中部以瀑布图形式展示各 Span 的层级调用关系及耗时,单击某个 Span 后右侧面板显示其详细属性,包括应用名接口名IP开始/结束时间spanId状态码以及 Attributes 中的 HTTP 方法、状态码、URL 等信息。

5. Plugin 监控(可选)

如果在编译时启用了 INSTGO_EXTRA_RULES="dify_python" 选项,ARMS 会同时监控由 dify-plugin-daemon 管理的 Python Plugin 进程。可以在 ARMS 控制台的应用列表中查看各 Plugin 的监控数据,包括:

  • Plugin 的请求量和响应时间。

  • Plugin 的错误率和异常信息。

  • Plugin 与 dify-plugin-daemon 之间的调用链路。