feat(monitor): F6 日志告警引擎 — AlertEngine 支持 type=log 规则查询Loki/ES+触发/恢复 + 数据源认证升级(none/basic/bearer/custom) + 从查询创建告警真实API对接

This commit is contained in:
attacker committed 2026-06-28 04:00:38 +08:00
1 parent 894cb16a05
commit 6bba6144f3
17 files changed
+657 -74

No files matched your search

+49
View File
@@ -918,3 +918,52 @@ cd /srv/aiops/frontend && npm run build
- [x] U8: `npm run build` 零错误
- [x] 前端 `traceApi` 不再是 mock 桩(真实 HTTP 调用)
- [x] 17/17 集成回归测试通过
---
## P1-F6: 日志告警引擎
### 验证用例
```bash
TOKEN=$(python3 -c "
import urllib.request, json
login_data = json.dumps({'username': 'admin', 'password': '123456'}).encode()
req = urllib.request.Request('http://127.0.0.1:8080/api/v1/auth/login', data=login_data, method='POST')
req.add_header('Content-Type', 'application/json')
resp = urllib.request.urlopen(req)
print(json.loads(resp.read())['data']['token'])
")
# U1: 引擎状态正常(LogDSHandler 注入后分布式锁仍工作)
curl -s http://127.0.0.1:8080/api/v1/alerts/engine/status \
-H "Authorization: Bearer *** | python3 -c "import json,sys; d=json.load(sys.stdin)['data']; assert d['is_leader']==True and d['distributed']==True; print('✅ engine distributed+leader')"
# U2: 创建日志告警规则(type=log, datasource_id 绑定)
curl -s -X POST "http://127.0.0.1:8080/api/v1/alerts/rules" \
-H "Authorization: Bearer *** -H "Content-Type: application/json" \
-d '{"name":"F6-test","type":"log","source":"loki","expression":"{app=\"nginx\"} |= \"ERROR\"","severity":"critical","duration":60,"datasource_id":1}' \
| python3 -c "import json,sys; d=json.load(sys.stdin); assert d['code']==200; print(f'✅ create log rule id={d[\"data\"][\"id\"]}')"
# U3: 数据源认证字段(auth_type/token/headers)
docker exec aiops-mysql mysql -uroot -paiops_root_2026 aiops -e "SHOW COLUMNS FROM monitor_log_datasources;" 2>/dev/null | grep -E 'auth_type|token|headers'
# U4: go build
cd /srv/aiops/backend && go build ./...
# U5: npm build
cd /srv/aiops/frontend && npm run build
# U6: 集成回归
python3 tests/integration_test.py --host 127.0.0.1 --port 8080 | tail -3
```
### 通过标准
- [x] U1: 引擎状态 distributed=true, is_leader=true(LogDSHandler 注入不破坏锁机制)
- [x] U2: 创建 `type="log"` 告警规则返回 200,含 datasource_id + expression
- [x] U3: DB 表 monitor_log_datasources 含 auth_type/token/headers 三列
- [x] U4: `go build` 零错误
- [x] U5: `npm run build` 零错误(✓ built in 18.57s)
- [x] U6: 17/17 集成回归测试通过
- [x] 引擎无 panic(无效 datasource_id 优雅跳过)
- [x] 日志数据源支持 none/basic/bearer/custom 四种认证方式(round-trip 验证)