1. 18:43 14/10/2026
Khi dự án mở rộng sang nhiều nền tảng (web, mobile, desktop) trong một monorepo, việc theo dõi lỗi, hiệu năng và hành vi người dùng trở nên phức tạp. Giải pháp phổ biến hiện nay là tập trung telemetry bằng OpenTelemetry và hiển thị trên Grafana. Dưới đây là quy trình thực tế đã áp dụng thành công trong một dự án sản phẩm thực tế.
1. Cài đặt OpenTelemetry SDK cho từng nền tảng
- Web (Next.js): sử dụng @opentelemetry/sdk-node và @opentelemetry/instrumentation-http để tự động thu thập trace của request server-side và client-side.
- React Native: tích hợp @opentelemetry/sdk-react-native cùng với expo-modules-core để gửi span qua HTTP/JSON.
- Desktop (Electron): kết hợp Node SDK trong process chính và renderer process qua preload script.
2. Cấu hình Collector trung tâm
- Triển khai OpenTelemetry Collector trên Kubernetes (Deployment + Service).
- Receiver: otlp (gRPC & HTTP) cho trace, metrics, logs.
- Processor: batch, memory_limiter, resource/detection (thêm label env, service.name, platform).
- Exporter: prometheusremotewrite cho metrics, loki cho logs, tempo cho traces.
3. Định nghĩa naming convention và attribute chuẩn
- service.name = "myapp-{platform}" (web, mobile, desktop).
- deployment.environment = "staging|production".
- custom attribute: feature_flag, user_tier để lọc nhanh khi debug.
4. Tích hợp logging có cấu trúc
- Sử dụng pino (Node) / react-native-logs (RN) với formatter JSON.
- Gắn trace_id, span_id vào mỗi log line thông qua OpenTelemetry context propagation.
- Ship log qua Fluent Bit → Loki.
5. Dashboard Grafana thống nhất
- Board "Overview": RED metrics (Rate, Error, Duration) theo platform.
- Board "Trace Explorer": lọc theo feature_flag, user_tier.
- Board "Log Stream": hiển thị log có trace_id để correlating nhanh.
- Alert rule: error rate > 1% trong 5 phút trên bất kỳ platform nào → PagerDuty.
6. CI/CD gate kiểm tra telemetry
- Unit test mock OpenTelemetry exporter để đảm bảo span được tạo.
- Integration test trong pipeline staging: chạy smoke test, kiểm tra trace xuất hiện trong Tempo.
Lời khuyên áp dụng ngay:
- Bắt đầu từ một nền tảng, chuẩn hóa attribute, sau đó replicate sang nền tảng khác.
- Dùng feature flag để bật/tắt instrumentation mà không deploy lại.
- Review dashboard hàng tuần, loại bỏ metric không dùng để giảm chi phí lưu trữ.
Với cách tiếp cận này, đội ngũ giảm 70% thời gian trung bình để phát hiện root cause sự cố cross‑platform và có nền tảng mở rộng khi thêm dịch vụ mới.