Skip to content

[Priority: Med] [chore] 修复 Metrics 模块生产与消费逻辑不匹配问题 #279

@liujuanjuan1984

Description

@liujuanjuan1984

🔍 发现的问题 / 原始需求描述

当前 src/codex_a2a_server/metrics.py 模块存在完整的指标埋点逻辑(生产方),业务代码如 request_handler.pystream_processor.py 也在积极调用 inc_counter/inc_gauge 记录数据。然而,生产代码中缺失对应的“消费方”:

  1. snapshot_metrics()reset_metrics() 函数目前仅在单元测试中被调用。
  2. FastAPI 应用中未挂载 /metrics 或类似的监控接口,导致运行时的内存指标无法被外部运维系统(如 Prometheus)或开发者感知。

🛠️ 详细实施方案

涉及文件:src/codex_a2a_server/server/application.py, src/codex_a2a_server/metrics.py

方案建议:

  1. 暴露监控接口:在 create_app 函数中增加一个内部路由(受配置开关控制,例如 a2a_enable_metrics_endpoint),调用 snapshot_metrics() 并返回 JSON 格式的指标快照。
  2. 清理或移动:若短期内无监控需求,应将 snapshot_metricsreset_metrics 标记为仅限测试使用,或移动到测试支持目录中,以消除静态扫描中的“死代码”警告。
  3. 完善文档:在 README.md 或相关运维文档中说明如何获取服务运行指标。

核心逻辑示例:

# src/codex_a2a_server/server/application.py
if settings.a2a_enable_metrics_endpoint:
    @app.get("/metrics")
    async def get_metrics():
        return snapshot_metrics()

🧪 回归测试建议

  1. 运行 pytest tests/execution/test_metrics.py 确保现有断言依然通过。
  2. 启动服务后,手动请求 GET /metrics(若已实现),验证返回的 countersgauges 是否随业务操作(如发起流请求)实时更新。

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:todoPlanned but not started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions