Skip to content

OpenResty JSON access log format #252

@test3207

Description

@test3207

OpenResty JSON Access Log Format - Implemented ✅

Summary

Implemented JSON access log format for Gateway nginx/OpenResty to enable structured querying in Grafana Loki.

Changes Made

Repository: test3207/m3w-k8s

Commits

  • d4d1dce: feat(observability): add JSON access log format to Gateway nginx - Closes OpenResty JSON access log format #252
  • b6647f0: fix: initialize routing variables at server level to avoid warnings

Files Modified

  1. ansible/playbooks/templates/gateway-nginx.conf.j2

    • Replaced log_format main (combined format) with log_format json_access escape=json
    • Added structured JSON fields for observability
    • Added server-level default variables for $backend and $target_region
  2. ansible/playbooks/templates/gateway-routing.lua.j2

    • Added ngx.var.target_region = region to set the target region variable for logging

JSON Log Format Fields

{
  "timestamp": "ISO8601 format",
  "service": "m3w-gateway",
  "gateway": "gateway1|gateway2",
  "type": "access",
  "http": {
    "method": "GET|POST|...",
    "path": "/api/...",
    "query": "query string",
    "status": 200,
    "bytes_sent": 1234,
    "request_time": 0.006,
    "upstream_time": "0.006"
  },
  "client": {
    "ip": "client IP",
    "user_agent": "browser/version",
    "referer": "",
    "forwarded_for": ""
  },
  "upstream": {
    "addr": "10.10.0.2:30400",
    "status": "200"
  },
  "routing": {
    "backend": "jp_backend|sea_backend",
    "target_region": "jp|sea"
  },
  "traceId": "" 
}

Verification

  • ✅ Deployed to both Gateway VMs (gateway1 and gateway2)
  • ✅ OpenResty containers restarted successfully
  • ✅ JSON logs verified working via curl requests
  • ✅ No warnings in error logs

Example Log Output

{"timestamp":"2025-12-22T05:40:16+00:00","service":"m3w-gateway","gateway":"gateway1","type":"access","http":{"method":"GET","path":"/api/health","query":"","status":200,"bytes_sent":79,"request_time":0.006,"upstream_time":"0.006"},"client":{"ip":"221.225.23.135","user_agent":"curl/8.16.0","referer":"","forwarded_for":""},"upstream":{"addr":"10.10.0.2:30400","status":"200"},"routing":{"backend":"jp_backend","target_region":"jp"},"traceId":""}

Part of Epic

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions