Course map

Topic 201 / Core

Observability: logs, metrics and distributed tracing (OpenTelemetry as the example)

Chapter 12.4 · Observability

What and why

A chaos experiment (→ 200), or any real incident, only reveals something useful if the system can be questioned about what it did — observability is the ability to ask new questions of a running system after the fact, not just check a fixed set of pre-built dashboards. Three signals make that possible: logs (discrete, structured events with context), metrics (cheap, aggregated numeric time series), and traces (one request's path across every service it touched, with per-step timing). OpenTelemetry is the vendor-neutral standard this topic uses as the example for producing and exporting all three. Builds on: → 200 (resilience and chaos testing — the signals this topic produces are what a chaos experiment is read through).

Diagram

Signal Captures Cost Answers
Logs a discrete event, with structured fields and a correlation ID highest per-event cost "what exactly happened on this one request?"
Metrics an aggregated number over time (count, histogram) cheapest, always-on "what's the trend, and is it within range?"
Traces one request's path across services, as timed spans moderate, needs propagation "which hop, across services, is slow?"

How it works

  1. Emit structured, leveled logs with a correlation ID attached to every request, so related log lines across a system can be found together instead of grepped by eye.
  2. Emit metrics as cheap, pre-aggregated counters and histograms — designed to be queried over time, not to describe any single event.
  3. Emit traces as spans: one span per unit of work, with a start time, duration, and a parent span ID — a single request's full set of spans across every service it touched is one trace.
  4. Context propagation carries the trace ID (and parent span ID) forward on every outgoing call a service makes, so each downstream service's spans can be stitched back into the same trace — without this, tracing stops at the first service boundary.
  5. OpenTelemetry standardizes how all three signals are produced and exported, decoupling instrumentation (what a service emits) from the backend that stores and displays it — switch backends without re-instrumenting the code.

Practical example

(illustrative). A request crosses four services and takes 2 seconds. Without tracing, diagnosing the slow hop means correlating four separate services' logs by eyeballed timestamps. With a propagated trace ID and OpenTelemetry spans, one trace shows: service A 50 ms, service B 100 ms, service C 1,800 ms, service D 50 ms — the single trace identifies service C as responsible for 1,800 of the 2,000 ms (90%) without touching a single log file.

When to use it

  • Use instrumentation from day one at every service boundary — adding it after the fact means retrofitting trace-context propagation through code that was never written to carry it.
  • Avoid logging at high verbosity, unfiltered, in production — the cost and noise of every detail, forever, defeats the purpose; use levels and sampling deliberately.
  • Failure scenarios: a trace with a gap because one service in the chain doesn't propagate the context header — the trace simply stops there, and the real bottleneck might be hiding past the gap; a metric with an unbounded label (a raw user ID as a label value) causing a cardinality explosion that blows up storage cost; trying to answer a cross-service latency question from logs alone, which was never designed to answer it.
  • Simpler alternative: for a single-service system with no downstream calls, structured logs plus a handful of metrics may be the entire observability stack needed — tracing earns its cost once a request actually crosses service boundaries.

Practice

(mechanism kind — instrument a small script; 20–30 min, no lab service required beyond what the script itself calls). Write a small script with three function calls, one of which calls another in turn (so there's a nested chain). Instrument it to emit: (1) a structured log line per function entry/exit with a shared correlation ID, (2) a counter metric for calls and a histogram for duration, (3) a simple span per function with start/end time and a parent-span reference (OpenTelemetry's SDK, or a hand-rolled equivalent, both acceptable). Done when you can print one request's complete span tree, showing which nested call took the most time.

Key takeaway

Logs, metrics, and traces each answer a different question about a running system — "what happened," "what's the trend," and "which hop was slow" — and context propagation is what lets traces cross service boundaries at all. Done when you can: build a request's span tree by hand from a small instrumented program and identify which call dominates its latency. Used next in → 202 (monitoring and alerting, built on the metrics this topic produces), → 205 (incident management, where logs and traces are read live during a real incident).

References

OpenTelemetry: Signals — see references.md.

Source directory

Search the guide