Phạm vi của ghi chú lab
Các endpoint và metric thuộc lab IoT được mô tả trong tài liệu, chưa được chạy lại ở lượt nhập này. Với counter, đọc rate/increase theo cửa sổ thời gian thay vì coi tổng tích lũy > 0 là lỗi đang diễn ra. Tên duration_seconds không tự đảm bảo có p95: cần histogram/summary hoặc cách thu thập phù hợp. Prometheus đánh giá alert rule, Alertmanager định tuyến thông báo.
Mục tiêu của bước này là thêm metric từ chính application, không chỉ nhìn metric hạ tầng.
Trước bước này, Grafana chủ yếu nhìn được:
Kafka topic rate
Kafka consumer lag
TimescaleDB transaction/insert/lock/connection
Redis/TimescaleDB up/downCác chỉ số đó giúp biết hạ tầng đang ra sao, nhưng chưa đủ để trả lời:
Bridge nhận được bao nhiêu message từ MQTT?
Bridge publish Kafka lỗi không?
Consumer đọc Kafka nhanh cỡ nào?
Consumer insert DB mất bao lâu?
AI analyzer có drop message do lag không?
Alert dispatcher có dedupe/rate-limit/DLQ không?
Batch export sang MinIO có chạy không?Vì vậy cần app-level metrics.
1. Kiến trúc sau khi thêm app metrics
Go service
-> expose /metrics
-> Prometheus scrape
-> Grafana dashboard
-> Alertmanager rule sau nàyCác endpoint:
bridge: http://localhost:9101/metrics
consumer: http://localhost:9102/metrics
ai-analyzer: http://localhost:9103/metrics
alert-dispatcher: http://localhost:8090/metrics
batch-export: http://localhost:9104/metricsPrometheus scrape qua Docker bằng host:
host.docker.internalLý do: app Go hiện chạy trên host Windows, còn Prometheus chạy trong container.
2. File đã thêm/sửa
internal/metrics/metrics.go
cmd/bridge/main.go
cmd/consumer/main.go
cmd/ai-analyzer/main.go
cmd/alert-dispatcher/main.go
cmd/batch-export/main.go
cmd/batch-export/exporter.go
docker/prometheus/prometheus.yml
docker/grafana/dashboards/iot-app-metrics.jsoninternal/metrics là một metrics helper nhỏ xuất format Prometheus text.
Trong production thật có thể dùng thư viện chuẩn:
prometheus/client_golang
OpenTelemetry metrics SDKỞ lab này dùng helper nhỏ để không kéo thêm dependency và dễ nhìn code.
3. Các metric quan trọng
Bridge
iot_bridge_mqtt_received_total
iot_bridge_kafka_published_total
iot_bridge_kafka_publish_failed_total
iot_bridge_queue_depthMục đích:
Biết bridge có nhận được MQTT không.
Biết bridge có publish sang Kafka kịp không.
Biết Kafka publish có lỗi không.
Biết queue nội bộ của bridge có bị dồn không.Cách đọc:
received rate gần published rate
-> bridge khỏe.
received tăng nhưng published thấp hơn nhiều
-> bridge hoặc Kafka publish đang nghẽn.
queue_depth tăng liên tục
-> worker không xử lý kịp input.
publish_failed > 0
-> kiểm tra Kafka, network, broker health.DB Consumer
iot_consumer_kafka_read_total
iot_consumer_db_inserted_rows_total
iot_consumer_db_deduped_rows_total
iot_consumer_db_insert_failed_total
iot_consumer_kafka_commit_failed_total
iot_consumer_parse_failed_total
iot_consumer_pending_batch_size
iot_consumer_db_batch_insert_duration_secondsMục đích:
Biết consumer đọc Kafka bao nhiêu message/s.
Biết insert DB thực tế bao nhiêu rows/s.
Biết duplicate bị skip bao nhiêu.
Biết lỗi DB insert, lỗi commit Kafka, lỗi parse message.
Biết batch insert mất bao lâu.Cách đọc:
Kafka ingress cao, consumer read thấp
-> consumer không đọc kịp, lag sẽ tăng.
consumer read cao, DB inserted thấp
-> có thể duplicate nhiều, parse lỗi, hoặc DB insert fail.
db_batch_insert_duration p95 tăng
-> DB hoặc batch insert bắt đầu chậm.
db_insert_failed > 0
-> DB down, network lỗi, constraint/schema lỗi.
kafka_commit_failed > 0
-> DB đã ghi nhưng offset chưa commit, có thể retry và rely on idempotency.AI Analyzer
iot_ai_kafka_read_total
iot_ai_anomaly_detected_total
iot_ai_alert_published_total
iot_ai_alert_publish_failed_total
iot_ai_poison_message_total
iot_ai_lagged_message_dropped_total
iot_ai_tracked_shipsMục đích:
Biết AI đọc được bao nhiêu message/s.
Biết phát hiện bao nhiêu anomaly.
Biết publish alert thành công/thất bại.
Biết có message rác hoặc message quá cũ bị bỏ không.
Biết số tàu đang giữ state trong memory.Cách đọc:
ai read thấp hơn Kafka ingress nhiều
-> ai-group lag sẽ tăng.
lagged_message_dropped tăng
-> AI xử lý quá muộn, alert realtime mất ý nghĩa.
anomaly_detected tăng nhưng alert_published không tăng
-> lỗi publish sang topic alerts.
poison_message tăng
-> cần xem producer/data validation.Alert Dispatcher
iot_dispatcher_alert_read_total
iot_dispatcher_sse_broadcast_total
iot_dispatcher_alert_deduped_total
iot_dispatcher_rate_limited_total
iot_dispatcher_dlq_sent_total
iot_dispatcher_notification_sent_total
iot_dispatcher_notification_failed_total
iot_dispatcher_redis_error_total
iot_dispatcher_sse_clients
iot_dispatcher_insight_read_totalMục đích:
Biết dispatcher đọc alert không.
Biết dashboard có được push SSE không.
Biết Redis dedupe chặn bao nhiêu alert trùng.
Biết notification bị rate limit hoặc fail bao nhiêu.
Biết có đẩy DLQ không.
Biết dashboard đang có bao nhiêu client SSE.Cách đọc:
alert_read tăng nhưng sse_broadcast không tăng
-> dispatcher hoặc SSE hub có vấn đề.
deduped tăng cao
-> nhiều alert lặp cùng ship_id/metric trong cửa sổ TTL.
rate_limited tăng
-> alert quá dày so với giới hạn notification.
dlq_sent tăng
-> cần đọc lý do DLQ: redis_down, api_timeout, rate_limited.Batch Export
iot_batch_export_success_total
iot_batch_export_failed_total
iot_batch_export_duration_seconds
iot_batch_export_records_total
iot_batch_export_uploaded_bytes_total
iot_batch_export_minio_upload_failed_totalMục đích:
Biết job export có chạy thành công không.
Biết mỗi lần export mất bao lâu.
Biết export được bao nhiêu record và bao nhiêu byte.
Biết MinIO upload có lỗi không.Cách đọc:
export_success tăng đều
-> cron export đang chạy.
records_total không tăng dù DB có dữ liệu mới
-> kiểm tra watermark/export state/query.
export_failed hoặc upload_failed tăng
-> kiểm tra TimescaleDB/MinIO/network.4. Dashboard mới
Trong Grafana mở:
Dashboards -> IoT Pipeline -> IoT App MetricsCác panel chính:
Bridge Throughput
Bridge Queue Depth
Bridge Publish Errors
DB Consumer Throughput
DB Batch Insert Duration p95
DB Consumer Errors
AI Analyzer Flow
Alert Dispatcher Flow
Batch Export Flow
Dashboard SSE Clients
AI Dropped Messages5. Cách test nhanh
Start observability:
.\run_observability.batBuild lại app:
.\run_ci_local.ps1Start app như bình thường:
.\run_all.batHoặc start từng service riêng để dễ test:
.\bin\bridge.exe
.\bin\consumer.exe
.\bin\ai-analyzer.exe
.\bin\alert-dispatcher.exe
.\bin\batch-export.exeChạy load:
$env:SHIP_COUNT="50"
$env:SENSORS_PER_SHIP="10"
$env:RATE_PER_SEC="200"
$env:DURATION="2m"
.\run_load_generator.batSau khoảng 15-30 giây, Prometheus scrape xong thì Grafana bắt đầu có data.
6. Tư duy phân tích khi load test
Đi theo luồng:
Load generator báo sent rate
-> Bridge Throughput có nhận gần rate đó không?
-> Bridge published sang Kafka có gần received không?
-> Kafka topic ingress có tăng tương ứng không?
-> DB consumer read có theo kịp Kafka ingress không?
-> DB inserted rows/s có theo kịp consumer read không?
-> DB insert duration p95 có tăng không?
-> Kafka consumer lag có tăng và có tự tụt không?Nếu nghẽn bridge:
bridge queue_depth tăng
published thấp hơn received
publish_failed có thể tăngNếu nghẽn DB consumer:
Kafka ingress cao
iot-consumer-group lag tăng
consumer read/insert thấp hơn ingress
db_batch_insert_duration p95 tăngNếu DB lỗi:
db_insert_failed tăng
consumer lag tăng
TimescaleDB up có thể = 0 nếu DB downNếu AI không kịp:
ai-group lag tăng
ai read thấp hơn ingress
lagged_message_dropped tăng sau khi backlog quá cũNếu alert/notification có vấn đề:
alerts topic có message
dispatcher alert_read thấp hoặc lỗi
dlq_sent/rate_limited/notification_failed tăng7. Luyện phỏng vấn
Đọc đoạn trả lời gốc và luyện kể lại lab
8. Lưu ý
Metric label phải giữ low-cardinality. Không đưa ship_id, request_id, trace_id vào label metric nếu số lượng lớn.
Các thông tin chi tiết theo từng event sẽ đưa vào logs/traces ở bước sau.
Hiện bước này mới là metrics. Các bước tiếp theo:
Level 2: Health endpoints /live /ready
Level 3: Structured logs
Level 4: Loki hoặc Alloy để gom logs
Level 5: Correlation ID
Level 6: OpenTelemetry tracing + Tempo/JaegerNối với lý thuyết
Bốn tín hiệu observability: Metrics, Logs, Traces, Profiles
Four Golden Signals: khung tối thiểu cho service hướng người dùng
RED và USE: hai framework thực dụng
Cardinality: thứ có thể làm hệ monitoring tự sập
Bài thực hành tiếp theo · Lộ trình thực hành
Nguồn: observability-level-1-app-metrics.md. Lệnh chạy thuộc dự án IoT, không chạy trong repo Quartz.