context

package
v1.41.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2025 License: Apache-2.0 Imports: 12 Imported by: 5

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

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) Clone added in v1.19.3

func (c Context) Clone() Context

func (Context) Debugf

func (c Context) Debugf(format string, args ...interface{})

func (Context) Error

func (c Context) Error(err error, msg ...any)

func (Context) Errorf

func (c Context) Errorf(format string, args ...interface{})

func (Context) GetSpan

func (c Context) GetSpan() trace.Span

func (Context) GetTracer added in v1.15.1

func (c Context) GetTracer() trace.Tracer

func (Context) Infof added in v1.20.2

func (c Context) Infof(format string, args ...interface{})

func (Context) IsDebug

func (c Context) IsDebug() bool

func (Context) IsTrace

func (c Context) IsTrace() bool

func (Context) Logf added in v1.20.2

func (c Context) Logf(level int, format string, args ...interface{})

func (Context) StartSpan

func (c Context) StartSpan(name string) (Context, trace.Span)

func (Context) String added in v1.26.0

func (c Context) String() string

func (Context) Tracef

func (c Context) Tracef(format string, args ...interface{})

func (Context) Warnf added in v1.20.2

func (c Context) Warnf(format string, args ...interface{})

func (Context) WithDeadline added in v1.21.0

func (c Context) WithDeadline(deadline time.Time) (Context, gocontext.CancelFunc)

func (Context) WithDebug added in v1.19.3

func (c Context) WithDebug() Context

func (Context) WithTimeout added in v1.14.2

func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFunc)

func (Context) WithTrace added in v1.19.3

func (c Context) WithTrace() Context

func (*Context) WithTracer

func (c *Context) WithTracer(tracer trace.Tracer)

func (Context) WithValue

func (c Context) WithValue(key, val interface{}) Context

func (Context) WithoutSpan added in v1.26.0

func (c Context) WithoutSpan() Context

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))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL