diff --git a/docs/LOGBOOK.md b/docs/LOGBOOK.md index 6c06fc73..8de489ba 100644 --- a/docs/LOGBOOK.md +++ b/docs/LOGBOOK.md @@ -1,3 +1,38 @@ +## 2026-06-11|P0 Telegram 監控告警主鏈路修復 + +**背景**:使用者指出 Telegram 監控告警已異常很久、等同沒有任何告警訊息。即時盤點 production 後確認:Telegram Bot token / chat id 仍設定完成,CI/CD Telegram outbound 仍可送出;真正斷點在一般 Alertmanager webhook 進 API 後缺少 `project_id` tenant context,導致 approval / incident 路徑被 RLS fail-closed 擋下,API 又以 degraded accepted no-retry 吃掉告警,Alertmanager 不會重送,Telegram 因此沉默。 + +**根因證據:** +- Production API log 曾出現 `alertmanager_degraded_accepted_no_retry`,錯誤為 `401: Missing tenant context: project_id is required`。 +- 同期 `/api/v1/platform/events/recent?project_id=awoooi&provider_prefix=alertmanager` 可看到 `BackupAggregateRunFailed`、`DockerContainerUnhealthy`、`HostDiskUsageHigh`、`ColdStartHost120Unreachable` 等 Alertmanager 事件進站,代表 Alertmanager 有送到 API,不是 Alertmanager 或 TG token 完全中斷。 +- `/api/v1/telegram/health` 顯示 `status=configured`、`bot_token_set=true`、`chat_id_set=true`、`sre_group_chat_id_set=true`;CI/CD outbound 在修復前後也能送出,排除「整個 Telegram Bot 無法送訊息」。 + +**完成內容:** +- 新增 `apps/api/src/main.py` 的 `_resolve_request_project_context()`,將 request context 解析集中化。 +- 只針對 `/api/v1/webhooks/alertmanager` 在沒有 `X-Project-ID` / `X-Tenant-ID` / `project_id` query 時補上 `project_id=awoooi` 與 `source=request.alertmanager.default_project`。 +- 保留其他 API 的 fail-closed 行為;`/api/v1/security/db-context-guard` 等非 Alertmanager 路由缺 project_id 仍維持 `request.project_id.missing`。 +- 新增 `apps/api/tests/test_alertmanager_project_context.py`,覆蓋 Alertmanager 預設 project、explicit header 優先、非 Alertmanager route 仍 fail-closed。 + +**驗證與正式站證據:** +- 本地:`DATABASE_URL='postgresql+asyncpg://test:test@localhost/test' pytest apps/api/tests/test_alertmanager_project_context.py apps/api/tests/test_db_context_guard.py -q` 通過,`7 passed`。 +- 本地:`python3 -m py_compile apps/api/src/main.py apps/api/src/api/v1/webhooks.py apps/api/src/db/base.py` 通過。 +- 本地:`git diff --check` 通過。 +- Code commit:`6bae94fa fix(api): restore Alertmanager project context`。 +- Deploy marker:`edbb1194 chore(cd): deploy 6bae94f [skip ci]`;後續最新 production marker 為 `aa79b3dc chore(cd): deploy 8c11af7 [skip ci]`,該 tree 仍包含 `6bae94fa` 修復。 +- Production image 已更新:`192.168.0.110:5000/awoooi/api:8c11af7c19f95c8688a2ff1f48dd925f1343f72b`,`awoooi-api` rollout 成功,兩個 pod ready。 +- Production health:`/api/v1/health` 回傳 `healthy`、`environment=prod`、`mock_mode=false`。 +- 非 CI/CD smoke:從 API pod 內送 `AlertChainSmokeTest`,`auto_repair=false`,HTTP 200,`alert_id=alert-20260611115346`。 +- Smoke 後續完成:建立 `INC-20260611-8D5CB4`、approval `d8a0a8db-6425-42b8-8af7-fff98c1d4570`,TG approval card 送達 `target_chat_id=-1003711974679`,Telegram `message_id=21867`,log 出現 `telegram_approval_card_sent` 與 `telegram_push_success`。 +- 最新 image 上的真實 Alertmanager 告警也已使用 `project_context_source=request.alertmanager.default_project`,並且 CI/CD `CI_build_and_deploy_success` 已 `telegram_cicd_progress_sent`;heartbeat 也出現 `telegram_heartbeat_sent`。 + +**完成度與邊界:** +- Alertmanager → API tenant context 主斷點:`100%`。 +- Alertmanager → DB event mirror:`100%`,smoke 與真實告警皆已記錄 received / converged / incident linked 等 stage。 +- Alertmanager → Telegram outbound 主鏈路:`100%`,已用非 CI/CD smoke 與 CI/CD 告警雙重驗證。 +- TG 監控治理長期項:`75%`;仍需後續處理非 Alertmanager route 的 project context warning、OpenClaw / Ollama 分析 timeout 降噪、以及告警沉默自我告警可視化。 +- 本階段沒有呼叫 `logOut`、沒有讀取或輸出 Telegram token / Secret payload、沒有 SSH、沒有 active scan、沒有修改 Alertmanager rule / receiver、沒有手動重啟服務、沒有執行 auto repair;smoke 明確設 `auto_repair=false`,只驗證告警送達。 +- IwoooS 整體仍維持 `64%`;active runtime gate 仍為 `0`;owner response received / accepted 仍為 `0 / false`。 + ## 2026-06-11|IwoooS 高價值配置 Owner Packet 草案 **背景**:P0.2 已建立高價值配置變更 Gate,但若只有分類結果,owner 仍需要人工整理欄位。本階段把分類結果轉成 owner response packet 草案,並對齊 S4.9 canonical owner response envelope,避免高價值配置流程和 S4.9 欄位分裂。