Skip to article content
FrontendXLesson 20 / 20

Cross-service tracing

Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.

advanced · about 40 minutes · 2 exercises

  • Debug
  • Interview

What you will be able to do

#
  • Follow one request across services using a propagated trace id.
  • Find the first span that fails or slows and name the boundary where context is lost.

Overview

#

When one user action spans several services, a single log line is not enough. A trace follows one request end to end, so you can see which hop failed or slowed instead of guessing from timestamps.

The unit of a trace is the span. A span has a name, a start and end time, a service, a status and a parent. The trace is the tree of spans for one request, joined by a trace id that every service shares.

The trace id must be propagated. The first service creates it if the request does not carry one, and every call it makes forwards it in a header. A queue or an async boundary is where propagation is most often dropped, and a dropped id splits one request into two unrelated traces.

snippet.js
async function callDownstream(url, traceId) {
  return fetch(url, { headers: { 'x-trace-id': traceId } });
}

Structured logs carry the same trace id, so a log search joins the events of one request across services. Metrics give the aggregate shape, such as a rising error rate, and the trace explains a single case.

To debug a slow or failing request, start from the trace id in the user report, open the trace and read the tree. Find the first span that errored or the span that dominates the duration. That span names the service and the operation to investigate. Compare it with a healthy trace of the same route to see what changed.

Common failures have a small set of causes. A missing propagation drops the context at a boundary. Clock skew between hosts makes the timeline look impossible, such as a child span ending before its parent starts. Sampling means some requests have no trace, so a missing trace is not proof that nothing happened. When you instrument a new service, create a span if none exists, forward the id on every outbound call, and include the id in every log line.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This handler ignores the incoming trace header and creates a new id, so the request appears as two unrelated traces. Fix it to reuse the incoming trace id and forward it on the downstream call.

    starter.javascript
    async function handle(req) {
      const traceId = generateId();
      logger.info({ traceId }, 'handling request');
      return callDownstream('https://api.internal/price', {});
    }

    Reveal the reference answer
    reference.javascript
    async function handle(req) {
      const traceId = req.headers['x-trace-id'] || generateId();
      logger.info({ traceId }, 'handling request');
      return callDownstream('https://api.internal/price', { 'x-trace-id': traceId });
    }

    Authored expected output: The handler reuses the incoming x-trace-id when present, generates one only when absent, and forwards it on the downstream call so the trace stays joined.

    Generating a new id unconditionally breaks the chain at this service. Reusing the incoming id when present keeps one trace across services, and forwarding it on the outbound call preserves the chain for the next hop.

  2. Exercise 2Interview

    A user reports that checkout is slow. Describe how you would use tracing to find the slow hop across services, and how you would rule out a missing trace.

    Reveal the reference answer
    reference.text
    Start from the trace id in the user report and open the trace. Read the span tree and find the span that dominates the total duration or the first span that errored; that span names the service and the operation to investigate. Compare it with a healthy trace of the same route to see what differs. If the request has no trace at all, do not conclude it never reached a service: check whether sampling excluded it, and check for a boundary such as a queue or an async callback where the trace id was not propagated. A missing trace is a propagation or sampling question, not proof of absence.

    Authored expected output: A method that starts from the trace id, finds the dominant or first failing span, compares with a healthy trace, and treats a missing trace as a propagation or sampling issue.

    The trace tree turns a distributed request into a readable sequence, so the slow hop is the span that dominates the duration. A missing trace is explained by sampling or a dropped propagation, so it is not evidence that the request never arrived.

Common misconceptions

#
  • A missing trace proves the request never reached the service.
  • Timestamps from different hosts can be compared directly.

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.