Documentation
¶
Overview ¶
Package context provides an enhanced context implementation that combines standard Go context functionality with integrated logging and distributed tracing.
The Context type embeds context.Context and adds:
- Integrated structured logging with level control
- OpenTelemetry tracing support
- Debug and trace mode flags
- Automatic correlation between logs and traces
Basic Usage:
ctx := context.NewContext(context.Background()) ctx.Infof("Processing request %s", requestID) // Enable debug logging debugCtx := ctx.WithDebug() debugCtx.Debugf("Detailed information: %v", details) // Start a traced operation ctx, span := ctx.StartSpan("database-query") defer span.End() ctx.Infof("Executing query: %s", query)
With Custom Logger and Tracer:
ctx := context.NewContext( context.Background(), context.WithLogger(customLogger), context.WithTracer(otel.Tracer("my-service")), )
The context automatically propagates logging configuration and trace spans through the call chain, ensuring consistent observability across your application.
Index ¶
- type Context
- func (c Context) Clone() Context
- func (c Context) Debugf(format string, args ...interface{})
- func (c Context) Error(err error, msg ...any)
- func (c Context) Errorf(format string, args ...interface{})
- func (c Context) GetSpan() trace.Span
- func (c Context) GetTracer() trace.Tracer
- func (c Context) Infof(format string, args ...interface{})
- func (c Context) IsDebug() bool
- func (c Context) IsTrace() bool
- func (c Context) Logf(level int, format string, args ...interface{})
- func (c Context) StartSpan(name string) (Context, trace.Span)
- func (c Context) String() string
- func (c Context) Tracef(format string, args ...interface{})
- func (c Context) Warnf(format string, args ...interface{})
- func (c Context) WithDeadline(deadline time.Time) (Context, gocontext.CancelFunc)
- func (c Context) WithDebug() Context
- func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFunc)
- func (c Context) WithTrace() Context
- func (c *Context) WithTracer(tracer trace.Tracer)
- func (c Context) WithValue(key, val interface{}) Context
- func (c Context) WithoutSpan() Context
- type ContextOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { gocontext.Context Logger logger.Logger // contains filtered or unexported fields }
Context is an enhanced context that embeds the standard context.Context and adds integrated logging and tracing capabilities.
It provides:
- Structured logging with automatic trace correlation
- Debug and trace mode management
- OpenTelemetry span creation and management
- Context value propagation with type safety
The Context maintains its configuration (logger, tracer, debug/trace settings) when creating derived contexts through WithValue, WithTimeout, etc.
func NewContext ¶
func NewContext(basectx gocontext.Context, opts ...ContextOptions) Context
NewContext creates a new enhanced context from a standard Go context. The context is configured with the provided options and defaults to the standard logger and a no-op tracer if not specified.
Example:
// Basic context with defaults ctx := NewContext(context.Background()) // Context with custom configuration ctx := NewContext( context.Background(), WithLogger(myLogger), WithTracer(myTracer), WithDebugFn(customDebugCheck), )
func (Context) WithDeadline ¶ added in v1.21.0
func (Context) WithTimeout ¶ added in v1.14.2
func (*Context) WithTracer ¶
func (Context) WithoutSpan ¶ added in v1.26.0
type ContextOptions ¶
type ContextOptions func(*Context)
ContextOptions is a function that configures a Context during creation.
func WithDebugFn ¶
func WithDebugFn(fn func(Context) *bool) ContextOptions
WithDebugFn sets a custom function to determine if debug logging is enabled. This allows dynamic control of debug logging based on context values or external conditions.
func WithLogger ¶
func WithLogger(log logger.Logger) ContextOptions
WithLogger sets a custom logger for the context. If not specified, the standard logger is used.
func WithTraceFn ¶
func WithTraceFn(fn func(Context) *bool) ContextOptions
WithTraceFn sets a custom function to determine if trace logging is enabled. This allows dynamic control of trace logging based on context values or external conditions.
Example:
ctx := NewContext(context.Background(), WithTraceFn(func(ctx Context) *bool { // Enable trace for specific user IDs userID := ctx.Value("userID") enabled := userID == "debug-user" return &enabled }))
func WithTracer ¶
func WithTracer(tracer trace.Tracer) ContextOptions
WithTracer configures the OpenTelemetry tracer for the context. If not specified, a no-op tracer is used.
Example:
tracer := otel.Tracer("my-service") ctx := NewContext(context.Background(), WithTracer(tracer))