Skip to content

Worktree prometheus monitoring - #1

Open
ArtLjn wants to merge 13 commits into
mainfrom
worktree-prometheus-monitoring
Open

ArtLjn wants to merge 13 commits into
mainfrom
worktree-prometheus-monitoring

Conversation

@ArtLjn

@ArtLjn ArtLjn commented May 15, 2026

Copy link
Copy Markdown
Owner

No description provided.

ArtLjn and others added 13 commits May 15, 2026 16:07
在 metrics.py 顶部新增 11 个 Prometheus 指标(Counter/Histogram/Gauge),
保留原有 MetricsCollector 完全不变,确保向后兼容。

新增指标:
- HTTP_REQUEST_DURATION / HTTP_REQUESTS_TOTAL
- AGENT_EXECUTION_TOTAL / AGENT_EXECUTION_DURATION
- LLM_CALLS_TOTAL / LLM_CALL_DURATION
- CACHE_QUERIES_TOTAL / CACHE_SIZE / CACHE_HIT_RATE
- SYSTEM_UPTIME_SECONDS / ACTIVE_REQUESTS

同时创建 tests/core/test_prometheus_metrics.py 验证 prometheus_client
基本行为,14 条测试全部通过。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- 新增 MetricsMiddleware,记录请求到 Prometheus Counter/Histogram
  - HTTP_REQUEST_DURATION 按 method/endpoint 分桶记录耗时
  - HTTP_REQUESTS_TOTAL 按 method/endpoint/status 计数
  - ACTIVE_REQUESTS Gauge 在请求开始 +1、结束 -1
  - 保留原有 metrics_collector.record_request() 向后兼容
- lifespan 中设置 SYSTEM_UPTIME_SECONDS 为启动时间戳
- 新增 /prometheus 端点返回 Prometheus 文本格式指标
- 原有 /metrics JSON 端点保持不变(向后兼容)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- classify/process/review 节点增加 AGENT_EXECUTION_TOTAL 和 AGENT_EXECUTION_DURATION 指标上报
- handle_failure 节点增加 error 计数
- 使用 time.perf_counter() 计时,try/except/else/finally 保证异常时也能记录

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- chat_completions_create 中跳过缓存和缓存未命中两种情况均增加 LLM_CALLS_TOTAL 和 LLM_CALL_DURATION 指标上报
- 缓存命中时不记录 LLM 调用指标(无实际 API 调用)
- 使用 time.perf_counter() 计时,finally 保证异常时也能记录

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- get 方法中命中/未命中分别增加 CACHE_QUERIES_TOTAL 计数,并更新 CACHE_HIT_RATE
- set 方法后更新 CACHE_SIZE
- clear 方法后重置 CACHE_SIZE 和 CACHE_HIT_RATE

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ArtLjn added a commit that referenced this pull request Jul 13, 2026
修复 v1.1 P0 #1:traces.total_tokens 永远为 0,token 控制台无数据源。

- 新增 models/token_stats.py:TokenDailyStatsORM 表
  字段:id/user_id(nullable)/date/model/call_type/ticket_id/prompt/completion/
  total/request_count/estimated_cost_cny
  唯一约束 (user_id, date, model, call_type);3 个索引
  call_type 6 枚举:intent/classify/process/review/coordinator/rag
- database.py 新增 accumulate_token_daily_stats + list_token_daily_stats
  用 SELECT-then-INSERT/UPDATE 模式跨方言兼容 NULL 累加(MySQL/SQLite 中
  NULL 在 UNIQUE 索引里被视为 distinct)
- TraceManager 新增 accumulate_token_daily_stats:通过 trace_id 反查 ticket_id
  + user_id,调用方只需传 trace_id + model + call_type + token 用量
- CachedLLMClient._finalize_llm_span 在已有 add_token_usage 调用后追加
  accumulate_token_daily_stats;task_type 映射到 call_type 枚举
- 新增 tests/core/test_token_accumulation.py:验证 INSERT/UPDATE 累加、
  user_id=None 独立行、6 个 call_type 分行、traces.total_tokens 修复后非 0
  (7 个测试)

完成 C2 任务块 3(Token 累加修复)。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant