장애 감지 흐름

앱 로그 발생
    │
    ▼
CloudWatch Logs (Log Group)
    │
    ├── Metric Filter → 특정 패턴 카운트
    │       │
    │       ▼
    │   Custom Metric
    │       │
    │       ▼
    │   Alarm → 임계값 초과 시 알림
    │
    └── Logs Insights → 직접 쿼리로 원인 분석

1. 로그 설계 (JSON 형식 필수)

왜 JSON이어야 하나?

# 일반 텍스트 로그 → Metric Filter 작성 어려움
[ERROR] 2026-04-06 DB connection failed

# JSON 로그 → 필드 기반 필터링 가능 ✅
{"level": "ERROR", "path": "/ready", "status_code": 500, "instance_id": "i-xxx"}

경기 과제 기준 필수 필드

{
  "request_id":        "uuid-xxxx",
  "instance_id":       "i-0abc1234",
  "availability_zone": "ap-northeast-2a",
  "path":              "/v1/users",
  "status_code":       200,
  "level":             "INFO",
  "message":           "user created"
}

CloudWatch Agent 설정

{
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "/opt/app/logs/app.log",
            "log_group_name": "/worldpay/user/app",
            "log_stream_name": "{instance_id}",
            "timezone": "UTC"
          }
        ]
      }
    }
  }
}

2. Metric Filter 설계

패턴 문법

# JSON 필드 기준 필터
{ $.status_code = 500 }
{ $.level = "ERROR" }
{ $.path = "/ready" && $.status_code != 200 }

# 텍스트 기준 필터
ERROR
"connection refused"

경기 과제 기준 Metric Filter

① 앱 에러 카운트

Filter name:    wp-app-error-count
Filter pattern: { $.status_code >= 500 }
Metric name:    AppErrorCount
Namespace:      WorldPay/App
Value:          1

② /ready 실패 카운트

Filter name:    wp-ready-fail-count
Filter pattern: { $.path = "/ready" && $.status_code != 200 }
Metric name:    ReadyFailCount
Namespace:      WorldPay/App
Value:          1