Dưới đây là các truy vấn hữu ích (query) trong AWS CloudWatch Logs Insights, được nhóm theo mục đích, cùng ví dụ cụ thể bạn có thể copy dùng ngay:
🔍 1. Truy tìm IP truy cập
fields @timestamp, @message, @logStream
| filter headers.HTTP_X_FORWARDED_FOR like "13.115.24.102"
| sort @timestamp desc
| limit 100
✅ Dùng để tìm truy cập từ 1 IP cụ thể (đặc biệt khi dùng ALB hoặc API Gateway).
📈 2. Truy vấn lỗi HTTP 4xx/5xx
fields @timestamp, status, request, @message
| filter status >= 400
| sort @timestamp desc
| limit 100
✅ Tìm các request lỗi, giúp debug ứng dụng nhanh chóng.
⚠️ 3. Tìm lỗi Exception trong log
fields @timestamp, @message
| filter @message like /Exception|Error/
| sort @timestamp desc
| limit 100
✅ Tìm lỗi runtime, trace backtrace, useful cho backend apps (Rails, Node.js, etc.).
🧪 4. Tìm các request theo URL cụ thể
fields @timestamp, request, status, @message
| filter request like "/api/v1/search"
| sort @timestamp desc
| limit 100
✅ Tìm request tới API cụ thể, để debug hoặc theo dõi performance.
⏱️ 5. Thống kê tần suất theo URL
fields request
| stats count() by request
| sort count() desc
| limit 20
✅ Xem API/route nào được gọi nhiều nhất – useful cho monitoring hot endpoints.
🔄 6. Tìm retry hoặc request lặp lại nhiều lần
fields request, headers.HTTP_X_FORWARDED_FOR
| stats count() by request, headers.HTTP_X_FORWARDED_FOR
| sort count() desc
| limit 50
✅ Phát hiện DDoS, spam request, hoặc bot truy cập.
🧭 7. Tìm theo cụm từ khóa tuỳ chỉnh (debug string, trace ID, v.v.)
fields @timestamp, @message
| filter @message like "trace_id=abc123"
| sort @timestamp desc
✅ Tìm các log liên quan đến một trace cụ thể trong distributed tracing.
📊 8. Đếm số lỗi theo loại mã lỗi
fields status
| filter status >= 400
| stats count() by status
| sort status
✅ Biểu đồ số lượng lỗi 400, 403, 404, 500, 502, etc.
🗓 9. Truy xuất theo khoảng thời gian
fields @timestamp, request, status
| filter @timestamp >= '2025-07-24T10:00:00Z' and @timestamp <= '2025-07-24T12:00:00Z'
| sort @timestamp desc
| limit 100
✅ Dùng khi cần điều tra theo giờ, ví dụ sau sự cố production.
🔎 10. Tìm các request có response chậm
fields @timestamp, request, duration
| filter duration > 1000
| sort duration desc
| limit 100
✅ Giúp phát hiện API slow, tắc nghẽn.