TL;DR
A single user request can pass through dozens of programs on many machines before it returns. Distributed tracing gives every request a trace ID and makes each program record a span: a timed piece of work with its start, its end, and the span that caused it. Put together, the spans form a tree that shows where the request spent its time and which service was slow. It solves the problem that no single service's logs show the whole path of a request.
Builds on: Distributed System, Remote Procedure Call, Hypertext Transfer Protocol · Leads to: the Dapper paper
Where it's used
Web search. A search query at a large search engine fans out to many backend services, such as spelling correction, ads and index servers. Google built Dapper during the 2000s to answer the question "why was this query slow?" for exactly this kind of request, which may touch thousands of machines.
Online shops and payments. A "buy" click usually calls an order service, which calls a payment service and an inventory service, which in turn query a database. When checkout takes two seconds, a trace shows whether the time went to the payment provider's network call, to a slow database query, or to the shop's own code.
Ride-hailing and streaming apps. A request to show nearby drivers or to start a video can cross a dozen internal services. Tracing lets an operator pick one slow request out of millions and see the full path, rather than guessing from averages.
How it works
Follow one request through three services. A shopper clicks "buy" at time 0 milliseconds, and the request goes to a frontend service, then to an order service, which calls a payment service and queries a database.
-
The frontend starts the trace. It creates a random trace ID, here called
T1, and a span for its own work, called span A. Span A records a start time of 0 ms. The trace ID is what ties every later record to this request. -
The frontend calls the order service. The call is an RPC over HTTP. The frontend adds the trace ID and its own span ID, A, as a header on the request. This header is called the context, and passing it along is called context propagation. Check: the order service now knows both
T1and that its caller is A. -
The order service starts its span. It creates span B with a start of 5 ms, and records A as its parent. Span B covers all the order service's work for this request.
-
The order service queries the database. It creates span D from 10 ms to 30 ms, with B as its parent. Check: D lasts 20 ms.
-
The order service calls the payment service. The same way as step 2, it passes
T1and B as the parent. The payment service records span C from 40 ms to 80 ms, with B as its parent. Check: C lasts 40 ms. -
Each service reports its spans. Each program sends its finished spans to a collector, which may be a separate process. The collector groups spans by trace ID and rebuilds the tree from the parent links. The spans can arrive in any order, because the tree is rebuilt from the IDs, not from arrival times.
-
Read the tree. Span A ends at 100 ms, and it is the root. Span B ends at 90 ms. Its children are D, lasting 20 ms, and C, lasting 40 ms. Span B's own time is what is left over: ms. Check: 25 ms of the order service's work is not spent waiting on the database or the payment service. The payment call, at 40 ms, is the largest single cost.
sequenceDiagram participant F as Frontend (span A) participant O as Order service (span B) participant D as Database (span D) participant P as Payment (span C) F->>O: request with trace T1, parent A O->>D: query, parent B (10 to 30 ms) O->>P: charge, parent B (40 to 80 ms) P-->>O: reply O-->>F: reply (B ends at 90 ms, A at 100 ms)
The math
A trace is a rooted tree of spans. Each span has a trace ID that is the same for the whole tree, its own ID, the ID of its parent, a start time and an end time.
A span has a start time and an end time , with , and its duration is . Its parent is the span that called it, or none for the root. A trace is the set of spans that share one trace ID, linked by parent pointers into a tree whose root is the first span.
Step 1 in symbols
Span A starts at and ends at , so ms. Span A is the root, so it has no parent. The trace ID is shared by all spans in this request.
Step 2 in symbols
The context passed from parent to child is the pair (trace ID, parent span ID). When A calls B, B stores the pair . Context propagation means that every call copies the trace ID unchanged and sets the parent to the caller's own span ID.
Step 3 in symbols
For the order service, span B has , , so ms. Its parent is , so .
Step 4 in symbols
Span D is a child of B with and , so ms. Its parent is B.
Step 5 in symbols
Span C is a child of B with and , so ms.
Step 6 in symbols
The collector recovers the tree without relying on arrival order. For each span it looks up the parent ID, and it attaches each span to the parent with that ID. The result is a function from each span to its parent that defines a tree. Since the spans are linked by IDs, a delayed or reordered report changes nothing.
Step 7 in symbols
The self time of a span is its duration minus the time covered by its children. When the children do not overlap, it is
For B, ms. For A, ms, which is the time the frontend spent before and after waiting on the order service, plus the network time on the way back.
The critical path is the chain of spans that determines the end time of the root. Here it is , because C ends last among B's children.
Worked examples
A client calls a server. The client's span X runs from 0 to 50 ms. The server's span Y runs from 10 to 40 ms, with X as its parent.
- The duration of X is ms.
- The duration of Y is ms.
- The self time of X is ms. This is 10 ms of network time before the server starts and 10 ms after it replies.
- Check: , which equals the total.
- Interpretation: if a user sees 50 ms, the trace says that 30 ms are on the server and 20 ms are on the way there and back, so the server is not the only place to look.
Use the trace from How it works. Span A runs 0 to 100 ms, span B runs 5 to 90 ms, span D runs 10 to 30 ms and span C runs 40 to 80 ms.
- The total request time is ms.
- The order service's time is ms.
- The database time is ms.
- The payment time is ms.
- The self time of the order service is ms.
- The critical path is A, B, C, because C ends last among B's children. Payment accounts for percent of the total request time.
- Interpretation: the largest single cost is the payment call. Speeding up the database would not shorten this request, because D finishes at 30 ms, before the payment call starts at 40 ms. The trace points the work at the payment service.
An online shop's checkout takes 2,000 ms at the 99th percentile. A trace of one slow request shows the following spans, all in milliseconds: frontend 2,000 in total; order service 1,850; inventory check 60; payment provider call 1,700; database write 40. The order service's own children are the inventory check, the payment call and the database write, and they do not overlap.
- The order service's self time is ms.
- The payment call takes of the total, or 85 percent.
- The frontend's self time is ms, which is the network and the page rendering on the way out.
- The inventory and database spans together take 100 ms, which is 5 percent of the total.
- Interpretation: in this request, most of the delay is in the external payment provider, not in the shop's code or its database. A team that only looked at average CPU use on its own servers would have missed this. The useful next step is to compare payment-call durations across many traces, to see whether the provider is slow in general or only for some requests. Try it: a request lasts 800 ms. Its order service span lasts 700 ms, and the payment span inside it lasts 500 ms. There is no other child span. What is the order service's self time, and what share of the total does the payment call take? Answer: The order service's self time is ms. The payment call takes , or 62.5 percent, of the total request time.
Common mistakes and tradeoffs
It is tempting to think that a trace records the exact order of events across machines. In fact, each machine reads its own clock, and clocks drift apart. Two spans on different machines can appear to start in the wrong order, or a child can seem to start before its parent. The tree is still correct because it is built from IDs, but timing comparisons across machines carry an error equal to the clock disagreement. Clock Synchronization explains how that error is bounded, and Happened-Before Relation explains which orderings a trace can actually prove.
It is tempting to think that a trace shows everything. In fact, most systems keep only a sample of requests, which is called sampling. A head-based sampler decides when the root span is created, with a chance such as 1 in 100, and every service follows that decision. This keeps the cost low, but a rare slow request may not be among the sampled ones, so tracing shows typical behavior well and rare behavior less well.
It is tempting to think that tracing replaces logs and metrics. In fact, a span tells you where time went, but not what a program decided or what value a variable held. Tracing is most useful when it is paired with logs that carry the trace ID, so an engineer can jump from a slow span to the detailed log lines for that request.
It is tempting to think that every call needs a new trace. In fact, the trace ID must be passed along every call, including calls made through queues or background jobs. If a service drops the header, its work appears as a separate trace with no parent, and the tree breaks at that point.
Use distributed tracing when a request crosses several services and the question is where its time or errors came from. It adds overhead for each call and needs every service to propagate the context, so it is less useful for a single program on one machine, where a profiler or ordinary logs are simpler. Alternatives include metrics, which show that something is slow across many requests but not which request, and logs, which show detail but are hard to join across services without an ID.
History
By the early 2000s, large web companies ran many services for one page, and debugging a slow request meant reading the logs of each service by hand and matching timestamps, which is unreliable when the clocks disagree. Google's engineers wanted a way to follow one request through all of its services.
In 2010 a Google team described Dapper, a tracing system that gave each request a trace ID, passed it along the call path, and recorded spans. Dapper sampled requests to keep the cost low, and it argued that a small sample was often enough to find typical and slow behavior. The paper made the span-and-tree model widely known.
Later open-source systems such as Zipkin and Jaeger used the same model, and in 2019 the OpenTracing and OpenCensus projects merged into OpenTelemetry, which now gives one common way to create and pass on spans. The idea caught on because the cost of a request crossing many services had grown beyond what logs alone could explain.
Recap
- A request gets one trace ID, and every service that handles it records a span with a start, an end and a parent.
- Context propagation passes the trace ID and the caller's span ID along every call, so the spans can be joined into a tree.
- A span's self time is its duration minus its children's time, and the critical path shows which chain set the total time.
- Sampling keeps the cost low, and the trace is most useful when linked to logs by the trace ID.
- Clocks on different machines disagree, so cross-machine timing has an error bound; see clock synchronization.
Read next: Dapper, a large-scale distributed systems tracing infrastructure for the original system and its design choices, and Happened-Before Relation for what the order of events can and cannot prove across machines.
Check yourself
QuestionWhat does a span record, and what links two spans together in a trace?Show answer
A span records a start time, an end time and the work it timed. Spans are linked by a shared trace ID, which identifies the whole request, and by a parent ID, which identifies the span that called them.
QuestionA span lasts 300 ms, and its two non-overlapping child spans last 120 ms and 90 ms. What is its self time?Show answer
Answer: ms.
QuestionWhy can a trace stay correct even when the spans arrive at the collector out of order?Show answer
The collector rebuilds the tree from the trace ID and the parent IDs, not from the order in which the spans arrive. Each span names its parent, so the links do not depend on arrival time.
QuestionA service drops the trace header when it calls a background queue. What happens to the trace, and why does this matter?Show answer
The work done after the queue starts a new trace with no parent link to the original request, so the tree breaks at that point. The engineer can no longer see how much of the request's time was spent in the background work.