ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/tracer/tasks/task-23-1-1-trace-context-propagation
TASK

Implementation

Distributed tracing links spans from a single request as it flows through multiple services. Each service propagates the trace context (trace ID + parent span ID) in a header so every hop can be assembled into a single trace tree.

Implement a node that handles trace context operations:

// Service B receives request with traceparent -> creates child span
{ "type": "http_request", "msg_id": 1, "path": "/api/data",
  "headers": {"traceparent": "00-trace123-span_a-01"} }
-> { "type": "http_response", "in_reply_to": 1,
    "span": {"trace_id": "trace123", "span_id": "span_b",
              "parent_span_id": "span_a"} }

// Parse a W3C traceparent header into its parts
{ "type": "parse_traceparent", "msg_id": 2,
  "traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b57-01" }
-> { "type": "parse_ok", "in_reply_to": 2,
    "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
    "parent_span_id": "00f067aa0ba902b57", "sampled": true }

// Reconstruct trace tree from span list
{ "type": "reconstruct_trace", "msg_id": 3,
  "spans": [{"trace_id":"t1","span_id":"s1","parent":null},
             {"trace_id":"t1","span_id":"s2","parent":"s1"},
             {"trace_id":"t1","span_id":"s3","parent":"s2"}] }
-> { "type": "trace_tree_ok", "in_reply_to": 3,
    "trace_id": "t1", "span_count": 3, "depth": 3 }

Sample Test Cases

Propagate trace contextTimeout: 5000ms
Input
{
  "src": "service_a",
  "dest": "service_b",
  "body": {
    "type": "http_request",
    "msg_id": 1,
    "path": "/api/data",
    "headers": {
      "traceparent": "00-trace123-span_a-01"
    }
  }
}
Expected Output
{"src": "service_b", "dest": "service_a", "body": {"type": "http_response", "in_reply_to": 1, "span": {"trace_id": "trace123", "span_id": "span_b", "parent_span_id": "span_a"}}}
Parse W3C traceparent headerTimeout: 5000ms
Input
{
  "src": "service",
  "dest": "parser",
  "body": {
    "type": "parse_traceparent",
    "msg_id": 1,
    "traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b57-01"
  }
}
Expected Output
{"src": "parser", "dest": "service", "body": {"type": "parse_ok", "in_reply_to": 1, "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736", "parent_span_id": "00f067aa0ba902b57", "sampled": true}}

Hints

Hint 1
W3C traceparent format: 00-{trace_id}-{parent_span_id}-{flags}
Hint 2
When service B receives a traceparent header, create a child span with parent_span_id from the header
Hint 3
sampled flag: last byte 01 = sampled, 00 = not sampled
Hint 4
Generate traceparent: format as 00-{trace_id}-{span_id}-01
Hint 5
Trace tree depth = length of the longest parent->child chain
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

distributed tracingtrace contextW3C traceparentspantrace tree
main.py
python
Implement Distributed Trace Context Propagation - The Tracer | Build Distributed Systems