修復 AI 自動化 Prometheus scrape
All checks were successful
CD Pipeline / deploy (push) Successful in 1m26s
All checks were successful
CD Pipeline / deploy (push) Successful in 1m26s
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
> 本文件定義專案開發的核心準則與不可違反的規範
|
||||
> **建立日期**: 2026-01-12
|
||||
> **當前版本**: V10.10 (四 AI Agent 自動化 Grafana 觀測版)
|
||||
> **當前版本**: V10.11 (四 AI Agent 自動化 Metrics Scrape 修復版)
|
||||
> **最後更新**: 2026-04-29
|
||||
|
||||
---
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
- ADR-018:四 AI Agent 自動化控制面立案。
|
||||
- Memory:新增 `docs/memory/ai_automation_closure_20260429.md`。
|
||||
- Guide/Skills 替代:新增 `docs/guides/ai_automation_session_sop.md`。
|
||||
- SOT:更新 `docs/AI_INTELLIGENCE_MODULE_SOT.md` 至 V10.10 AI Automation Grafana Observability 架構。
|
||||
- SOT:更新 `docs/AI_INTELLIGENCE_MODULE_SOT.md` 至 V10.11 AI Automation Metrics Scrape 架構。
|
||||
- Codex 規則:更新 `AGENTS.md`、`CONSTITUTION.md`、ADR/memory 索引。
|
||||
- Prometheus 指標化:新增 EventRouter / AutoHeal / safe action / replay in-process metrics,並接入 `/metrics`。
|
||||
- 線上 smoke dashboard:新增 `/ai_automation_smoke` 與 `/api/ai-automation/smoke`,覆蓋 EventRouter、AutoHeal、NemoTron fallback、OpenClaw embedding queue、ElephantAlpha HITL。
|
||||
@@ -15,9 +15,12 @@
|
||||
- Smoke 趨勢管理:新增 JSONL 匯出、清理與每日摘要。
|
||||
- Smoke 每日摘要:新增 Telegram 手動推播 API 與 momo-scheduler 每日 09:10 排程入口。
|
||||
- Grafana 視覺化:新增 `MOMO AI Automation Overview` provisioning dashboard,覆蓋 EventRouter、safe action、replay、AutoHeal 指標。
|
||||
- Grafana 線上部署:188 active Grafana 已載入 4 個 dashboard,`MOMO AI Automation Overview` provisioning 成功。
|
||||
- Prometheus scrape 修復:active monitoring stack 新增 `momo-app` scrape job,目標 `momo-pro-system:80/metrics`。
|
||||
- Gunicorn preload 修復:`post_fork` 略過 Flask/Werkzeug request-bound LocalProxy,避免 worker boot fail。
|
||||
|
||||
【下次待辦】
|
||||
- 將 Grafana provisioning dashboard 部署到 110/188 監控環境後,觀察 panel 是否都有資料。
|
||||
- 觀察 Prometheus scrape 後 `momo_ai_*` 是否在事件發生後產生時間序列。
|
||||
- Superset panel 設定與 Smoke 摘要成效觀察。
|
||||
|
||||
================================================================================
|
||||
|
||||
4
app.py
4
app.py
@@ -95,8 +95,8 @@ except Exception as e:
|
||||
sys_log.error(f"無法檢測磁碟空間: {e}")
|
||||
|
||||
# 🚩 系統版本定義 (備份與顯示用)
|
||||
# 🚩 2026-04-29 V10.10: AI 自動化 Grafana dashboard provisioning
|
||||
SYSTEM_VERSION = "V10.10"
|
||||
# 🚩 2026-04-30 V10.11: Gunicorn preload guard + AI metrics scrape
|
||||
SYSTEM_VERSION = "V10.11"
|
||||
|
||||
# ==========================================
|
||||
# 🔒 SQL Injection 防護函數
|
||||
|
||||
@@ -253,7 +253,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '')
|
||||
# ==========================================
|
||||
# 系統版本與路徑
|
||||
# ==========================================
|
||||
SYSTEM_VERSION = "V10.10"
|
||||
SYSTEM_VERSION = "V10.11"
|
||||
LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log')
|
||||
public_url = PUBLIC_URL # 用於模板顯示
|
||||
|
||||
|
||||
@@ -75,9 +75,9 @@ scrape_configs:
|
||||
# ---------------------------------------------------------------------------
|
||||
- job_name: 'momo-app'
|
||||
static_configs:
|
||||
- targets: ['192.168.0.110:5001']
|
||||
- targets: ['momo-pro-system:80']
|
||||
labels:
|
||||
instance: 'momo-flask'
|
||||
instance: 'momo-pro-system'
|
||||
env: 'uat'
|
||||
service: 'application'
|
||||
metrics_path: /metrics
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# MOMO PRO — AI 競價情報模組 Single Source of Truth
|
||||
|
||||
> **最後更新**: 2026-04-29 (台北時間)
|
||||
> **狀態**: 🟢 四 AI Agent 自動化閉環已落地 — EventRouter / AutoHeal / OpenClaw Memory / ElephantAlpha bridge / Prometheus metrics / Smoke Dashboard / Smoke Trend Management / Telegram Summary / Grafana provisioning 具測試覆蓋
|
||||
> **適用版本**: V10.10 AI Automation Grafana Observability 架構
|
||||
> **狀態**: 🟢 四 AI Agent 自動化閉環已落地 — EventRouter / AutoHeal / OpenClaw Memory / ElephantAlpha bridge / Prometheus metrics / Smoke Dashboard / Smoke Trend Management / Telegram Summary / Grafana provisioning / Prometheus scrape 具測試覆蓋
|
||||
> **適用版本**: V10.11 AI Automation Metrics Scrape 架構
|
||||
|
||||
---
|
||||
|
||||
@@ -68,6 +68,7 @@ SQL漏斗(~300筆)
|
||||
- Smoke history 支援 JSONL 匯出、清理與每日 OK / Warning / Critical 摘要。
|
||||
- Smoke 每日摘要支援手動 Telegram 推播,並由 `momo-scheduler` 每日 09:10 呼叫 `run_ai_smoke_daily_summary_task()`。
|
||||
- Grafana provisioning 新增 `docker/grafana/provisioning/dashboards/json/ai-automation-overview.json`,觀測 EventRouter dispatch/latency、safe action、Telegram replay 與 AutoHeal action/duration。
|
||||
- Active monitoring stack 使用 `monitoring/prometheus.yml` 的 `momo-app` job scrape `momo-pro-system:80/metrics`;Prometheus container 需加入 `momo-network`。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -149,7 +149,8 @@ L1 Hermes 掛 → L0 模板直出 + 🟡 「AI 分析暫不可用」
|
||||
- 2026-04-29 已補 smoke history JSONL 匯出、清理與每日摘要。
|
||||
- 2026-04-29 已補 smoke 每日摘要 Telegram 手動推播與 momo-scheduler 09:10 排程入口。
|
||||
- 2026-04-29 已補 Grafana provisioning dashboard:`MOMO AI Automation Overview` 覆蓋 EventRouter、L2 safe action、Telegram replay 與 AutoHeal Prometheus 指標。
|
||||
- 尚未完成:Superset 視覺化面板、Grafana 線上部署後資料觀察與 Smoke 摘要推播成效觀察。
|
||||
- 2026-04-30 已補 active monitoring scrape:Prometheus `momo-app` job 讀取 `momo-pro-system:80/metrics`,供 Grafana AI dashboard 使用。
|
||||
- 尚未完成:Superset 視覺化面板、AI event 發生後的 `momo_ai_*` 時間序列觀察與 Smoke 摘要推播成效觀察。
|
||||
|
||||
## References
|
||||
- `services/event_router.py` — 分流入口(Phase 1)
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
- EventRouter / AutoHeal 變更必須更新 `services/ai_automation_metrics.py` 指標或確認既有指標已覆蓋。
|
||||
- AI 自動化閉環變更必須確認 `/api/ai-automation/smoke` 與 `/ai_automation_smoke` 仍能反映新狀態。
|
||||
- AI 自動化 Prometheus 指標變更必須同步檢查 `docker/grafana/provisioning/dashboards/json/ai-automation-overview.json` 是否需要新增 panel 或查詢。
|
||||
- 線上 active monitoring stack 以 `monitoring/prometheus.yml` 為準;若 dashboard 無資料,先確認 Prometheus `momo-app` target 與 `momo-network` 連線。
|
||||
- Smoke dashboard 會保存 JSONL 趨勢;若新增檢查項目,要確保 history compact record 仍保持小而可讀。
|
||||
- Smoke history 管理只能操作 `MOMO_AI_AUTOMATION_SMOKE_HISTORY` 指向的 JSONL,不得清理 DB 或 EventRouter queue。
|
||||
- Smoke 每日摘要推播只讀 history,不得重新執行 smoke,也不得把完整 details 寫進 Telegram。
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
- Smoke history 已支援 JSONL 匯出、清理與每日摘要;清理只影響 smoke history,不碰 DB 或 EventRouter queue。
|
||||
- Smoke 每日摘要已支援手動 Telegram 推播與 scheduler 09:10 排程入口;摘要只讀 JSONL history。
|
||||
- Grafana provisioning 已新增 `MOMO AI Automation Overview`,由 Prometheus `/metrics` 觀測 EventRouter、safe action、replay 與 AutoHeal 趨勢。
|
||||
- 2026-04-30 active Grafana 已載入 4 個 dashboard;AI dashboard 檔案同步到 188 實際掛載目錄 `monitoring/grafana/provisioning/dashboards/json/`。
|
||||
- 2026-04-30 active Prometheus 補 `momo-app` scrape job,目標 `momo-pro-system:80/metrics`;Prometheus 需加入 `momo-network` 才能解析 app container DNS。
|
||||
- 2026-04-30 發現並修復 `gunicorn.conf.py` `post_fork` 掃到 Flask/Werkzeug LocalProxy 導致 worker boot fail 的問題。
|
||||
|
||||
## 已落地範圍
|
||||
|
||||
@@ -32,6 +35,7 @@
|
||||
- Export API 回傳 `application/x-ndjson`,clear API 只刪除 `MOMO_AI_AUTOMATION_SMOKE_HISTORY` 指向檔案。
|
||||
- Daily summary API:`POST /api/ai-automation/smoke/daily-summary/send`。
|
||||
- Grafana dashboard 檔案:`docker/grafana/provisioning/dashboards/json/ai-automation-overview.json`;provider 會載入 JSON 目錄,不需要修改 dashboard provider。
|
||||
- Active monitoring 使用 `monitoring/prometheus.yml`,不是 `docker/prometheus/prometheus.yml`;若線上 panel 無資料,先查 Prometheus 是否有 `momo-app` target。
|
||||
|
||||
## 驗證紀錄
|
||||
|
||||
@@ -42,6 +46,8 @@
|
||||
- 2026-04-29 AI smoke summary 批次:`9 passed`(smoke + metrics)。
|
||||
- 2026-04-29 AI Grafana observability 批次:`3 passed`(Grafana dashboard JSON 結構與必要 metric)。
|
||||
- 2026-04-29 AI Grafana observability + AI core 回歸:`36 passed`,collect-only:`36 tests collected`。
|
||||
- 2026-04-30 Gunicorn LocalProxy 修復:新增 `tests/test_gunicorn_config.py`。
|
||||
- 2026-04-30 Prometheus scrape 修復:新增 `tests/test_prometheus_ai_automation_scrape.py`。
|
||||
- 2026-04-29 L2 安全記憶批次:`24 passed`。
|
||||
- collect-only:`48 tests collected`。
|
||||
- `git diff --check` 已通過。
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
- **Smoke 趨勢管理**: Dashboard 增加 JSONL 匯出、清理與每日摘要,清理範圍限定 smoke history 檔。
|
||||
- **Smoke 每日摘要推播**: 新增 Telegram 手動推播 API 與 momo-scheduler 每日 09:10 摘要任務,只讀 smoke history。
|
||||
- **Grafana AI 觀測**: 新增 `MOMO AI Automation Overview` provisioning dashboard,覆蓋 EventRouter、safe action、replay、AutoHeal Prometheus 指標。
|
||||
- **Grafana 線上載入與 scrape 修復**: 188 active Grafana 載入 4 dashboards;active Prometheus 補 `momo-app` scrape job,並修復 gunicorn preload LocalProxy boot crash。
|
||||
|
||||
### 2026-04-28~29:Phase 3e 重構大戰 + daily_sales cache 隱形 bug 根除
|
||||
- **app.py 縮減 -10.8%**: 7,386 → 6,590 行,11 commits 全綠零 502。
|
||||
|
||||
@@ -38,6 +38,7 @@ services:
|
||||
- '--web.console.templates=/usr/share/prometheus/consoles'
|
||||
networks:
|
||||
- monitoring
|
||||
- momo-network
|
||||
|
||||
# Grafana - 視覺化儀表板
|
||||
grafana:
|
||||
@@ -95,6 +96,9 @@ services:
|
||||
networks:
|
||||
monitoring:
|
||||
driver: bridge
|
||||
momo-network:
|
||||
external: true
|
||||
name: momo-network
|
||||
|
||||
volumes:
|
||||
prometheus_data:
|
||||
|
||||
@@ -39,6 +39,16 @@ scrape_configs:
|
||||
instance: 'uat-server'
|
||||
environment: 'uat'
|
||||
|
||||
# MOMO Flask app metrics(含 AI automation /metrics)
|
||||
- job_name: 'momo-app'
|
||||
metrics_path: /metrics
|
||||
static_configs:
|
||||
- targets: ['momo-pro-system:80']
|
||||
labels:
|
||||
instance: 'momo-pro-system'
|
||||
service: 'application'
|
||||
environment: 'uat'
|
||||
|
||||
# Blackbox - HTTP/HTTPS 網站監控
|
||||
- job_name: 'blackbox-http'
|
||||
metrics_path: /probe
|
||||
|
||||
27
tests/test_prometheus_ai_automation_scrape.py
Normal file
27
tests/test_prometheus_ai_automation_scrape.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def test_active_monitoring_prometheus_scrapes_momo_app_metrics():
|
||||
config = (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")
|
||||
|
||||
assert "job_name: 'momo-app'" in config
|
||||
assert "metrics_path: /metrics" in config
|
||||
assert "targets: ['momo-pro-system:80']" in config
|
||||
|
||||
|
||||
def test_monitoring_stack_connects_prometheus_to_momo_network():
|
||||
compose = (ROOT / "monitoring/docker-compose.yml").read_text(encoding="utf-8")
|
||||
|
||||
assert "momo-network:" in compose
|
||||
assert "external: true" in compose
|
||||
assert "name: momo-network" in compose
|
||||
|
||||
|
||||
def test_compose_prometheus_uses_container_dns_for_momo_app():
|
||||
config = (ROOT / "docker/prometheus/prometheus.yml").read_text(encoding="utf-8")
|
||||
|
||||
assert "job_name: 'momo-app'" in config
|
||||
assert "targets: ['momo-pro-system:80']" in config
|
||||
Reference in New Issue
Block a user