The RequestID middleware assigns a unique identifier to every incoming request, stores it in the Gin context, and echoes it back in theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/AndresGT/GoKit/llms.txt
Use this file to discover all available pages before exploring further.
X-Request-ID response header. Downstream handlers, loggers, and error responses can all reference the same ID — making it straightforward to correlate logs and trace a single request across services.
Import
Overview
On each request the middleware:- Reads the incoming header (default:
X-Request-ID). - If
TrustIncomingistrueand the value passes the validator, reuses the provided ID. - Otherwise generates a new UUID via the configured
Generator. - Stores the ID in the Gin context under
"request_id". - Writes the ID to the response header.
- Optionally injects a logger enriched with
request_idinto the context (whenInjectToLogger: true).
Presets
DefaultRequestID
Trusts a valid incoming
X-Request-ID, generates a new UUID when none is provided, and injects request_id into the logger in context.StrictRequestID
Always generates a fresh UUID, ignoring any
X-Request-ID sent by the client. Useful when you cannot trust callers to provide valid IDs.Reading the Request ID in Handlers
Usemiddleware.GetRequestID to retrieve the ID from the Gin context at any point after the middleware has run:
Constants
| Constant | Value | Description |
|---|---|---|
middleware.RequestIDKey | "request_id" | Context key used with c.Get |
middleware.RequestIDHeader | "X-Request-ID" | Default header name read and written |
Manual Configuration
Usemiddleware.RequestID(RequestIDConfig{...}) for full control:
RequestIDConfig Fields
The HTTP header name read from the request and written to the response. Defaults to
"X-Request-ID". Set to "X-Trace-ID" or any custom name to integrate with existing distributed tracing infrastructure.A function that returns a new request ID string. Defaults to
uuid.NewString() from the Google UUID package. Replace it to add a custom prefix or use a different ID format.A function that checks whether an incoming header value is acceptable before reusing it. Defaults to a regex that requires between 8 and 128 alphanumeric, hyphen, underscore, or dot characters:
^[a-zA-Z0-9\-_.]{8,128}$.When
true, the middleware reuses a valid incoming ID from the request header. When false (as in StrictRequestID), a new ID is always generated regardless of what the client sends.When
true, creates a new logger with request_id pre-attached and stores it in the Gin context under the key "logger". The GoKit logger middleware and logger.GetLogger(c) will automatically pick this up so every subsequent log entry for this request includes the request ID.Built-in Generator and Validator
GoKit exports two default functions used by all presets. You can reference them directly in aRequestIDConfig or replace them with your own implementation.
| Function | Signature | Behaviour |
|---|---|---|
DefaultGenerator | func() string | Returns uuid.NewString() — a random RFC 4122 UUID v4. |
DefaultValidator | func(id string) bool | Accepts IDs matching ^[a-zA-Z0-9\-_.]{8,128}$ — 8–128 alphanumeric, hyphen, underscore, or dot characters. |
Custom Generator Example
Use a custom header name, a prefixed ID format, and a simple length-based validator:Middleware Ordering
Add
RequestID first in your middleware chain — before the logger middleware — so that every subsequent log entry automatically includes the request ID. If InjectToLogger is true, the enriched logger is stored in the context as soon as RequestID runs, and all downstream log calls made via logger.GetLogger(c) will carry the ID.