Distributed patterns and operations

Observability: logs, metrics and traces

Know what each signal answers and what it costs, measure services with RED and USE, alert on SLOs, and use the three together to find the cause of an incident.

Lesson14 min read
Video walkthrough: not recorded yet. The lesson below is complete without it.

The problem

A team splits a monolith into 30 services. In the monolith, a slow request was easy to debug: one process, one log file, one stack trace. Now a checkout request passes through a gateway, six services, two databases, a cache and a queue.

One Tuesday the checkout p99 goes from 400 ms to 3 seconds. Every CPU dashboard is green. Each team says its own service looks fine. Engineers log in to hosts and search log files, but the lines for one request are spread across 40 machines with nothing to join them. Two hours later someone finds that a connection pool in one service became too small after a traffic shift.

The team had monitoring, which answers questions chosen in advance, such as “is CPU high?”. What it lacked was observability: the ability to answer new questions about the system from the data it already emits, without shipping new code first. That data comes as three signals, and each answers a different question at a different cost.

The three signals

Signal Answers Cost grows with
Metrics Is something wrong, how badly, since when? Number of time series
Traces Where did this request spend its time? Traffic, reduced by sampling
Logs What exactly happened in this event? Traffic

Logs

A log is a timestamped record of one event. Write structured logs: key and value fields, usually JSON, not free text. Then you can filter and count without fragile text parsing.

{"ts":"2026-03-03T14:02:11Z","level":"error","service":"orders",
 "trace_id":"4bf92f3577b34da6a3ce929d0e0e4736","route":"POST /orders",
 "status":503,"duration_ms":2610,"msg":"connection pool exhausted"}

Logs are the most detailed signal and the most expensive. At 10,000 requests per second and about 1 KB per line, one line per request is roughly 864 GB a day before replication and indexing. So log at levels, sample routine successes, keep errors, and set retention by value. Never log passwords, tokens or card numbers.

Metrics

A metric is a number measured over time and stored as a time series. Its cost does not depend on traffic: a counter that has counted a billion requests takes the same space as one that has counted ten.

  • Counter. Only goes up, such as total requests. You graph its rate.
  • Gauge. Goes up and down, such as queue depth or memory in use.
  • Histogram. Counts observations into buckets, such as request durations. It is what you compute percentiles from.

Two checklists say what to measure:

  • RED, for every request-driven service: rate, errors, duration. It was proposed by Tom Wilkie.
  • USE, for every resource such as a CPU, disk, connection pool or queue: utilisation, saturation (work that is waiting), errors. It comes from Brendan Gregg.

RED tells you users are hurting. USE tells you which resource is the reason. Google’s four golden signals (latency, traffic, errors, saturation) cover nearly the same ground.

Percentiles, not averages

Take 99 requests at 10 ms and one at 5 seconds. The mean is about 60 ms, which looks healthy, yet one user in 100 waited 5 seconds. Tails compound, too: if a page makes 100 backend calls and each has a 1% chance of being slow, about 63% of page loads hit at least one slow call.

Report p50, p95 and p99. Percentiles cannot be averaged across instances, because a quiet instance with a bad p99 would count as much as a busy one. Sum the histogram buckets from all instances and compute the percentile from the total. The result is an estimate whose accuracy depends on where the bucket boundaries sit.

Cardinality

Every distinct combination of label values is a separate time series. A request counter labelled by 20 routes, 5 status classes and 50 instances is 5,000 series, which is fine. Add a user_id label with a million users and the same metric could need billions.

Keep metric labels to small, bounded sets: route template, status class, region, version. Put user ids, order ids and raw URLs in logs and traces, which are stored per event and built for that detail.

Distributed tracing

A trace records one request’s path through the system. It is a tree of spans. Each span is one operation in one service, with a start time, a duration, attributes and the id of its parent. All spans in a trace share a trace id. Google described the approach in its Dapper paper in 2010.

One trace across three services7 components: Browser, API gateway, Order service, Payment service, Orders database, OTel Collector, Tracing backend. Connections: Browser to API gateway; API gateway to Order service (traceparent); Order service to Payment service (traceparent); Order service to Orders database; API gateway to OTel Collector (spans); Order service to OTel Collector; Payment service to OTel Collector; OTel Collector to Tracing backend.BrowserAPI gatewayspan A, the rootOrder servicespan B, parent APayment servicespan C, parent BOrders databasespan D, parent BOTel Collectorbatch, sample, exportTracing backendjoins spans by trace idtraceparenttraceparentspans
One trace across three services

Context propagation is what holds a trace together. Each service reads the incoming trace context, starts a child span, and sends the updated context on every outgoing call. The W3C Trace Context standard defines the traceparent header for this: a version, a 16-byte trace id, the 8-byte parent span id and flags. The context must also cross thread pools and travel in message headers through queues. If one service drops it, the trace splits in two.

Sampling controls cost. Storing every span at high traffic is rarely affordable.

  • Head-based sampling decides at the first service, for example keep 1%, and carries the decision in the trace flags so every service agrees. It is cheap, but it decides before anyone knows whether the request will fail.
  • Tail-based sampling decides after the trace finishes, so it can keep every error and slow trace plus a small share of the rest. The cost is a collector tier that buffers all spans of every trace in flight, and all spans of one trace must reach the same collector.

OpenTelemetry is the vendor-neutral standard for producing all three signals. It was formed in 2019 by merging OpenTracing and OpenCensus, and it is a CNCF project. It provides APIs and SDKs for most languages, automatic instrumentation for common frameworks, a wire protocol (OTLP), and the Collector, a pipeline that receives, batches, samples, redacts and exports telemetry.

Alerting on SLOs

Page people for symptoms that users feel, not for causes. “CPU above 80%” fires on healthy busy machines and stays silent during many real outages.

Start with a service level indicator (SLI): the share of requests that were good, for example non-5xx and faster than 300 ms, measured as close to the user as you can, such as at the load balancer. Set a service level objective (SLO) on it, say 99.9% over 30 days. The remaining 0.1% is the error budget, equal to about 43 minutes of full outage. Availability and reliability covers the arithmetic.

Then alert on burn rate, the speed at which the budget is being spent. A burn rate of 1 uses it up exactly at the end of the period. Google’s SRE Workbook suggests paging at 14.4 over one hour, which is 2% of the month’s budget in an hour, confirmed by a 5 minute window so the alert clears quickly after a fix. Slower burns open a ticket.

Debugging with all three

Replay the Tuesday incident with the signals in place.

  1. The alert fires on the checkout SLO burn rate. Users are affected, and you know when it started.
  2. Metrics narrow it down. RED dashboards for each service show that duration rose in the order service only, starting at 14:00, with no deploy at that time.3. Traces show where. Slow traces all have a 2.6 second span named “acquire database connection” in the order service. The database query span after it is fast. Some systems attach example trace ids, called exemplars, to histogram buckets, so you can jump from a latency graph to a matching trace.
  3. Logs say why. Filtering the order service’s logs by one trace id shows “connection pool exhausted”. The pool’s USE metrics confirm it: utilisation at 100% and a growing wait queue.
  4. Fix and verify. Raise the pool size, watch p99 and the burn rate return to normal.

Each signal hands over to the next through shared fields: service name, route and trace id.

When to choose it and when not to

  • Metrics: always, for every service and resource. They are cheap and they drive alerts.
  • Structured logs: always, with a request or trace id in every line, and with sampling and retention sized to your budget.
  • Tracing: worth it once requests cross more than a few services. A monolith with one database gets most of the value from a request id in its logs and a profiler.
  • Tail-based sampling: when you can afford to run the collector tier. Start with head-based sampling.

Avoid high-cardinality metric labels, logging full request bodies, and pages on causes.

In the interview

Mention observability near the end of a design, in a sentence or two, and in depth when asked “how would you know this is broken?” or “how would you debug a latency regression?”.

A strong answer in a minute: every service emits RED metrics and every resource USE metrics, with latency as histograms so p99 can be aggregated. Logs are structured and carry the trace id. Trace context is propagated on every call and through queues, with sampling that keeps errors and slow requests. Alerts page on SLO burn rate measured at the edge. Then describe the path from alert to metric to trace to log, as above.

Key takeaways

  • Metrics tell you that something is wrong and since when, traces tell you where the time went, and logs tell you why.
  • Metric cost grows with cardinality, while log and trace cost grows with traffic. Keep unbounded values such as user ids out of metric labels.
  • Measure every service with RED (rate, errors, duration) and every resource with USE (utilisation, saturation, errors).
  • Use percentiles, not averages, and aggregate histograms across instances. Percentiles themselves cannot be averaged.
  • A trace is a tree of spans joined by a trace id that every service must propagate, including through queues.
  • Page on symptoms users feel, expressed as SLO burn rate, and keep cause-based alerts for tickets and dashboards.

Check yourself

1. A service handles 99 requests in 10 ms each and 1 request in 5 seconds. What do the mean and the p99 tell you?
2. A team adds a user_id label to its request counter. The product has 2 million users. What is the likely result?
3. Ten instances each report their own p99 latency. How do you get the p99 for the whole service?
4. Why might a team choose tail-based sampling over head-based sampling for traces?
5. Which alert is the best candidate to page an engineer at night?

0 of 5 answered

Flashcards

These 6 cards are in your review deck. Tap a card to see the answer.

What question does each of the three signals answer?

Metrics show that something is wrong, how badly and since when. Traces show where in the call path the time or the error is. Logs show what exactly happened in one event.

What are RED and USE?

RED is rate, errors and duration, for request-driven services. USE is utilisation, saturation and errors, for resources such as CPU, disks, pools and queues.

What is metric cardinality?

The number of distinct time series, which is the number of unique label value combinations. It drives the cost of a metrics system, so labels must come from small, bounded sets.

What does the W3C traceparent header carry?

A version, a 16-byte trace id, the 8-byte id of the parent span, and flags including whether the trace is sampled.

Head-based versus tail-based sampling?

Head-based decides at the root and is cheap but blind. Tail-based decides after the trace completes, so it can keep errors and slow traces, at the cost of buffering every span first.

What is a burn rate alert?

An alert on how fast the error budget is being spent. Burn rate 1 uses the budget exactly over the SLO period. A common page is 14.4 over one hour, which is 2% of a 30-day budget.

Review the deck