DDevArchive
Đăng nhập

Monitoring & Alerting: biết hệ thống đang khoẻ hay đang cháy

Log cho bạn biết chuyện gì đã xảy ra. Monitoring cho bạn biết chuyện gì đang xảy ra — ngay bây giờ. Kết hợp alerting để được thông báo TRƯỚC khi user phàn nàn.

4 Golden Signals

Google SRE đề xuất 4 tín hiệu vàng cần giám sát cho mọi service:

SignalĐo gìVí dụ cảnh báo
LatencyThời gian phản hồip95 > 500ms trong 5 phút
TrafficLượng requestRPS giảm 50% so với cùng giờ hôm qua
ErrorsTỷ lệ lỗi5xx > 1% tổng request
SaturationMức sử dụng tài nguyênCPU > 80% trong 10 phút

Prometheus + Grafana

Prometheus thu thập metrics từ ứng dụng theo kiểu pull (kéo dữ liệu). Grafana vẽ dashboard từ metrics đó. Combo này miễn phí, mạnh, và là tiêu chuẩn ngành.

// File: metrics.js
import promClient from 'prom-client'

// Đếm số request theo route và status
const httpRequests = new promClient.Counter({
  name: 'http_requests_total',
  help: 'Total HTTP requests',
  labelNames: ['method', 'route', 'status'],
})

// Đo thời gian phản hồi
const httpDuration = new promClient.Histogram({
  name: 'http_request_duration_seconds',
  help: 'HTTP request duration in seconds',
  labelNames: ['method', 'route'],
  buckets: [0.01, 0.05, 0.1, 0.5, 1, 5],
})

// Middleware đo metrics
app.use((req, res, next) => {
  const end = httpDuration.startTimer({ method: req.method, route: req.path })
  res.on('finish', () => {
    end()
    httpRequests.inc({ method: req.method, route: req.path, status: res.statusCode })
  })
  next()
})

Alerting: cảnh báo đúng lúc, không spam

Alert phải actionable: nhận được → biết phải làm gì. Alert quá nhiều = alert fatigue → bỏ qua hết. Phân level: Critical (gọi điện), Warning (Slack), Info (dashboard).

Đừng alert mọi thứ

CPU > 50% → KHÔNG alert (bình thường). CPU > 90% trong 10 phút → WARNING. Service chết → CRITICAL. Quy tắc: nếu nhận alert mà không cần làm gì → xoá alert đó.

❓ Trong 4 Golden Signals, "Saturation" đo gì?

  • Nói được 4 Golden Signals
  • Thiết lập Prometheus metrics trong Node.js
  • Hiểu Grafana dashboard cơ bản
  • Thiết kế alert không spam