Microservices Distributed Tracing
One tap on a phone can trigger calls across a dozen services. When the screen feels slow, logs from each service show only a piece of the story. Distributed tracing connects those pieces into a single timeline that follows the request from start to finish.
Layman Explanation
A courier parcel carries a tracking number. Each depot scans the number and records the time. Anyone can open the tracking page and see every stop, including the stop where the parcel waited three days. Distributed tracing gives every request a tracking number and records every service it visits.
Key Terms
Trace
A trace is the complete journey of one request through the system. Every trace carries a unique trace id.
Span
A span is one unit of work inside a trace, such as "Payments service charges card." Each span records a start time, a duration, and a few labels.
Parent and Child Spans
A span that calls another service becomes the parent of the new span. Parent-child links form a tree that shows who waited for whom.
Context Propagation
Context propagation passes the trace id from service to service, usually in an HTTP header. Without it, each service would start an unrelated trace.
Diagram: One Trace, Four Spans
Trace id: 7f3a91
Gateway |==========================================| 480 ms
Orders |=====================================| 400 ms
Inventory |====| 60 ms
Payments |==========================| 290 ms
Bank API |======================| 230 ms
0 100 200 300 400 500 ms
The timeline reveals the culprit at a glance. The bank call consumes almost half of the total time. A developer who read only the gateway log would see "480 ms" and learn nothing more.
How Propagation Works
Gateway --> Orders header: traceparent: 00-7f3a91...-a1b2c3-01 Orders --> Payments header: traceparent: 00-7f3a91...-d4e5f6-01 (same trace id, new span id)
The W3C Trace Context standard defines the traceparent header, so tools from different vendors understand each other.
Tools and Standards
| Name | Role |
|---|---|
| OpenTelemetry | Open standard and libraries that create traces, metrics, and logs |
| Jaeger | Open source backend that stores and shows traces |
| Zipkin | Open source tracing system with a simple interface |
Applications emit spans through OpenTelemetry, a collector gathers them, and a backend such as Jaeger displays them.
Adding Useful Details
Attach labels to spans, such as order id, region, or payment method. Engineers search by those labels to find traces for a single complaint. Keep personal data like card numbers and passwords out of labels.
Sampling
Recording every trace at high traffic creates huge storage bills. Sampling keeps a fraction.
- Head sampling decides at the start, for example keep one request in a hundred.
- Tail sampling decides after the trace ends and keeps slow or failed traces.
Tail sampling saves the interesting traces and discards the boring ones.
Connecting Traces with Logs
Write the trace id into every log line. A search for that id across all services returns the matching log lines in order. Traces show where the time went, and logs explain why.
Questions Tracing Answers
- Which service slows down checkout?
- Which dependency fails most often?
- How many calls does one request make?
- Did a release change latency?
Key Takeaways
Tracing turns scattered logs into one readable timeline. Propagate the trace id everywhere, use OpenTelemetry for consistency, sample wisely, and link traces to logs.
