Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. Why one request needs a trace
  2. 2. Spans, context and the critical path
  3. 3. Reshape a service tree, then sample it
  4. 4. When one slow call in a hundred reaches most requests
  5. 5. Traces that cross a queue
  6. 6. What a tail sampler costs to run
  7. 7. Off-path spans, 100 backends, keeping slow traces

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
gateway
spans so far
1
  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.

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.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms