ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/tracer/tasks/task-23-1-2-span-lifecycle
TASK

Implementation

A span represents one operation within a trace: it has a name, start and end timestamps, status, and optionally events and links. The span kind identifies whether the operation is an incoming server call, an outgoing client call, or internal work.

Implement a node that manages span lifecycles:

// Full lifecycle: create -> event -> end
{ "type": "create_span", "msg_id": 1, "name": "GET /api/users/123" }
{ "type": "span_event", "span_id": "span1", "event": "db.query" }
{ "type": "span_end", "span_id": "span1", "status": "OK" }
-> { "type": "span_complete",
    "span_id": "span1", "duration_us": 10000, "status": "OK" }

// Error during span -> ERROR status
{ "type": "create_span", "msg_id": 2, "name": "GET /api/users/999" }
{ "type": "span_error", "span_id": "span2", "error": "User not found" }
-> { "type": "span_complete",
    "span_id": "span2", "status": "ERROR", "error": "User not found" }

// CLIENT kind for outbound calls
{ "type": "create_span", "msg_id": 3,
  "name": "GET /api/data", "kind": "CLIENT" }
-> { "type": "span_created", "span_id": "span1", "kind": "CLIENT" }

Sample Test Cases

Span lifecycle progressionTimeout: 5000ms
Input
{"src":"service","dest":"tracer","body":{"type":"create_span","msg_id":1,"name":"GET /api/users/123"}}
{"type":"span_event","span_id":"span1","event":"db.query"}
{"type":"span_end","span_id":"span1","status":"OK"}
Expected Output
{"src": "tracer", "dest": "service", "body": {"type": "span_complete", "span_id": "span1", "duration_us": 10000, "status": "OK"}}
Span with error statusTimeout: 5000ms
Input
{"src":"service","dest":"tracer","body":{"type":"create_span","msg_id":1,"name":"GET /api/users/999"}}
{"type":"span_error","span_id":"span2","error":"User not found"}
Expected Output
{"src": "tracer", "dest": "service", "body": {"type": "span_complete", "span_id": "span2", "status": "ERROR", "error": "User not found"}}

Hints

Hint 1
Span starts on create_span and ends on span_end; duration = end_time - start_time
Hint 2
Default status is OK; span_error sets status=ERROR with the error message
Hint 3
Span kind: SERVER for incoming requests, CLIENT for outbound calls to other services
Hint 4
Span events are timestamped point-in-time annotations inside a span
Hint 5
Span links connect related spans that are not in a direct parent-child relationship
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

span lifecyclespan kindspan eventsspan linksduration
main.py
python
Implement Span Lifecycle Management - The Tracer | Build Distributed Systems