Overview
Therequestid middleware generates or propagates unique request IDs for distributed tracing and debugging. Each request gets a unique identifier that can be logged and passed to downstream services.
Installation
Quick Start
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
Header | string | "X-Request-ID" | Header name |
Generator | func() string | UUID v4 | ID generator function |
Examples
Basic Usage
Custom Header
Custom Generator
ID Propagation
If the incoming request has a request ID header, it’s preserved:Logging with Request ID
Pass to Downstream Services
API Reference
Technical Details
Implementation
The requestid middleware is implemented with the following key components:- Context Storage: Request IDs are stored in the request context using a private
contextKey{}struct type to prevent collisions - ID Generation: Default generator creates UUID v4-style identifiers using crypto/rand for cryptographic randomness
- Header Propagation: The middleware checks for existing request IDs in incoming headers and preserves them, or generates new ones if absent
- Response Headers: Request IDs are automatically added to response headers for client visibility
UUID v4 Format
The default generator produces IDs in the format:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
- Uses 16 random bytes from
crypto/rand - Version bits (byte 6): Set to
0x40to indicate version 4 - Variant bits (byte 8): Set to
0x80to indicate RFC 4122 variant 2 - Final format: 32 hexadecimal characters in 5 groups (8-4-4-4-12)
Context Key Design
The middleware uses a private struct type as the context key:Flow
- Extract request ID from incoming header (if present)
- If no header, generate new ID using configured generator
- Store ID in request context with private key
- Set ID in response header
- Continue to next handler
Best Practices
- Add early in middleware chain
- Include in all logs
- Pass to downstream services
- Use for error correlation
Testing
The middleware includes comprehensive test coverage for all functionality:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew/generates request ID | Tests default middleware behavior when no request ID is provided | Generates a new UUID v4-style request ID, stores it in context, and sets it in response header |
TestNew/uses existing request ID | Tests ID propagation when client provides an existing request ID | Preserves the existing request ID from request header, stores it in context, and returns it in response header |
TestWithOptions_CustomHeader | Tests custom header name configuration | Uses custom header name (e.g., “X-Correlation-ID”) instead of default “X-Request-ID” |
TestWithOptions_CustomGenerator | Tests custom ID generator function | Uses provided generator function to create sequential custom IDs instead of UUID v4 format |
TestGenerateID | Tests default UUID v4 generator format and uniqueness | Generates IDs in correct UUID v4 format (8-4-4-4-12), sets version 4 bit, and ensures uniqueness across 1000 generations |
TestGet | Tests that Get() and FromContext() are equivalent | Both functions return the same request ID value from context |