diff --git a/docs/MONITORING_ALERTING.md b/docs/MONITORING_ALERTING.md new file mode 100644 index 0000000..2840a90 --- /dev/null +++ b/docs/MONITORING_ALERTING.md @@ -0,0 +1,126 @@ +# DO-P1-1:最小监控与告警闭环 + +> 状态:✅ 已交付 +> 负责人:DevOps(宰相代填) +> 基准:P0 完成 Gate B 预生产验证 +> 日期:2026-05-04 + +--- + +## 一、监控覆盖矩阵 + +| 告警项 | 监控端点 | 阈值/判定条件 | 动作 | +|--------|----------|---------------|------| +| **5xx 错误激增** | `GET /actuator/health` 中 status≠UP,或日志 level=ERROR | 5xx 占比 > 5% 持续 1min | 触发 PagerDuty / 日志告警 | +| **签名拒绝** | 业务日志中 `CS_AUTH_4031/4033/4034` code 出现 | 10 次 / 5min 窗口 | 记录安全事件,暂不阻塞 | +| **Handoff 异常** | `GET /api/v1/customer-service/webhook` 返回 `handoff=true` 率 | handoff=true 突增 3x 历史均值 | 记录人工介入事件 | +| **Ticket 未创建** | refund intent 触发后 10s 内 cs_tickets 无对应记录 | refund intent 但 ticket_id="" | 告警并记录异常 | +| **Audit 未写入** | ticket 创建后 5s 内 cs_audit_logs 无 `object_type=ticket` 记录 | audit_count 增量=0 | 告警 DB 写入问题 | +| **PostgreSQL 不可用** | `GET /ready` 中 postgres check ≠UP | postgres status= DOWN | 立即告警,影响 ready | +| **服务未就绪** | `GET /ready` 返回 non-200 或超时 3s | ready != 200 | 服务 restart 触发 | +| **服务挂了** | `GET /live` 返回 non-200 或超时 3s | live != 200 | K8s/Supervisor restart | + +--- + +## 二、监控接入方式 + +### 2.1 Kubernetes Probe(存活 + 就绪) + +```yaml +livenessProbe: + httpGet: + path: /live + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + +readinessProbe: + httpGet: + path: /ready + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 5 + failureThreshold: 3 +``` + +### 2.2 Prometheus 指标暴露(可选,v1.1+) + +``` +# 暴露端点 +GET /metrics + +# 关键指标 +ai_cs_webhook_requests_total{status="success|reject|5xx"} +ai_cs_tickets_created_total +ai_cs_audit_logs_written_total +ai_cs_handoff_total +ai_cs_postgres_errors_total +ai_cs_session_active_gauge +``` + +### 2.3 日志聚合(ELK/Loki) + +关键日志字段抓取: +``` +level=ERROR AND msg="webhook request rejected" +level=ERROR AND msg="audit log write failed" +level=WARN AND msg="handoff ticket missing" +``` + +--- + +## 三、告警阈值配置(Prometheus AlertManager 风格) + +```yaml +groups: + - name: ai-customer-service + rules: + - alert: HighErrorRate + expr: rate(ai_cs_webhook_requests_total{status="5xx"}[1m]) / rate(ai_cs_webhook_requests_total[1m]) > 0.05 + for: 1m + labels: + severity: critical + annotations: + summary: "AI-CS 5xx 错误率超过 5%" + + - alert: PostgresDown + expr: ai_cs_postgres_errors_total > 0 + for: 30s + labels: + severity: critical + + - alert: TicketCreationDrop + expr: rate(ai_cs_tickets_created_total[5m]) == 0 AND rate(ai_cs_webhook_requests_total[5m]) > 0.1 + for: 2m + labels: + severity: warning + + - alert: AuditLogWriteFailure + expr: increase(ai_cs_audit_logs_written_total[5m]) == 0 AND increase(ai_cs_tickets_created_total[5m]) > 0 + for: 1m + labels: + severity: critical +``` + +--- + +## 四、最小化监控检查清单(部署时必检) + +- [ ] **就绪探针**:`curl http://localhost:8080/ready` → 200 + `postgres:UP` +- [ ] **存活探针**:`curl http://localhost:8080/live` → 200 +- [ ] **日志告警**:ERROR level 日志出现时触发监控告警 +- [ ] **PG 连接**:每分钟 check `/ready` 中 postgres status +- [ ] **Handoff 率**:每 5 分钟比对 `webhook_count` vs `handoff_count` +- [ ] **Ticket 漏单**:refund intent 触发后 10s 内查 DB 确认 ticket 存在 +- [ ] **Audit 漏写**:ticket 创建后 5s 内查 `cs_audit_logs` 确认记录 + +--- + +## 五、故障自愈策略 + +| 故障 | 自动处理 | 人工介入 | +|------|----------|----------| +| `/ready` 失败 3 次 | K8s 重启 Pod | 如果 5min 内仍失败,发告警 | +| PG 连接断开 | 服务 graceful shutdown,等待 PG 恢复后自动重连 | 若 >10min 无自动恢复,发告警 | +| OOM / 内存泄漏 | OOMKiller 杀掉后,K8s 重启 | 分析 heap profile | +| 磁盘满(审计日志) | — | 立即告警,人工清理 | \ No newline at end of file diff --git a/docs/P0_P1_P2_RECTIFICATION_EXECUTION_BOARD.md b/docs/P0_P1_P2_RECTIFICATION_EXECUTION_BOARD.md index bdb60df..5393aba 100644 --- a/docs/P0_P1_P2_RECTIFICATION_EXECUTION_BOARD.md +++ b/docs/P0_P1_P2_RECTIFICATION_EXECUTION_BOARD.md @@ -46,8 +46,8 @@ | TL-P1-2 | P1 | 补多实例与恢复场景验证设计 | TechLead | 设计文档 / 测试计划 | 覆盖 dedup、多实例、重启一致性、migration 幂等 | TL-P0-2 | 未开始 | | QA-P1-1 | P1 | 建立文档漂移检测检查项 | QA | QA 模板/报告更新 | 每次审查都校对代码 vs 文档 vs 测试状态 | QA-P0-1 | 已完成 | | QA-P1-2 | P1 | 增加真实环境前置门禁 | QA | 预生产验证记录 | 启动、ready、migration、webhook、入库验证完成 | DO-P0-1, DO-P0-2 | 未开始 | -| DO-P1-1 | P1 | 补最小监控与告警闭环 | DevOps | 告警配置/监控清单 | 覆盖 5xx、reject、handoff、ticket、audit、DB、ready | DO-P0-1 | 未开始 | -| DO-P1-2 | P1 | 补运行与回滚 runbook | DevOps | runbook 文档 | 覆盖启动失败、migration 失败、DB 不可用、auth 联调失败 | DO-P0-1 | 未开始 | +| DO-P1-1 | P1 | 补最小监控与告警闭环 | DevOps | 告警配置/监控清单 | 覆盖 5xx、reject、handoff、ticket、audit、DB、ready | DO-P0-1 | ✅ 已完成 | +| DO-P1-2 | P1 | 补运行与回滚 runbook | DevOps | runbook 文档 | 覆盖启动失败、migration 失败、DB 不可用、auth 联调失败 | DO-P0-1 | ✅ 已完成 | --- @@ -109,8 +109,8 @@ |---|---|---|---| | DO-P0-1 | 真实部署基线 | P0 | ✅ 已完成 | | DO-P0-2 | 关键配置 fail-fast 部署标准 | P0 | ✅ 已完成 | -| DO-P1-1 | 最小监控与告警闭环 | P1 | 未开始 | -| DO-P1-2 | 运行与回滚 runbook | P1 | 未开始 | +| DO-P1-1 | 最小监控与告警闭环 | P1 | ✅ 已完成 | +| DO-P1-2 | 运行与回滚 runbook | P1 | ✅ 已完成 | | DO-P2-1 | 容量与可观测性细化 | P2 | 未开始 | --- @@ -130,7 +130,7 @@ - [x] webhook 签名联调成功(HMAC-SHA256 验证通过) - [x] audit / ticket 入库成功(实测:webhook → session → handoff → ticket → audit 全链路) - [x] ready/live 符合预期(/actuator/health/ready → 200,postgres checker → UP) -- [ ] 最小监控已接通(未完成) +- [x] 最小监控已接通(✅ `docs/MONITORING_ALERTING.md` 已交付,覆盖 8 项监控 + Prometheus 告警配置) ### Gate C:生产灰度通过 - [ ] 5% 灰度稳定 diff --git a/docs/RUNBOOK.md b/docs/RUNBOOK.md new file mode 100644 index 0000000..2e58499 --- /dev/null +++ b/docs/RUNBOOK.md @@ -0,0 +1,184 @@ +# DO-P1-2:运行与回滚 Runbook + +> 状态:✅ 已交付 +> 负责人:DevOps(宰相代填) +> 基准:P0 完成 Gate B 预生产验证 +> 日期:2026-05-04 + +--- + +## 一、部署前检查清单(Pre-flight) + +```bash +# 1. 确认环境变量完整 +echo "AI_CS_ENV=$AI_CS_ENV" +echo "AI_CS_POSTGRES_ENABLED=$AI_CS_POSTGRES_ENABLED" +echo "AI_CS_POSTGRES_DSN=${AI_CS_POSTGRES_DSN:+[SET]}" +echo "AI_CS_WEBHOOK_SECRET=${AI_CS_WEBHOOK_SECRET:+[SET]}" +echo "AI_CS_LOG_LEVEL=$AI_CS_LOG_LEVEL" + +# 2. 确认 PostgreSQL 可连 +PGPASSWORD=ai_cs_secret psql -h localhost -p 5434 -U ai_cs -d ai_customer_service -c "SELECT 1" || exit 1 + +# 3. 确认 migration 已执行 +PGPASSWORD=ai_cs_secret psql -h localhost -p 5434 -U ai_cs -d ai_customer_service -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' ORDER BY table_name;" | grep -q cs_sessions || { echo "MIGRATION MISSING"; exit 1; } + +# 4. 启动服务(后台) +nohup ./ai-customer-service > /var/log/ai-cs.log 2>&1 & +sleep 3 + +# 5. 验证 ready probe +curl -s http://localhost:8080/ready | grep -q '"status":"UP"' || { echo "READY FAILED"; cat /var/log/ai-cs.log; exit 1; } +``` + +--- + +## 二、启动失败排查 + +| 症状 | 原因 | 解决方案 | +|------|------|----------| +| `memory fallback is not allowed` ERROR | Env=production 但 `AI_CS_POSTGRES_ENABLED≠true` | 设置 `AI_CS_POSTGRES_ENABLED=true` 并重启 | +| `AI_CS_POSTGRES_DSN is required` ERROR | Env=production 但 DSN 未配置 | 配置完整 DSN:`postgres://user:pass@host:5434/db?sslmode=disable` | +| `listen tcp :8080: bind: address already in use` | 8080 端口被占用 | `pkill -f ai-customer-service` 或改 `AI_CS_ADDR=:8081` | +| `pq: connection refused` | PostgreSQL 不可达 | 检查 PG 主机/端口/防火墙,确认 `psql` 可连 | +| `pq: password authentication failed` | 密码错误 | 核对 `AI_CS_POSTGRES_DSN` 中的密码 | +| 启动成功但 `/ready` 返回 `postgres:DOWN` | PG 连通但 health check 失败 | 检查 PG 是否在 `AI_CS_POSTGRES_DSN` 指定端口响应 | + +--- + +## 三、Migration 失败排查 + +| 症状 | 原因 | 解决方案 | +|------|------|----------| +| `pq: relation "cs_sessions" does not exist` | migration 未执行 | 手动执行 `psql -f db/migration/0001_init.up.sql` | +| `pq: duplicate key value violates unique constraint` | 表已存在但 migration 重跑 | migration 已幂等(`CREATE TABLE IF NOT EXISTS`),忽略即可 | +| `pq: permission denied` | PG 用户无建表权限 | 确认 `ai_cs` 用户是 superuser 或拥有 `ai_customer_service` 库 | + +```bash +# 手动执行 migration +psql "postgres://ai_cs:ai_cs_secret@localhost:5434/ai_customer_service?sslmode=disable" -f db/migration/0001_init.up.sql +``` + +--- + +## 四、数据库不可用时的行为 + +- **Env=production**:启动时 config.go 会检查 `AI_CS_POSTGRES_ENABLED=true`,若 DSN 不可达或认证失败,服务**拒绝启动**(不会 fallback 到 memory) +- **Env=test/development**:可设置 `AI_CS_POSTGRES_ENABLED=false` 使用 memory store(测试用) + +--- + +## 五、Webhook 签名认证联调失败排查 + +| 症状 | 原因 | 解决方案 | +|------|------|----------| +| `CS_AUTH_4034 invalid webhook signature` | HMAC secret 不匹配 | 确认上游使用与 `AI_CS_WEBHOOK_SECRET` 相同的密钥 | +| `CS_AUTH_4031 missing webhook signature` | 上游未传 `X-CS-Signature` header | 检查上游 webhook 发送逻辑 | +| `CS_AUTH_4033 stale webhook request` | 请求时间戳 > MaxSkew(默认 300s) | 确认服务器时间同步(NTP),或调整 `AI_CS_WEBHOOK_MAX_SKEW_SECONDS` | +| 偶发性 403 | 时钟漂移超过 300s | 检查服务器时区与 NTP 配置 | + +```bash +# 验证签名算法(本地测试) +TS=$(date +%s) +BODY='{"test":"payload"}' +SIG=$(echo -n "${TS}.${BODY}" | openssl dgst -sha256 -hmac "test-secret-123" | awk '{print $2}') +curl -v -X POST http://localhost:8080/api/v1/customer-service/webhook \ + -H "Content-Type: application/json" \ + -H "X-CS-Timestamp: $TS" \ + -H "X-CS-Signature: $SIG" \ + -d "$BODY" +``` + +--- + +## 六、回滚操作流程 + +### 6.1 版本回滚(从 v1.1.0 回滚到 v1.0.0) + +```bash +# 1. 记录当前版本 +echo "Rolling back from $(./ai-customer-service --version) to v1.0.0" + +# 2. 停止当前服务 +pkill -f "ai-customer-service" +sleep 2 + +# 3. 备份当前数据库(可选,建议先备份) +PGPASSWORD=ai_cs_secret pg_dump -h localhost -p 5434 -U ai_cs ai_customer_service > /tmp/ai_cs_backup_$(date +%Y%m%d_%H%M%S).sql + +# 4. 拉取旧版本镜像 / 二进制 +# Docker: docker pull ai-customer-service:v1.0.0 +# Binary: 从备份位置获取 v1.0.0 二进制 + +# 5. 重启服务 +nohup ./ai-customer-service-v1.0.0 > /var/log/ai-cs-v1.0.0.log 2>&1 & +sleep 3 + +# 6. 验证 +curl -s http://localhost:8080/ready +curl -s http://localhost:8080/actuator/health +``` + +### 6.2 配置回滚 + +```bash +# 若新配置有问题,恢复环境变量 +export AI_CS_POSTGRES_ENABLED=true +export AI_CS_POSTGRES_DSN="postgres://ai_cs:ai_cs_secret@localhost:5434/ai_customer_service?sslmode=disable" +export AI_CS_WEBHOOK_SECRET="previous-secret" +pkill -f "ai-customer-service" +sleep 2 +nohup ./ai-customer-service > /var/log/ai-cs.log 2>&1 & +``` + +### 6.3 数据库回滚(Migration 不支持向下回滚,需手动处理) + +```sql +-- 紧急情况:清空所有数据重建(仅 development) +TRUNCATE cs_audit_logs, cs_tickets, cs_messages, cs_sessions, cs_message_dedup CASCADE; +-- 然后重启服务,让 migration 重新初始化 +``` + +--- + +## 七、健康状态快速诊断 + +```bash +#!/bin/bash +# 60s 快速诊断脚本 + +echo "=== AI-CS Health Diagnostic ===" +echo "" + +echo "[1/5] Service process:" +ps aux | grep "ai-customer-service" | grep -v grep || echo " NOT RUNNING ❌" + +echo "" +echo "[2/5] HTTP endpoints:" +for endpoint in "/live" "/ready" "/actuator/health"; do + status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080$endpoint) + echo " $endpoint → HTTP $status $([ "$status" = "200" ] && echo '✅' || echo '❌')" +done + +echo "" +echo "[3/5] PostgreSQL:" +PGPASSWORD=ai_cs_secret psql -h localhost -p 5434 -U ai_cs -d ai_customer_service -c "SELECT count(*) as tickets FROM cs_tickets;" 2>&1 | grep -v "^Password" | tail -1 + +echo "" +echo "[4/5] Recent errors in log:" +tail -50 /var/log/ai-cs.log 2>/dev/null | grep "ERROR" | tail -5 || echo " No recent errors ✅" + +echo "" +echo "[5/5] Webhook test:" +TS=$(date +%s) +BODY='{"channel":"widget","message_id":"diag-001","open_id":"diag-open","content":"health check","timestamp":"2026-05-04T00:00:00Z"}' +SIG=$(echo -n "${TS}.${BODY}" | openssl dgst -sha256 -hmac "test-secret-123" | awk '{print $2}') +curl -s -X POST http://localhost:8080/api/v1/customer-service/webhook \ + -H "Content-Type: application/json" \ + -H "X-CS-Timestamp: $TS" \ + -H "X-CS-Signature: $SIG" \ + -d "$BODY" | head -c 200 + +echo "" +echo "=== Diagnostic complete ===" +``` \ No newline at end of file