Course contentsShow
← System Design
Lesson 1146 of 1,91929. Observability: LoggingFree lesson

Correlation IDs Across Services

Using trace_id and span_id fields to connect logs across distributed service calls.

Correlation IDs Across Services

What you'll learn: How to connect logs from multiple services that collaborate on a single user request using trace and span identifiers.

The Challenge of Distributed Logging

When a user request flows through five microservices, each service writes its own logs independently. Without a linking mechanism, you'd need to manually search each service's logs by timestamp and guess which entries belong together—a nightmare during incident investigation.

Trace IDs and Span IDs

A trace ID uniquely identifies the entire user request journey across all services. Think of it like a package tracking number that follows your shipment through every warehouse and truck.

A span ID uniquely identifies each step or service call within that journey. Each service generates its own span ID but inherits the parent's trace ID.

When Service A calls Service B:

  • Service A generates a trace ID (if starting the request)
  • Service A creates a span ID for its work
  • Service A passes both IDs to Service B via HTTP headers
  • Service B logs both IDs and creates its own child span ID
  • Now you can query logs for that trace ID across all services

In Practice

Every log entry includes fields like:

{"trace_id": "abc123", "span_id": "span-001", "message": "Processing order"}

When investigating, you search all services for trace_id: abc123 and instantly see the complete request path in chronological order.

Key Takeaway: Propagating trace_id and span_id fields through service calls transforms isolated log entries into a connected story, enabling rapid debugging of distributed system failures.