Logging, metrics and tracing
One slow checkout, seen three ways: the lines each service wrote, a number charted every minute, and the request’s path drawn as bars. Then how a trace ID ties them together.
In 30 seconds
Software running on servers is invisible unless it reports on itself. It does so with three kinds of record.
Logs are lines describing single events. Metrics are numbers measured over and over, such as how long requests take. Traces follow one request through every service and show how long each part took.
A trace ID, passed along with the request and stamped on every record, links them. Metrics reveal that something is wrong, traces show where, and logs explain why.
Logging, metrics and tracing, step by step
All figures are invented. Real trace IDs are 32 hex characters, shortened here to a1f3; the standard header that carries them is called traceparent (W3C Trace Context).
- Step 1: A slow checkout. A customer says the shop’s checkout is slow. Their request passed through three services, API, orders and payments, and payments called an outside card company. From outside, all anyone can measure is that the page took 2300 ms. Three kinds of record let engineers see inside a running system: logs, metrics and traces.
- Step 2: Logs: what happened. A log is a list of events. Whenever something notable happens, a service writes one line: the time, how serious it is, and the details. Five lines tell the story of this checkout, including a warning that the card company timed out. Logs hold the most detail, but each service writes its own and a busy system writes millions.
- Step 3: Metrics: numbers over time. A metric is a number measured again and again, such as requests per minute.This one is p95 checkout time: 95 of every 100 checkouts in that minute were faster. It sat near 300 ms, then jumped to 2100 ms at 10:41. Metrics are cheap to keep, so they suit graphs and alarms, but they hold no detail about any single request.
- Step 4: Traces: one request’s path. A trace follows one request through every service. Each piece of work is a span: a bar with a start and a length, nested inside the span that caused it. Reading down the bars shows where the 2300 ms went. Saving the order took 40 ms. The first call to the card company took 2000 ms before timing out, and the retry took 110.
- Step 5: The trace ID. What joins the records together is a trace ID. The first service to receive a request invents a random ID and passes it along with every call it makes, normally in a request header. Each service stamps that ID on its spans and on every log line it writes. One search for the ID then gathers this request’s lines from all services.
- Step 6: Following one incident. The three are used in turn. An alarm on the metric says checkouts became slow at 10:41. An engineer opens the trace of one slow checkout and sees the time going into the call to the card company. Searching the logs for that trace ID finds the warning: the provider timed out and the call was retried. The shop’s own code is fine.
- Step 7: Which one for which question. Each signal answers a different question. Metrics answer “how many, how often, since when” for the whole system. Traces answer “where did this request spend its time”. Logs answer “what exactly happened here”. Each has a price: metrics lose the detail, traces are often kept for only a sample of requests, and logs are bulky.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Distributed tracingFollowing one request through many services to find the slow hop. Intermediate
- ObservabilityWorking out what a system is doing inside from the logs, metrics and traces it emits. Beginner
- SLIs, SLOs and SLAsWhat you measure, what you aim for and what you promise, with an error budget worked out. Beginner
Spotted a mistake on this page?