Node.js
Distributed tracing
Learn distributed tracing with OpenTelemetry.
Distributed tracing
Distributed tracing gathers a request's path across many services into one view. OpenTelemetry is the open standard that collects traces uniformly. Each trace gets a trace ID, each step a span ID.
A traceContext(traceId, spanId) function models this link: it joins the two ids with ':'. Example: traceContext('abc123', 'span01') returns 'abc123:span01'. In real OpenTelemetry this context propagates through HTTP headers; here we only see how the ids pair up. The trace ID ties the whole request; the span ID marks a step. This split is what lets you find one request's path among millions of logs.
Context propagation
A trace context propagates across services through HTTP headers. OpenTelemetry's traceparent header carries it in the form 00-<traceId>-<spanId>-<flags>. Each service reads the incoming context, adds its own span and forwards it.
In our simple model traceContext('abc123', 'span01') returns 'abc123:span01'; the real traceparent header would be 00-abc123-span01-01. Joining the two ids shows the structure of the context; the full header format comes later. This model captures the heart of distributed tracing: chaining every step under a single trace ID. This is how you quickly find which service caused a failure.
Exercise Write the trace context function
Write function traceContext(traceId, spanId). Return traceId + ':' + spanId. Print console.log(traceContext('abc123', 'span01')).
Show example solution
function traceContext(traceId, spanId) {
return traceId + ':' + spanId;
}
console.log(traceContext('abc123', 'span01'));
Test yourself
-
Match each piece to its role
- trace ID
- Ties the request
- span ID
- Marks a step
- OpenTelemetry
- Open standard
- traceContext
- Joins ids
-
What is a trace associated with in OpenTelemetry?
traceContext('abc123', 'span01')
- Only the span
- Trace ID ve span ID
- Only the flag
- Only the context
Show answer
Trace ID ve span ID — A trace is associated with a trace ID and a span ID.
-
Complete the join separator
return traceId + '____' + spanId;
Show answer
return traceId + ':' + spanId;
Next lesson: Event sourcing