Course contents · Lesson 12 of 22

What Is Distributed Tracing? Spans Explained

Traces · Lesson 12 of 22 · 10 min Engineer

Your checkout page has started taking most of a second to answer, and nobody can say why. The request passes through a frontend, a cart service, a cache, a payment service and a database, and every team's dashboard looks normal on its own. Distributed tracing ends the guessing. It records the path of one request through every service, with a stopwatch on each step, so you can point at the exact step that ate the time.

Traces and spans

In a system made of many small services, one click becomes a chain of calls between programs running on different machines. A trace is the record of that whole journey for one request. A span is one timed step inside the trace: one HTTP request handled, one database query run, or one piece of code a developer chose to time.

Every span records the same few facts:

  • Name: what the step was, such as GET /checkout or INSERT payments.
  • Start time and duration: when the step began and how long it took (end time minus start time).
  • Attributes: key-value details, such as the HTTP method or the database query.
  • Status: Unset (the usual value for success), Ok or Error.

The word "distributed" matters. Each service sends its own spans, on its own schedule, to a tracing backend such as Jaeger, Grafana Tempo or Zipkin. The backend has never seen your architecture diagram. It joins the spans into one trace purely by the IDs they carry. If metrics, logs, traces and profiles still blur together, the four signals lesson shows where traces fit.

Flow of one checkout request: browser to frontend, frontend to cart and payment, cart to Redis, payment to Postgres, with a span recorded at each hop
One click becomes five network hops and six spans, and a single shared trace ID is what lets you see them as one journey.

Parent and child spans form a tree

When the work inside one span calls something else, the new span is its child and the caller's span is its parent. The cart service's cart.get_items span is the parent of the redis GET span, because cart made that call. A span can have many children but only one parent.

The first span of a trace has no parent. It is the root span, and it usually stands for the request as it entered your system, here GET /checkout in the frontend. Its duration is close to what the user waited, so it is the bar you compare everything else against.

Spans do not have to cross a network. The frontend's auth.verify span times a check that runs inside the frontend process. Instrumentation libraries also often record one network call as two spans: a client span in the caller and a server span in the receiver. The difference between their durations is time spent on the wire or waiting in a queue. OpenTelemetry stores this role as the span's kind: CLIENT, SERVER, INTERNAL, PRODUCER or CONSUMER.

Tree of six spans under trace ID 0af7651916cd43dd8448eb211c80319c, with the root GET /checkout at the top and each child listing its parent's span ID
No span knows the whole tree; each one only remembers its parent, and that is enough for the backend to rebuild it.

Trace IDs, span IDs, attributes and events

Two IDs hold everything together. The trace ID is a random 16-byte number, written as 32 hexadecimal characters (the digits 0 to 9 and the letters a to f). It is created when the root span starts and copied into every span of the trace. The span ID is an 8-byte number, written as 16 hex characters, unique to one span. Each span also stores its parent's span ID, and that one field is what draws the tree. How the trace ID reaches the next service is the subject of the next lesson.

Attributes add the detail you search by. Older instrumentation writes http.method and db.statement. The current OpenTelemetry semantic conventions, the shared naming rules, call them http.request.method and db.query.text, so check which names your libraries emit before you write queries. Events are timestamped notes inside a span. The most common one is an exception event that carries the error type and message.

Here is the failed Redis call as a tracing UI would show it, trimmed to the useful fields:

name:           redis GET
trace_id:       0af7651916cd43dd8448eb211c80319c
span_id:        2f8c5a1e9b3d4c07
parent_span_id: 53995c3f42cd8ad8
kind:           CLIENT
start:          47 ms after the root span began
duration:       3 ms
attributes:
  db.query.text:   GET cart:42
  server.address:  cache
  server.port:     6379
status:         ERROR  "Connection refused"
events:
  +3 ms  exception  exception.type=ConnectionError
                    exception.message="Connection refused"

Read it from the top. The two IDs place the span in the tree, the attributes say which server and which key, and the status plus the exception event say why the bar is red. Write the same trace ID into your log lines, as the structured logging lesson suggests, and you can jump from this span straight to the matching logs.

Reading a waterfall to find where latency comes from

Tracing UIs draw a trace as a waterfall: one row per span, indented under its parent, with a bar whose left edge is the start time and whose length is the duration. Because every bar shares one time axis, you can see which steps ran one after another and which overlapped.

Waterfall of the checkout trace: a 780 ms root span, short auth and cart bars, a failed 3 ms Redis call, a 570 ms payment bar containing a 500 ms database insert, and 80 ms of the root with no child span, 60 ms of it at the end
Nearly two-thirds of the user's wait sits inside one database insert, something no single service's dashboard would have told you.

Read every waterfall with three questions. First, which bar is the longest? Here it is INSERT payments, 500 of the 780 ms. Check whether a long bar's time is its own or its children's: payment.charge looks slow only because of the insert inside it.

Second, where are the gaps? Time inside a parent that no child covers was spent in code nobody timed, such as building the response, waiting for a thread or garbage collection. Do the sum: the three children cover 30 + 100 + 570 = 700 ms, so 80 ms of GET /checkout is untimed. The last 60 ms is one gap; the rest are short pauses before and between the children.

Third, what is red? The redis GET span failed in 3 ms. The checkout still worked because cart fell back to slower work, which you can see as the untimed rest of its 100 ms bar.

Notice the shape as well. The auth, cart and payment steps ran one after another, so their times add up; steps that do not depend on each other could run in parallel. A long staircase of identical short database spans is another classic shape: one query per item in a loop, where one query would do. That is what distributed tracing is for: turning "checkout is slow" into "this call, in this service, for this reason".

Check yourself

A trace has 40 spans. How many of them are root spans, and how do you recognise it?

One: it is the only span with no parent span ID, because it is where the request entered the system.

A parent span lasts 900 ms and its only child lasts 200 ms. Where did the other 700 ms go?

Into the parent's own code or work nobody instrumented, which shows as a gap; add spans or use a profiler to find out what it was.

Two spans from different services carry the same trace ID. What does that tell you?

They belong to the same request's journey, because the trace ID is created once at the root and copied into every span of that trace.

Go deeper

  • NGINX Jaeger Tracing: get spans out of an NGINX reverse proxy and into Jaeger, useful when the application behind it cannot be instrumented.