Course contentsShow
← Node.js
Lesson 1721 of 1,98015. Logging, Observability, and MetricsFree lesson

Propagating Trace Context to Micro-Frontends

Link client-side request telemetry with backend distributed traces for full-stack visibility.

In this lesson, you will learn how to bridge the visibility gap between your browser-based micro-frontends and your Node.js backend services to maintain a continuous trace.

When your application is composed of several micro-frontends, a single user action might trigger multiple network requests to disparate backend services. Without a unified trace, the browser’s request appears as an isolated event, leaving a "blind spot" in your observability dashboard. To solve this, we must propagate the W3C traceparent header from the browser to the backend.

The Propagation Bridge

The core concept is to treat your micro-frontend as a "Span Producer." When your front-end code initiates an XMLHttpRequest or fetch call, it must inject the current trace-id and span-id into the HTTP headers.

If your micro-frontend uses an instrumentation library (like @opentelemetry/instrumentation-fetch), it will automatically handle this for you. However, when working in a modular micro-frontend architecture, you often need to manually ensure that the trace context is passed through your "shell" or "container" application.

Implementation Example

When the browser sends a request to your Node.js gateway, ensure the header is captured and linked:

// Inside your micro-frontend fetch interceptor
const spanContext = tracer.getActiveSpan().spanContext();
const traceparent = `00-${spanContext.traceId}-${spanContext.spanId}-01`;

fetch('/api/data', {
  headers: {
    'traceparent': traceparent
  }
});

On the Node.js side, your middleware—using the standard OpenTelemetry SDK—will automatically detect this traceparent header. Because the trace ID matches, the backend will treat the incoming request not as a new trace, but as a child span of the existing browser operation.

This creates a seamless "Full-Stack Trace." In your visualization tool, you will no longer see a fragmented list of requests; instead, you will see a unified timeline that starts at the user's click in the browser and flows directly into the specific database queries or downstream service calls within your Node.js environment. This visibility is vital for debugging latency issues that feel fast in the backend but "janky" in the user interface.

Key Takeaway: By injecting the traceparent header into outgoing browser requests, you anchor the backend's distributed trace to the user's specific interaction, ensuring end-to-end visibility across your entire architecture.