Skip to content

Lesson 8 of 9 · 24m

Logs, Metrics, and the Signals That Matter in an Incident

Shape access logs for answers, monitor saturation and errors, and use request tracing evidence to separate client, proxy, and upstream failure.

When production is down, nobody needs a dashboard with eighty graphs and no interpretation. You need to answer five questions quickly: are clients reaching us, are we accepting connections, are upstreams slow, are errors concentrated, and did anything change. Good logging is designed backward from those questions.

Access logs should be evidence

The default combined format is nostalgic, not operational. Add timing and upstream fields:

log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
                    'rt=$request_time uct=$upstream_connect_time '
                    'uht=$upstream_header_time urt=$upstream_response_time '
                    'upstream=$upstream_addr cache=$upstream_cache_status';

access_log /var/log/nginx/access.log main_ext buffer=64k flush=5s;

request_time is total proxy-visible time. Upstream timings tell you where that time went. If urt is high and upstream connect time is low, the application is slow after connection. If connect time is high, network or backlog is involved. These distinctions are incident gold.

Apache can expose equivalent timing

LogFormat "%a %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %D %I %O" timing
CustomLog /var/log/apache2/access.log timing

%D is microseconds to serve the request. %I and %O are bytes in and out. It lacks Nginx's built-in upstream timing breakdown unless you add application headers or use additional modules, so instrument the app when Apache proxies meaningful workloads.

Status codes need context

A 502 means the proxy got an invalid or absent response from upstream, not merely that 'the app is down.' A 499 in Nginx means the client disconnected before the server responded. A spike in 499 during deploys can mean slow responses and impatient browsers, or a proxy upstream timeout mismatch. A spike in 404 after a migration means routing or SEO trouble. Aggregate counts hide the URL and upstream concentration you need.

Metrics beat logs for saturation

Monitor at least:

active connections / workers
accepts, handled, requests
waiting, reading, writing states
4xx/5xx ratio by route class
upstream response time percentiles
TLS handshake failures
file descriptor usage
disk usage for logs and cache

Nginx stub status or commercial/proxy exporters can expose connection state. Apache mod_status provides similar worker visibility when protected properly. Never expose server status publicly because it reveals operational details and sometimes request paths.

Structured logs reduce parsing pain

JSON logs are easier to ship, but only if field names are stable and high-cardinality fields are deliberate. Do not log full query strings containing tokens, reset links, or personal data. Hash or truncate client identifiers when retention policy requires. Logs are sensitive infrastructure, not a bottomless debugging junk drawer.

Correlate the request across layers

Pass or generate a request ID:

proxy_set_header X-Request-ID $request_id;

Then log it in the application and include it in error responses where safe. When a user reports 'error at 14:03', a request ID turns rumor into evidence. If both proxy and app already have IDs, preserve the incoming one only when the source is trusted; otherwise generate a clean edge ID.

Field notes

  • Buffered logging reduces I/O but can delay the exact lines you need during a crash; tune flush intervals consciously.
  • Log rotation failures fill disks silently until everything fails loudly. Test logrotate or your journal pipeline.
  • Percentiles matter more than averages. p95 upstream latency often reveals overload long before mean latency moves.
  • Access logs are not audit logs. Security-sensitive administrative actions need immutable, structured application events.

Was this lesson helpful?

Sign in to rate