# Correlation IDs and Log Context — Logging, Metrics, Tracing & Alerting

Source: https://www.skillbyai.com/en/observability/ob-log-context

> Attach a request ID to every log line automatically so that all lines for one request can be found together.

## Finding one request in a million lines

A busy service handles many requests at once, so their log lines interleave. Without a shared key you cannot tell which "charge card" line belongs to which "order placed" line. The fix is a **correlation ID** (also called request ID): a unique value created when a request enters the system, or taken from an incoming header, and attached to every log line written while handling it.

Passing the ID as an argument to every function is clumsy. Instead, store it in **request-scoped context**: `contextvars` in Python, `context.Context` in Go, `AsyncLocalStorage` in Node.js, MDC in Java logging. A logging filter or processor reads the context and adds the field automatically.

The ID must also travel to downstream services, usually in an HTTP header, so their logs share it. When tracing is enabled, the **trace ID** plays this role, and logging libraries can inject it into each line so you can jump from a log to its trace.

## Tracing the context variable

Each `copy_context().run(...)` executes `handle` in its own copy of the context, like a framework does per request. Follow how the filter picks up the right ID, and why the last line shows the default.

```python
import contextvars, logging, sys

request_id = contextvars.ContextVar("request_id", default="-")

class RequestIdFilter(logging.Filter):
    def filter(self, record):
        record.request_id = request_id.get()
        return True

h = logging.StreamHandler(sys.stdout)
h.addFilter(RequestIdFilter())
h.setFormatter(logging.Formatter("%(request_id)s %(message)s"))
log = logging.getLogger("api")
log.addHandler(h)
log.setLevel(logging.INFO)

def handle(rid):
    request_id.set(rid)
    log.info("charge card")

contextvars.copy_context().run(handle, "req-1")
contextvars.copy_context().run(handle, "req-2")
log.info("background job")
```

- Line 3: A ContextVar is created with a default of '-'. Nothing is set yet, so any read outside a request returns the default.
- Line 21: A fresh copy of the current context is made and handle runs inside it. Changes made inside stay inside this copy.
- Line 18: Inside the copy, the variable is set to req-1. Only code running in this context sees that value.
- Line 7: When log.info runs, the handler's filter reads the ContextVar and stores it on the record so the formatter can print it.
- Line 22: A new copy is made from the outer context, where request_id is still '-'. handle sets req-2 inside this copy and logs.
- Line 23: Back in the outer context the variable was never set, so the filter reads the default '-'. No request ID leaked out of the two requests.

## Accept, validate, propagate

If an upstream service or gateway already sent a request ID header, reuse it instead of generating a new one, so the chain stays connected. Validate its length and characters first: a client could send a 10 KB header or inject newlines to forge log lines.

**Quiz:** Why is a correlation ID stored in request-scoped context rather than a global variable?

- [ ] Global variables cannot hold strings in Python
- [ ] Context variables are faster to read than globals
- [x] Concurrent requests would overwrite one global value and their logs would be mislabelled
- [ ] Logging libraries refuse to read global variables

*Answer:* Concurrent requests would overwrite one global value and their logs would be mislabelled. Many requests run at the same time. A single global would be overwritten by whichever request set it last; request-scoped context keeps one value per request.
