Request correlation IDs: connect logs and distributed traces
A request correlation ID links one request to the logs produced while handling it. Generate one when a service receives a request, or validate a value from a trusted tracing system, then carry it consistently through logs and internal calls. It must not contain authentication tokens or user data, and it must never grant access by itself.
A request correlation ID links one request to the logs produced while handling it. Generate one when a service receives a request, or validate a value from a trusted tracing system, then carry it consistently through logs and internal calls. It must not contain authentication tokens or user data, and it must never grant access by itself.
Distinguish request IDs from trace context
An application request ID can be a simple key for finding logs for one HTTP request. To connect multiple services and spans, use tracing instrumentation that supports W3C Trace Context: traceparent and optional tracestate. traceparent carries standardized trace position; tracestate carries vendor-specific context. A custom X-Request-ID header is only an organizational convention and does not automatically replace this standard.
Apply it through the request flow
**Set trust boundaries.** An external client can forge a correlation value, make it too long, or use it to disrupt log formatting. If you cannot distinguish a value set by your own proxy or gateway, generate a new ID on the server. Even at an internal boundary, validate length and character format; issue a new value if validation fails.
**Use a unique, non-secret value.** Choose a random identifier with sufficiently low collision risk. Do not include a user ID, email, session ID, API key, token, or personal data. An ID that is difficult to guess is still not proof of authorization.
**Propagate it through request context.** Keep the correlation across request receipt, related logs, associated asynchronous work, and internal service calls. With W3C tracing, follow the library's propagation rules for `traceparent`; name log fields clearly so a request ID is not confused with a trace ID.
**Return it only under an API contract.** If you use a response header such as `X-Request-ID`, define its name and format in the service contract and return the generated or validated ID. This is a custom convention, not a standard header. Browser clients that need to read it also require the right CORS exposed-header policy.
**Log the identifier with only necessary event data.** An ID alone may not be enough to diagnose an issue. Add structured fields such as route template, method, status, duration, and safe error category, while excluding credentials and sensitive bodies. Sanitize received header values for the log format to prevent log injection.
For example, a booking request could receive a local ID in a req-... format, with request_id, route template, and status recorded in logs. If internal calls join distributed tracing, the tracing system propagates standard trace context. The format and field names are illustrative, not a claim about any product or API.
Verify and troubleshoot
Confirm that the same request ID finds the relevant application logs.
Check that proxies and services preserve the ID across the intended trust boundaries.
If asynchronous work is tied to a request, confirm that the correlation remains available in logs after a delay.
Check behavior for missing, modified, oversized, or newline-containing client IDs.
Ensure external responses do not unnecessarily expose internal trace data or sensitive `tracestate` values.
A correlation ID is an operations tool for locating related events, not authentication or proof that logs are trustworthy. Manage access control and log retention/integrity under separate policies.
References
[OWASP Logging Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html)
[W3C Trace Context Recommendation](https://www.w3.org/TR/trace-context-1/)
[RFC 9110: HTTP Semantics — Via](https://www.rfc-editor.org/rfc/rfc9110.html#name-via)