Overview
Context propagation functions allow you to attach metadata (trace IDs, span IDs, request IDs, user IDs) to Go contexts. This metadata is automatically extracted and included in log entries when using context-aware logging functions.Functions
WithTraceID
Adds a trace ID to the context.ctx- The parent contexttraceID- The trace ID to associate with this context
- A new context with the trace ID stored
WithSpanID
Adds a span ID to the context.ctx- The parent contextspanID- The span ID to associate with this context
- A new context with the span ID stored
WithRequestID
Adds a request ID to the context.ctx- The parent contextrequestID- The request ID to associate with this context
- A new context with the request ID stored
WithUserID
Adds a user ID to the context.ctx- The parent contextuserID- The user ID to associate with this context
- A new context with the user ID stored
Chaining Context Functions
You can chain multiple context functions to attach multiple pieces of metadata:Automatic Extraction
When you use context-aware logging functions (LogCtx, InfoLogCtx, etc.), these context values are automatically extracted and included as structured fields in the log entry:
Implementation Details
Context keys are implemented using an unexportedcontextKey type to prevent collisions with other packages: