Traces, Spans, and Context Propagation
You'll learn how distributed tracing models a request's journey through your system using traces, spans, and propagated context.
The Core Idea
When a user request flows through multiple services—API gateway → authentication service → database → cache → response—you need a way to stitch together all those operations into a single story. That's what distributed tracing does.
A trace represents the entire journey of a single request through your distributed system. Think of it as a GPS route for one specific request from start to finish.
A span is one unit of work within that trace—a single operation like a database query, HTTP call, or function execution. Spans are the building blocks of traces. Each span has a start time, duration, and metadata about what happened.
Context propagation is how you connect these spans across service boundaries. When Service A calls Service B, it passes along a trace ID and span ID (usually via HTTP headers or message metadata). Service B uses this context to create child spans that belong to the same trace, maintaining the parent-child relationship.
How It Works Together
Imagine a package delivery:
- The trace is the entire journey from warehouse to your door
- Each span is a leg: warehouse → sorting center → delivery truck → your address
- Context propagation is the tracking number that connects all these legs together
Without context propagation, you'd have disconnected logs from each service with no way to know they're all part of the same request.
Key Takeaway: Traces tell the complete story of a request, spans are the individual chapters, and context propagation is the thread that stitches them together across service boundaries.