In this lesson · topic 2 of 7
Topic 2
Spans, context and the critical path
8 min read
Passing the identifier on
A trace works only if every service tags its spans with the same trace identifier and says which span called it. Each service does that by reading a small piece of metadata from the incoming request, and by writing it onto every outgoing request. This is context propagation. The W3C Trace Context standard fixes one format for it: a single header carrying the trace identifier, the identifier of the calling span, and a flag for whether the trace is being recorded.
In practice a library does the work. A web framework starts a span when a request arrives, reads the header, and sets itself as the parent of any call the handler makes. An HTTP client library adds the header to outgoing requests. A service that forgets to forward the header, or a queue that drops it, breaks the trace in two.
Dapper, the tracing system Google described in 2010, put this in shared libraries for exactly that reason. The paper makes the point that tracing has to be everywhere to be useful, and that the cheapest way to get everywhere is to change the common code rather than every application. The earlier X-Trace system took the same view and extended the idea beyond one protocol, by carrying metadata through every layer of a request.
Reading a trace
A trace is drawn as a waterfall. Time runs left to right. Each span is a bar, indented under its parent. The bar starts when the call started and ends when it returned. A parent spans the length of its children plus work of its own.
Step through a trace below. It is small enough to read all at once.
Reading a trace and finding the critical path
Step 1 / 5- spans so far
- 1
The gateway opens the root span
The request arrives. The gateway starts a span, gives the trace an identifier, and will pass that identifier to everything it calls.
Two calls start at once
The gateway calls catalog and pricing in parallel. Both child spans start almost together, and both carry the same trace identifier and the gateway span as parent.
One of them calls twice, in order
Pricing calls the database and then the cache, one after the other. The second call cannot start until the first has ended, so their bars sit end to end.
Parents end after their last child
Catalog finishes first. Pricing finishes later, because it waited for two calls in a row. The gateway cannot finish until both children have, and then it does a little work of its own.
Walk back from the end
Start at the end of the gateway and follow whichever child finished last, then whichever call last in its group. The path runs gateway, pricing, db, cache, and back up. Its time adds to the full 52 ms. Catalog has the longest own time of the calls, yet it is off the path, so speeding it up changes nothing.
- The gateway opens the root span. The request arrives. The gateway starts a span, gives the trace an identifier, and will pass that identifier to everything it calls. spans so far: 1.
- Two calls start at once. The gateway calls catalog and pricing in parallel. Both child spans start almost together, and both carry the same trace identifier and the gateway span as parent. spans so far: 3.
- One of them calls twice, in order. Pricing calls the database and then the cache, one after the other. The second call cannot start until the first has ended, so their bars sit end to end. spans so far: 5.
- Parents end after their last child. Catalog finishes first. Pricing finishes later, because it waited for two calls in a row. The gateway cannot finish until both children have, and then it does a little work of its own. end-to-end: 52 ms.
- Walk back from the end. Start at the end of the gateway and follow whichever child finished last, then whichever call last in its group. The path runs gateway, pricing, db, cache, and back up. Its time adds to the full 52 ms. Catalog has the longest own time of the calls, yet it is off the path, so speeding it up changes nothing. time on the path: 52 ms; slowest own time: catalog, 33 ms.
Parallel and sequential calls
Two patterns appear in every trace. When a service calls several others in parallel, it waits for the slowest. When it calls them one after another, it waits for all of them in turn. The same fan-out makes a different latency depending on which pattern the code uses.
The critical path is the chain of spans that sets the end-to-end latency. Follow it from the end. At a parallel group, take the child that finished last. At a sequential group, take every child. Add the time those spans spend on their own work and you get the full latency of the request. Anything off the path could be made faster, or even removed, without changing the total.
That fact has a useful and a surprising consequence. The useful one is a way to rank work: time on the critical path is the time worth saving. The surprising one is that the span with the longest own time is not always on the path, so it is not always the right thing to fix.
Which traces to keep
A busy service makes millions of requests an hour. Keeping every span is expensive in storage and in the overhead of sending them. So tracing systems sample.
In head-based sampling the first service decides, at the start, whether this request will be traced. It is a random draw, and the decision travels with the request in the flag of the trace context. It needs no buffering and it is cheap. It cannot know which requests will be slow, because they have not run yet. In tail-based sampling the decision is made after the trace is complete, so a rule can keep every trace that was slow or failed. That is more useful and costs more: every span of every trace has to be held, somewhere, until its trace ends, and all of one trace’s spans have to reach the same place to be judged.
Dapper used head-based sampling with a low fixed probability, 1 in 1024 in its first production setting. Its authors also described a scheme under deployment that aims for a number of traces per unit time instead of a fixed probability. The lab lets you compare both approaches on the same traffic.