AI Hints
Home›System Design Tutorial›Observability
System Design · Lesson 86 of 120

Logs and Tracing

Following one request.

⏱ 2 min read▶ Video at 1:43:27🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Metrics tell you something is wrong. Logs and traces tell you what, and where. With dozens of services, you need a way to follow one request through all of them.

Logs and Tracing
From the video: Logs and Tracing
  • Metrics: something's wrong
  • Logs and traces: what and where
  • Follow one request

A request ID ties it together

The trick is a request ID. The first service creates a unique ID, and passes it along to every service it calls. Every log line includes it. Run it. Now we can search for one ID, and see the whole journey of that request, across three services, in order.

tracing.pypython
import uuid

def log(service, request_id, message):
    print(f"[{request_id}] {service:>8}: {message}")

def gateway():
    rid = uuid.uuid4().hex[:8]
    log("gateway", rid, "POST /orders")
    orders(rid)

def orders(rid):
    log("orders", rid, "creating order 7")
    payments(rid)

def payments(rid):
    log("payments", rid, "charged 30, ok")

gateway()
Output
[14105524]  gateway: POST /orders
[14105524]   orders: creating order 7
[14105524] payments: charged 30, ok

Logs vs traces

Logs are text records of events, like order seven created. Collect them in one central place, in a structured format, so they're searchable. Traces go further. They record how long each step took, across services, as a timeline. A trace instantly shows that, say, payments took eight hundred milliseconds of a one second request.

Logs

  • Events as recordsCentral and searchableInclude the request ID

Traces

  • Timeline of one requestTime per serviceFind the slow step

Summary

Logs record events, traces show the timing of one request across services, and a request ID ties everything together. Next, turning reliability into numbers you can promise.

Key takeaways
  • One request ID
  • Central logs
  • Traces show timing
Next up: SLIs, SLOs and SLAs — promises in numbers