Documentation
¶
Overview ¶
Package correlation propagates a request-scoped correlation ID across HTTP entry, audit emission, log lines, and outbound calls.
One ID per top-level intent. See docs/engineering/correlation_id_propagation.md for the full design and specs/system/correlation.spec.yaml for the behavioral contract.
IDs are formatted as "<prefix>-<16 hex chars>", where the hex portion is the high-order 8 bytes of a UUIDv7 (so they are roughly time-ordered when sorted lexicographically). Prefixes signal origin:
req- HTTP request cron- scheduled job tick boot- process startup test- test harness (reserved)
Index ¶
Constants ¶
const HeaderName = "X-Correlation-Id"
HeaderName is the HTTP header carrying the correlation ID.
Variables ¶
This section is empty.
Functions ¶
func From ¶
From returns the correlation ID on the context, if any. The bool is false when no ID is set; callers should treat that as "logs/audit will have no correlation_id attached for this code path."
func Generate ¶
Generate returns a fresh correlation ID with the given prefix.
Format: <prefix>-<16 hex chars>. The 16 hex chars are 8 bytes:
- Bytes 0-5: 48-bit unix-millisecond timestamp (time-ordered when sorted)
- Bytes 6-7: 16-bit monotonic counter, randomly seeded each ms
IDs sort lexicographically in time order; within the same millisecond they sort by counter (monotonically increasing, distinct).
Panics on rand.Read failure — that condition signals the OS RNG is broken, which is not recoverable.
func HTTPMiddleware ¶
HTTPMiddleware extracts (or generates) a correlation ID for every incoming request, places it on the request context, and echoes it back in the response header.
It MUST be mounted before any other middleware that emits logs or audit events. Per correlation.spec.yaml AC-9..AC-11 and the design in docs/engineering/correlation_id_propagation.md §5.1.
Rejected client headers (charset/length/reserved-prefix violations) are replaced with a freshly-generated req- ID; a warn-level log records the rejection with a truncated preview of the bad value.
func SanitizeOrGenerate ¶
SanitizeOrGenerate inspects a client-supplied X-Correlation-Id header and returns either the original (when valid) or a freshly generated req- ID. The regenerated flag is true when the input was rejected and callers may want to log a warning.
Empty input is not a rejection — it just means "no client header, generate one." Rejection happens for:
- input that fails validIDPattern (charset/length)
- input that starts with a reserved prefix