在 Day 25 完成 全系統 E2E 端到端測試與 Locust 壓力測試 後,PrescriptionVLM 已確定能在高併發流量下維持承載能力與快取效果。
然而,當系統正式長期運行在雲端 VPS 時,運營者無法時刻盯著 Terminal 的日誌。我們需要一個視覺化的監控面板,隨時掌控系統的 CPU/記憶體用量、API 請求量、HTTP 回應狀態碼分佈、Gemini VLM 解析耗時以及 Redis 快取命中率。
今天我們將為 PrescriptionVLM 系統導入 Prometheus 數據指標收集與 Grafana 視覺化監控面板。完成項目包含:
prometheus-flask-exporter 整合 — 在 Flask API 中暴露 /metrics 端點,採集 HTTP 請求、延遲與快取命中數據。在微服務與生產環境營運中,Prometheus + Grafana 是常見的監控方案:
/metrics 端點與 Redis Exporter 拉取效能指標,並存入時序資料庫。requirements.txt)請開啟專案根目錄的 requirements.txt,加入 Prometheus Flask 導出套件:
prometheus-flask-exporter==0.23.0
app.py)開啟 app.py,導入 PrometheusMetrics 並建立自訂指標來追蹤 Redis 快取命中狀況:
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_client import Counter
# 初始化 PrometheusMetrics,自動追蹤 HTTP 請求數與 Latency
metrics = PrometheusMetrics(app)
# 靜態標籤資訊
metrics.info('app_info', 'PrescriptionVLM Application Info', version='1.0.0')
# 自訂 Prometheus 計數器:追蹤快取命中與未命中次數
cache_hit_counter = Counter('prescription_cache_hits_total', 'Total number of Redis cache hits')
cache_miss_counter = Counter('prescription_cache_misses_total', 'Total number of Redis cache misses')
在 /analyze-prescription API 邏輯中更新計數器:
# ── 快取命中 (Cache Hit) 時 ──
cache_hit_counter.inc()
# ── 快取未命中 (Cache Miss) 時 ──
cache_miss_counter.inc()
prometheus/prometheus.yml)在專案根目錄建立 prometheus/prometheus.yml 檔案,設定 Scrape 目標:
global:
scrape_interval: 5s # 每 5 秒拉取一次指標
evaluation_interval: 5s
scrape_configs:
- job_name: 'prescription-vlm'
metrics_path: '/metrics'
static_configs:
- targets: ['web:5000']
- job_name: 'redis'
static_configs:
- targets: ['redis-exporter:9121']
docker-compose.yml 導入 Prometheus & Grafana 服務修改專案根目錄的 docker-compose.yml,加入 Prometheus、Grafana 與 Redis Exporter 容器:
version: '3.8'
services:
redis:
image: redis:7.2-alpine
container_name: prescription_redis
ports:
- "6379:6379"
restart: always
redis-exporter:
image: oliver006/redis_exporter:v1.58.0
container_name: prescription_redis_exporter
environment:
- REDIS_ADDR=redis:6379
restart: always
web:
build:
context: .
dockerfile: Dockerfile
container_name: prescription_service
env_file:
- .env
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
depends_on:
- redis
restart: always
nginx:
image: nginx:1.25-alpine
container_name: prescription_nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
- ./certbot/conf:/etc/letsencrypt:ro
- ./certbot/www:/var/www/certbot:ro
depends_on:
- web
restart: always
prometheus:
image: prom/prometheus:v2.51.0
container_name: prescription_prometheus
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
ports:
- "9090:9090"
restart: always
grafana:
image: grafana/grafana:10.4.0
container_name: prescription_grafana
ports:
- "3000:3000"
depends_on:
- prometheus
restart: always
1. 透過 Docker Compose 啟動完整監控容器組
在 Terminal 執行命令:
docker-compose up --build -d
2. 驗證 Prometheus Metrics 端點
使用 curl 觀察 Flask 產生的數據指標:
curl http://127.0.0.1:5000/metrics
flask_http_request_duration_seconds 與 prescription_cache_hits_total 的 Prometheus 格式數據。3. 配置 Grafana 儀表板
http://localhost:3000(預設帳密:admin / admin)。http://prometheus:9090 並點擊 Save & Test。測試通過後,將 Day 26 的修改提交至 GitHub:
git add requirements.txt app.py prometheus/ docker-compose.yml
git commit -m "保留雙引號 改填寫自己要記錄的標記 ex.鐵人賽第二十六天"
git push
今天我們為 PrescriptionVLM 構建了可視化的 Prometheus + Grafana 系統監控,讓運營者能直觀地掌控 API 流量、回應延遲與快取效率。
明天(Day 27),我們將進一步強化運營維護能力,實作 Grafana Alerting 系統異常告警與 LINE Bot 告警通知推播,當系統發生 5xx 錯誤或 API 延遲過高時通知開發團隊!