Distributed tracing
Following one request across services to see where the time went, and why a single-service app usually does not need it yet.
A receipt stamped at every desk, showing where the customer waited longest.
That is a trace. One customer walks through several desks. Each desk stamps the time on the same receipt. Afterwards you can see which desk took the longest.
How it works
Traces answer: where did this request spend its time, across every service it touched?
Request
↓
API gateway 10 ms
↓
Auth service 20 ms
↓
User service 50 ms
↓
Database 100 ms
Total: 180 ms
Each step is a span with a start, a duration and a parent. A trace is the tree of spans for one request. To link spans across services, the first service creates a trace id and passes it on, usually in a request header. Every later service records its spans under the same id.
A tracing system collects the spans and draws them as a timeline. The slow bar is the place to look.
trace a81f
├── gateway ██ 10 ms
├── auth ████ 20 ms
└── user service ████████████ 50 ms
└── database ████████████████████████ 100 ms
What it adds beyond logs and metrics
A metric says that latency is high. A log says what one service did. Neither shows the path of one request through several services. With a trace you can see that the user service was fast and its database call was slow, which neither a single service's logs nor its metrics can reveal.
Instrumenting
OpenTelemetry is the common standard for creating spans and exporting them to a backend. Most web frameworks and database clients have ready-made instrumentation, so the first useful traces often need very little code. Add custom spans around the calls you suspect, such as a payment provider or a model request.
Sampling
Recording every trace is expensive. Most systems sample, keeping a fraction of normal requests and every slow or failed one. Decide the policy early, because a trace you did not keep cannot be investigated afterwards.
Carrying the id further
The trace id also belongs in logs. When every log line carries it, you can jump from a slow trace to the exact log lines for that request, which is the quickest route from where to why.
Reading a trace
Start with the longest span, then ask whether it is slow by itself or waiting on a child. A parent that is much longer than the sum of its children is spending time in its own code. Many short child spans in a row, such as a database call repeated for each item in a list, point to a loop that should be one query. Gaps between spans can mean queueing, a cold start or a network hop that was not instrumented.
Where I used it
I haven't shipped this in a public project yet.
My case studies do not describe tracing, so I will not claim it. The projects I build are mostly modular monoliths, where the request stays in one process, and that is the situation the next section says does not need tracing yet.
Trade-offs
- Not for a single-service monolith. When the request path never leaves one service, logs and metrics are usually enough. Tracing earns its place once the path crosses service boundaries, such as a gateway in front of several backends.
- Instrumentation work. Every service on the path must pass the trace id along. One service that drops it breaks the chain.
- Overhead and cost. Spans add a little latency and a lot of data. Sampling keeps the cost in check.
- Sensitive data. Span attributes can capture request details. Keep personal data out of them.
- A tool to learn. Reading traces takes practice, and a team that never looks at them wastes the setup.
A useful rule: reach for tracing when a slow request cannot be explained by one service's logs and metrics, because the slow part is probably a call to somewhere else.