# Observability & Ops (เฟส 8)
อัปเดต: 20 กุมภาพันธ์ 2026

## Logging
- ใช้ pino (`src/logger.ts`) พร้อม reqId อัตโนมัติ; dev เป็น pretty print
- Ingest job log event `"ingest completed"` พร้อม accountId, rows, duration_ms, checkpoint_date

## Metrics
- In-memory counter (`src/metrics.ts`) และ endpoint:
  - `GET /metrics` → text (Prometheus-style)
  - `GET /metrics/json` → snapshot JSON
- Countersที่มีตอนนี้: `ingest_success`, `ingest_rows`, `ingest_ms_total`, `ingest_row_skip`

## Alerts (ข้อเสนอเริ่มต้น)
- Error rate ingest > 2% ภายใน 15 นาที → แจ้งเตือน
- ingest lag: checkpoint_date < today - 1d → แจ้งเตือนราย account
- Throughput ต่ำผิดปกติ: `ingest_rows` < baseline/4 ต่อชั่วโมง → แจ้งเตือน

## Health/Readiness
- `/health` — service metadata
- `/ready` — ยังบอก `db-not-wired` จนกว่าจะเชื่อม DB ในขั้นต่อไป (ปรับในเฟส deploy)

## Runbook สั้น
1) เช็ค `/metrics` ว่ามี ingest_success เพิ่มหรือไม่  
2) ถ้า error: ดู log ผ่าน PM2 (`pm2 logs wepappopusapi`)  
3) ตรวจ checkpoint ใน DB: `SELECT * FROM api_checkpoints;`  
4) ถ้าต้อง reset ingest เฉพาะ account: ลบแถวใน `api_checkpoints` แล้วรัน job manual

## TODO (เฟส 9–10)
- เพิ่ม trace id propagation ไปยัง API client (header `X-Request-ID`)
- ต่อระบบ alert จริง (Prometheus/ELK) ตาม infra ที่ใช้
