In a monolith, logs are enough: a request comes in, a response is recorded. In a distributed system, a request passes through a gateway, several services, queues and databases, and each has its own log with its own timestamps and its own ordering of entries. Reassembling the picture by hand is nearly impossible, so the request is tagged with a shared identifier and its path is recorded as a single trace.
Trace and span
A trace is the entire path of a single request through the system. A span is one segment of work with a known start and end: handling a request in a service, running an SQL query, publishing a message to a queue. Spans nest inside one another and form a tree: the root is the arrival of the request, and its children are its internal steps and outgoing calls.
| Span field | What it records |
|---|---|
| Operation name | what was done: a handler, an SQL query, publishing to a queue |
| Time | the start and end timestamps of the segment |
| Trace ID | the identifier of the whole trace, shared by all of its spans |
| Span ID and parent | its own identifier and the parent span; the root has no parent |
| Status and attributes | success or failure; attributes — for example, the HTTP route, the address being called |
How a trace travels between services
Having received a request, each service passes the tracing context along — in the HTTP headers, in the queue message properties, in the gRPC metadata. The standard format is W3C Trace Context: the traceparent header carries the identifiers of the trace and the current span, while tracestate holds data specific to a particular tracing system.
traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01# ^version ^trace-id (32 chars) ^parent-id (16) ^flagsEach service reads these headers, continues the trace and records its own spans with the tracer. Example: a request passes through a gateway, the order service and a database; the total time is 320 ms, of which 290 ms is taken up by an SQL query without an index. The trace immediately shows the slow segment that would otherwise have to be pieced together by hand from the logs of three services.
Where it is used
- finding delays and errors: which call slowed the request down, where it failed
- a dependency map: which services actually call each other, and how often
The idea came from Google: in 2010 the company described Dapper, its internal system for tracing requests across large distributed systems. Today traces are usually collected to the OpenTelemetry standard: an SDK adds tracing to code and libraries, and exporters send the data to a backend of your choice — Jaeger, Zipkin or a cloud service.
Sources
the term is written from these pages- W3C Trace Context — Recommendationw3.org/TR/trace-context/(opens in a new tab)
- OpenTelemetry — Docs: Tracesopentelemetry.io/docs/concepts/signals/traces/(opens in a new tab)
- Google Research — Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)research.google/pubs/dapper-a-large-scale-distributed-systems-tracing-infrastructure/(opens in a new tab)
Verified 18 Nov