Observability & Operations

Logging & Distributed Tracing

Logs capture discrete events with full detail; traces stitch a single request's path across every service it touched into one timeline — together they answer "what happened" and "where did the time go" that metrics alone cannot.
  • Structured logging (key-value/JSON, not free-form text) is what makes logs queryable at scale — grep does not scale past one machine
  • A trace is a tree of spans; each span records one unit of work with a start time, duration, and parent span — the whole tree reconstructs the request's path
  • Context propagation — a trace ID and span ID passed through every hop, in headers or message metadata — is what lets independently-deployed services contribute spans to the same trace
  • Log correlation IDs, often the trace ID itself, let you jump from "this span was slow" to the exact log lines from that service during that span
  • Sampling is unavoidable at scale — tracing every request is often too expensive, so systems sample a percentage, or trace every erroring/slow request unconditionally ("tail-based sampling")
  • Metrics tell you something is wrong; traces tell you where; logs tell you why — they are complementary, not substitutes for each other
One request, one trace, many spans
Logs vs metrics vs traces
LogsMetricsTraces
Question answeredwhat exactly happenedis something wrong, in aggregatewhere did the time go
Granularitysingle eventaggregated over timesingle request across services
Storage costhigh — full detaillow — pre-aggregatedhigh, mitigated by sampling
Propagating trace context across a call
Span span = tracer.spanBuilder("chargeCard").setParent(Context.current()).startSpan();
try (Scope scope = span.makeCurrent()) {
    httpClient.send(request.header("traceparent", span.getSpanContext().toTraceparent()));
} finally {
    span.end();
}
Sources
  • Site Reliability Engineering: How Google Runs Production SystemsCh. 6 — Monitoring Distributed Systems
  • Release It! Design and Deploy Production-Ready Software (2nd ed.)Ch. — Logging and Transparency