envoy-guide
Module 4 · Observability·10 min·intermediate

Access logs

Customize Envoy's per-request log format.

Why Envoy's logs are special

Most reverse proxies log a single line per request and call it a day. Envoy gives you that, plus over 100 command operators you can compose into your own format — and the option to emit structured JSON so your log pipeline can index every field individually.

The result: when you're debugging "why is the p99 spiking?" you can answer it from logs alone, without touching the application.

Plain text format

Add an access_log block to the HTTP Connection Manager:

YAML
          access_log:
          - name: envoy.access_loggers.stdout
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
              log_format:
                text_format_source:
                  inline_string: |
                    [%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" %RESPONSE_CODE% %DURATION%ms upstream=%UPSTREAM_HOST% flags=%RESPONSE_FLAGS%

The most useful operators:

OperatorWhat it shows
%START_TIME%When Envoy received the request
%REQ(:METHOD)%HTTP method (GET, POST, …)
%REQ(:PATH)%Path including query string
%RESPONSE_CODE%HTTP status returned to the client
%DURATION%Total request time in ms
%UPSTREAM_HOST%Which backend served it (priceless for LB debugging)
%RESPONSE_FLAGS%Two-letter codes: UH, UT, UO, URX, etc.
%REQ(USER-AGENT)%Any request header — substitute the name
%RESP(X-CACHE)%Any response header

JSON format (the one you actually want in production)

YAML
          access_log:
          - name: envoy.access_loggers.stdout
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
              log_format:
                json_format:
                  ts: "%START_TIME%"
                  method: "%REQ(:METHOD)%"
                  path: "%REQ(:PATH)%"
                  status: "%RESPONSE_CODE%"
                  duration_ms: "%DURATION%"
                  upstream: "%UPSTREAM_HOST%"
                  flags: "%RESPONSE_FLAGS%"
                  ua: "%REQ(USER-AGENT)%"
                  bytes_out: "%BYTES_SENT%"

One JSON object per request. Loki, Datadog, Splunk — all happy.

Response flags are oxygen

%RESPONSE_FLAGS% deserves to be in every format you ever write. Two letters that tell you exactly why a request looked weird: UH = no healthy upstream, UO = circuit-breaker overflow, UT = upstream timeout, URX = retry limit exceeded, NR = no route. When something fails, this is where you start.

Hands-on: structured logs into jq

Hands-on · Log it, slice it

Use the Lesson 5 hello-world Envoy that proxies to httpbin.org. Add the JSON access_log block above. Restart.

Send a few requests:

Shell
for i in 1 2 3 4 5; do curl -s -o /dev/null localhost:10000/get; done
curl -s -o /dev/null localhost:10000/status/503
curl -s -o /dev/null localhost:10000/delay/2

Pipe Envoy's stdout through jq in another terminal:

Shell
docker logs -f <envoy-container> 2>&1 | jq 'select(.status >= 400)'

You should see the 503 (and possibly the 504 from the delay if your timeout fires). Now compute a quick p95 over your last batch:

Shell
docker logs <envoy-container> 2>&1 \
  | jq -r '.duration_ms // empty' \
  | sort -n \
  | awk 'BEGIN{c=0} {a[c++]=$1} END{print a[int(c*0.95)]}'

That's a one-liner p95 with no monitoring stack at all.

Headers can be huge

Logging unbounded headers is how you accidentally write a 10MB log line. Use the truncation form: %REQ(USER-AGENT?MAX:128)% caps that field at 128 chars. Apply liberally to any user-controlled header.

Real-world example · A B2B SaaS investigating a flaky tenant
Problem
One enterprise customer reports intermittent 502s. The team can't reproduce it, the app logs nothing weird, and APM samples don't show the failures.
Envoy solution
They added %RESPONSE_FLAGS% and %REQ(X-TENANT-ID)% to the Envoy JSON access log, then queried flags=="UH" AND tenant=="acme-corp". Result: the failures were UH (no healthy upstream) clustered around deploys to one specific shard. Root cause turned out to be a too-aggressive readiness probe. Without Envoy's flags + per-tenant tagging, this would have been a multi-day incident.

Up next

Logs are great for individual requests; metrics are great for the shape of the whole system. Lesson 13 turns on Prometheus.

Quick check

Which command formatter shows the upstream host that handled the request?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3