Documentation
¶
Overview ¶
Package loggingctx provides request ID middleware and context utilities for distributed tracing.
Index ¶
- func GetRequestID(ctx context.Context) string
- func LogCall(r *http.Request, component, action string, kv ...interface{})
- func LogRequest(ctx *http.Request, status int, latency time.Duration, bytes int)
- func RequestIDMiddleware(next http.Handler) http.Handler
- func WithRequestID(ctx context.Context, requestID string) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRequestID ¶
GetRequestID extracts the request ID from the given context. Returns an empty string if no request ID is found.
func LogCall ¶
LogCall logs a standardized line for handler/API invocations with critical details. Always logs at INFO; include additional method/path/query and req_id. Extra kv pairs are appended.
Parameters:
- r: HTTP request
- component: Component name (e.g., "api", "web", "worker")
- action: Action name (e.g., "GetUser", "CreateInteraction")
- kv: Optional key-value pairs for additional context (must be even number of arguments)
func LogRequest ¶
LogRequest logs a standardized HTTP request with key metrics. Extracts request ID from context for distributed tracing.
Parameters:
- ctx: Request context (should contain request ID from RequestIDMiddleware)
- r: HTTP request
- status: HTTP status code
- latency: Request processing duration
- bytes: Response size in bytes
func RequestIDMiddleware ¶
RequestIDMiddleware is HTTP middleware that generates a unique request ID for each request and stores it in the request context. The request ID is also added to the response headers for client-side correlation.
Request ID format: ULID-like (26 characters, timestamp-prefixed, URL-safe) Example: 01HQZX9P7J8K5M6N4Q3R2T1W0V
If the request already has an X-Request-ID header, it will be preserved. Otherwise, a new ULID is generated.
Usage:
mux := http.NewServeMux()
// ... register handlers ...
handler := loggingctx.RequestIDMiddleware(mux)
http.ListenAndServe(":8080", handler)
Types ¶
This section is empty.