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.
Attaching a GPS tracking barcode to a postal parcel: as the package moves across airplanes, sorting hubs, and delivery vans, each transit station scans the same barcode, allowing you to track the exact timeline and bottleneck.
Tail-based sampling buffers spans in memory and only persists traces containing HTTP 5xx errors or latency > 1,000ms.
Observability & Distributed Tracing • intermediate
// 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
}
});
}