CodeBlanks Logo Icon CodeBlanks

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.

Practice Write the trace context function

Write function traceContext(traceId, spanId). Return traceId + ':' + spanId. Print console.log(traceContext('abc123', 'span01')).

Starter code

index.js
// Write the traceContext function
// Join the two ids with ':' and return
// Call with 'abc123' and 'span01', print
// write here

Sample solution

index.js
function traceContext(traceId, spanId) {
  return traceId + ':' + spanId;
}
console.log(traceContext('abc123', 'span01'));

Expected output

abc123:span01

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
trace ID Ties the request
span ID Marks a step
OpenTelemetry Open standard
traceContext Joins ids

2 What is a trace associated with in OpenTelemetry?

index.js
traceContext('abc123', 'span01')
  • Only the span
  • Trace ID ve span ID Correct answer
  • Only the flag
  • Only the context

Explanation: A trace is associated with a trace ID and a span ID.

3 Complete the join separator

Question
return traceId + '____' + spanId;
Answer
return traceId + ':' + spanId;

Explanation: The two ids are joined with ':'.

All lessons in Expert