“In a microservices architecture, a single user click triggers a cascade of synchronous RPCs and asynchronous message queue jobs. Distributed Tracing injects a unique TraceID and parent SpanID into HTTP/gRPC metadata headers (W3C traceparent). This reconstructs the complete end-to-end execution waterfall DAG with precise latency breakdowns and error root-cause localization.”
Correlating async requests across 50+ microservices using W3C Trace Context, Span IDs, and OpenTelemetry collectors.
// W3C TraceContext Header Propagation
import { trace, context, propagation } from '@opentelemetry/api';
const tracer = trace.getTracer('payment-service');
export async function processPayment(orderId: string, amount: number) {
// Start active span inheriting trace parent from active context
return tracer.startActiveSpan('processPayment', async (span) => {
span.setAttribute('order.id', orderId);
span.setAttribute('payment.amount', amount);
try {
const chargeResult = await paymentGateway.charge(amount);
span.setStatus({ code: 1 }); // OK
return chargeResult;
} catch (error) {
span.recordException(error);
span.setStatus({ code: 2, message: error.message }); // ERROR
throw error;
} finally {
span.end(); // Flushes span metrics to collector
}
});
}API Gateway generates 128-bit hex TraceID: 4bf92f3577b34da6a3ce929d0e0e4736
Generates root SpanID: 00f067aa0ba902b7
Injects W3C header into outgoing RPC: traceparent: 00-4bf92f3577...-00f067aa...-01
Downstream service extracts TraceID, generates child SpanID, and records span duration
OpenTelemetry Collector aggregates all spans into a unified waterfall DAG trace
Tail-based sampling buffers spans in memory and only persists traces containing HTTP 5xx errors or latency > 1,000ms.