iT邦幫忙

2025 iThome 鐵人賽

DAY 29
0
AI & Data

ML/DL實作-「營養抗老」專題製作系列 第 29

Day 28_監控與日誌整合 (Monitoring & Logging)

  • 分享至 

  • xImage
  •  

🎯 目標

建立基礎監控機制,確保服務狀態可視化

統一日誌輸出,方便問題追蹤


🏗️ 技術設計
1. Spring Boot Actuator

新增依賴:
2. 日誌管理

使用 Logback(Spring Boot 預設)

設定不同層級:INFO、WARN、ERROR

Log 格式:時間戳、thread、class、訊息

Docker 容器中,log 輸出到 stdout,可用 docker logs 查看

3. 容器監控 (Docker Compose)

docker-compose.yml 中加入 Prometheus + Grafana

Prometheus 抓取 Spring Boot /actuator/prometheus

Grafana 以 dashboard 顯示:

QPS (每秒請求數)

平均響應時間

DB 查詢延遲

4. 錯誤追蹤

在 @ControllerAdvice 全域錯誤處理裡,新增日誌輸出:

記錄 API 路徑、使用者、錯誤訊息、StackTrace(ERROR)

可以考慮未來接入 Sentry → 集中錯誤追蹤

🧪 測試計畫

啟動應用後,訪問:

http://localhost:8080/actuator/health

http://localhost:8080/actuator/metrics

壓力測試(用 Apache Bench 或 wrk):

ab -n 1000 -c 10 http://localhost:8080/api/recommendations?effectId=E001


→ 觀察 /actuator/metrics/http.server.requests

模擬錯誤(例如輸入不存在的 effectId) → 應寫入 ERROR log

✅ 驗收標準

Actuator 端點可正常回應

Log 檔案/容器輸出包含錯誤訊息,方便追蹤

Prometheus + Grafana 可視化監控指標

開發者能清楚看到系統「健康狀態 + API 負載 + 錯誤日誌」



上一篇
Day 28_安全性與權限控管
下一篇
Day 30_專案總結與文件化 (Final Documentation & Wrap-up)
系列文
ML/DL實作-「營養抗老」專題製作30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言