veb.request_id: New middleware that implements request ID tracking #23727
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Request ID Middleware
This module implements request ID tracking functionality for V web applications. Request IDs are unique identifiers assigned to each HTTP request, which is essential for request tracing, debugging, and maintaining distributed systems.
Purpose
Request IDs help in:
Usage
To enable request ID tracking in your veb app, you must embed the
RequestIdContext
struct in yourContext
struct.Example:
Basic Configuration
Here's a simple configuration example:
Middleware Setup
Enable request ID tracking for all routes or specific routes using veb's middleware system.
Example:
Accessing the Request ID
You can access the request ID in your route handlers:
Configuration Options
The
Config
struct provides several configuration options:Configuration Options Explained
next
: Optional function to conditionally skip the middlewaregenerator
: Function to generate unique IDs (defaults to UUID v4)header
: HTTP header name for the request ID (defaults to "X-Request-ID")allow_empty
: Whether to allow empty request IDsforce
: Whether to generate a new ID even when one already existsAdvanced Usage
Custom ID Generator
You can provide your own ID generator function:
Conditional Middleware Execution
Use the
next
function to skip the middleware based on custom logic:Forcing New IDs
When you want to ensure a new ID is generated regardless of existing headers:
Best Practices
Security Considerations
While request IDs are not security features, consider these points:
Examples
Basic Integration
With Custom Generator and Conditional Execution