wool

package
v0.1.156 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 10 Imported by: 8

Documentation

Index

Constants

View Source
const LogEvent = "log"
View Source
const ProviderKey = ContextKey("provider")

Variables

View Source
var ContextKeys []ContextKey

ContextKeys lists all user identity keys for iteration.

View Source
var HTTPMappings = map[string]ContextKey{
	"X-User-Id":    UserIDKey,
	"X-Org-Id":     OrgIDKey,
	"X-Roles":      RolesKey,
	"X-Auth-Id":    UserAuthIDKey,
	"X-User-Email": UserEmailKey,
	"X-User-Name":  UserNameKey,
}

HTTPMappings maps HTTP header names to context keys. Uses standard header conventions (X-User-Id, X-Org-Id, etc.) for interoperability with API gateways and external tools.

Functions

func GRPCInstrumentation added in v0.1.123

func GRPCInstrumentation() []interface{}

GRPCInstrumentation returns gRPC server options for instrumentation.

func Header(key ContextKey) string

Header returns the HTTP header name for a context key.

func HeaderKey added in v0.1.16

func HeaderKey(header string) string

HeaderKey sanitizes a header name for use as a metadata key.

func Headers added in v0.1.124

func Headers() []string

Headers returns all known HTTP header names.

func IsDebug added in v0.0.60

func IsDebug() bool

IsDebug returns true if the global log level is DEBUG or TRACE.

func MetadataFromRequest added in v0.1.123

func MetadataFromRequest(_ context.Context, req *http.Request) metadata.MD

MetadataFromRequest extracts gRPC metadata from an HTTP request, mapping known HTTP headers to context keys.

func NotFound added in v0.1.18

func NotFound(what ContextKey) error

func RegisterTelemetry added in v0.1.155

func RegisterTelemetry(tp TelemetryProvider)

RegisterTelemetry sets the global telemetry provider. Typically called from an init() in a backend package (e.g. wool/otel).

func SetFallbackLogger added in v0.1.155

func SetFallbackLogger(l LogProcessor)

SetFallbackLogger sets (or clears) the global fallback LogProcessor. Pass nil to revert to the default Console logger.

func SetGlobalLogLevel added in v0.0.65

func SetGlobalLogLevel(loglevel Loglevel)

SetGlobalLogLevel sets the global log level (thread-safe).

func TelemetryEnabled added in v0.1.155

func TelemetryEnabled() bool

TelemetryEnabled returns true if a telemetry provider has been registered.

func TypeOf added in v0.0.55

func TypeOf[T any]() string

Types

type BufferedProcessor added in v0.1.155

type BufferedProcessor struct {
	// contains filtered or unexported fields
}

BufferedProcessor wraps a LogProcessor with channel-based async batching. Log messages are queued and processed by a background goroutine.

func NewBufferedProcessor added in v0.1.155

func NewBufferedProcessor(inner LogProcessor, bufferSize int) *BufferedProcessor

NewBufferedProcessor creates a buffered wrapper around a LogProcessor. bufferSize controls the channel capacity.

func (*BufferedProcessor) Flush added in v0.1.155

func (bp *BufferedProcessor) Flush()

Flush closes the buffer and waits for all queued messages to be processed.

func (*BufferedProcessor) Process added in v0.1.155

func (bp *BufferedProcessor) Process(msg *Log)

Process queues a log message for async processing. If the buffer is full, the message is dropped (non-blocking).

type CodePath added in v0.0.55

type CodePath struct {
	Method string      `json:"method"`
	Fields []*LogField `json:"fields"`
}

type CodeReference added in v0.0.60

type CodeReference struct {
	Line int    `json:"line"`
	File string `json:"file"`
}

func (*CodeReference) String added in v0.0.71

func (c *CodeReference) String() string

type Console added in v0.0.57

type Console struct {
	// contains filtered or unexported fields
}

Console is a simple stdout LogProcessor.

func NewMessageConsole added in v0.1.89

func NewMessageConsole() *Console

NewMessageConsole returns a Console that only prints the message text.

func (Console) Process added in v0.0.57

func (c Console) Process(msg *Log)

type ContextKey

type ContextKey string
const (
	// Standard identity headers (set by auth sidecar)
	UserIDKey ContextKey = "user.id"
	OrgIDKey  ContextKey = "org.id"
	RolesKey  ContextKey = "user.roles"

	// Auth provider identity (from JWT/gateway)
	UserAuthIDKey ContextKey = "user.auth.id"
	UserEmailKey  ContextKey = "user.email"
	UserNameKey   ContextKey = "user.name"
)

User identity context keys for propagation across service boundaries.

type GRPC added in v0.1.123

type GRPC struct {
	// contains filtered or unexported fields
}

GRPC returns a GRPC helper for metadata propagation. Backwards-compatible wrapper.

func (*GRPC) Inject added in v0.1.123

func (g *GRPC) Inject()

Inject reads gRPC incoming metadata and injects known keys into the Wool context.

type HTTP added in v0.1.123

type HTTP struct {
	// contains filtered or unexported fields
}

HTTP provides HTTP header propagation for user identity.

func (*HTTP) Headers added in v0.1.123

func (h *HTTP) Headers() http.Header

Headers returns HTTP headers populated from the Wool context.

type Identifier added in v0.0.55

type Identifier struct {
	Kind   string `json:"kind"`
	Unique string `json:"unique"`
}

Identifier holds the kind and unique name of a wool source.

func System added in v0.0.67

func System() *Identifier

System returns the system identifier.

func (*Identifier) IsSystem added in v0.0.67

func (identifier *Identifier) IsSystem() bool

IsSystem returns true if this is the system identifier.

type Log

type Log struct {
	Level   Loglevel    `json:"level"`
	Header  string      `json:"header"`
	Message string      `json:"message"`
	Fields  []*LogField `json:"fields"`
}

Log represents a structured log entry.

func LogError added in v0.0.67

func LogError(err error, msg string, fields ...*LogField) *Log

func LogTrace added in v0.0.67

func LogTrace(msg string, fields ...*LogField) *Log

func (*Log) AtLevel

func (l *Log) AtLevel(debug Loglevel) *Log

AtLevel filters fields by the given log level.

func (*Log) String added in v0.0.65

func (l *Log) String() string

String formats a log entry. Format: (level) (this) (header) message [key=value, ...]

type LogField

type LogField struct {
	Key   string   `json:"key"`
	Level Loglevel `json:"level"`
	Value any      `json:"value"`
}

LogField is a key-value pair with an associated log level. A field is shown only if the log level is equal or higher than the field's level.

func DirField added in v0.0.55

func DirField(dir string) *LogField

func ErrField added in v0.0.55

func ErrField(err error) *LogField

func Field

func Field(key string, value any) *LogField

func FileField added in v0.0.55

func FileField(file string) *LogField

func FocusField added in v0.0.69

func FocusField() *LogField

func GenericField added in v0.0.55

func GenericField[T any]() *LogField

func InField added in v0.0.81

func InField(s string) *LogField

func ModuleField added in v0.1.89

func ModuleField(name string) *LogField

func NameField added in v0.0.55

func NameField(name string) *LogField

func NullableField added in v0.0.81

func NullableField[T any](key string, value T) *LogField

func Path added in v0.1.114

func Path(dir string) *LogField

func PointerField added in v0.0.57

func PointerField[T any](override *T) *LogField

func RequestField added in v0.0.55

func RequestField(req any) *LogField

func ResponseField added in v0.0.67

func ResponseField(req any) *LogField

func ServiceField added in v0.1.18

func ServiceField(name string) *LogField

func SliceCountField added in v0.0.57

func SliceCountField[T any](slice []T) *LogField

func StatusFailed added in v0.0.55

func StatusFailed() *LogField

func StatusOK added in v0.0.55

func StatusOK() *LogField

func ThisField added in v0.0.55

func ThisField(this Unique) *LogField

func WorkspaceField added in v0.1.89

func WorkspaceField(name string) *LogField

func Writer added in v0.0.67

func Writer() *LogField

func (*LogField) Debug added in v0.0.67

func (f *LogField) Debug() *LogField

func (*LogField) Error added in v0.0.67

func (f *LogField) Error() *LogField

func (*LogField) String added in v0.0.57

func (f *LogField) String() string

func (*LogField) Trace added in v0.0.67

func (f *LogField) Trace() *LogField

type LogFunc added in v0.1.69

type LogFunc func(string, ...*LogField)

type LogProcessor added in v0.0.55

type LogProcessor interface {
	Process(msg *Log)
}

LogProcessor is the sink interface for log messages.

type LogProcessorWithSource added in v0.0.55

type LogProcessorWithSource interface {
	ProcessWithSource(source *Identifier, msg *Log)
}

LogProcessorWithSource extends LogProcessor with source tracking.

type Loglevel

type Loglevel int

Loglevel defines log severity.

const (
	DEFAULT Loglevel = iota
	TRACE
	DEBUG
	FOCUS
	INFO
	WARN
	ERROR
	FATAL
	FORWARD
)

func GlobalLogLevel added in v0.0.65

func GlobalLogLevel() Loglevel

GlobalLogLevel returns the current global log level (thread-safe).

type NotFoundError added in v0.1.18

type NotFoundError struct {
	// contains filtered or unexported fields
}

func (*NotFoundError) Error added in v0.1.18

func (err *NotFoundError) Error() string

type Provider added in v0.0.55

type Provider struct {
	// contains filtered or unexported fields
}

Provider creates Wool instances and manages an optional telemetry backend.

func New

func New(ctx context.Context, r *Resource) *Provider

New creates a new Provider for the given resource. If a TelemetryProvider has been registered (via RegisterTelemetry or WithTelemetry), tracing is automatically enabled.

func (*Provider) Done added in v0.0.55

func (p *Provider) Done()

Done shuts down telemetry if enabled.

func (*Provider) Get added in v0.0.55

func (p *Provider) Get(ctx context.Context) *Wool

Get creates a Wool instance from this provider.

func (*Provider) Inject added in v0.0.71

func (p *Provider) Inject(ctx context.Context) context.Context

Inject stores this provider in the context.

func (*Provider) WithConsole added in v0.0.69

func (p *Provider) WithConsole(lvl Loglevel) *Provider

WithConsole sets a console logger at the given level.

func (*Provider) WithLogger added in v0.0.55

func (p *Provider) WithLogger(l LogProcessor) *Provider

WithLogger sets a custom log processor.

func (*Provider) WithTelemetry added in v0.1.155

func (p *Provider) WithTelemetry(tp TelemetryProvider) *Provider

WithTelemetry explicitly sets a telemetry provider for this provider.

type Resource added in v0.0.55

type Resource struct {
	Kind   string
	Unique string
}

Resource is the input to New -- identifies what is being observed.

type Span added in v0.1.155

type Span interface {
	// AddEvent records a log entry as a span event.
	AddEvent(name string, fields []*LogField)
	// End completes the span.
	End()
}

Span represents an active trace span. Implementations are provided by telemetry backends (e.g. wool/otel).

type TelemetryProvider added in v0.1.155

type TelemetryProvider interface {
	// NewTracer creates a Tracer scoped to the given name.
	NewTracer(name string) Tracer
	// Shutdown flushes and closes the telemetry backend.
	Shutdown(ctx context.Context) error
}

TelemetryProvider is the bridge between wool and a tracing backend. Register one via RegisterTelemetry or pass it to Provider.WithTelemetry.

func GetTelemetry added in v0.1.155

func GetTelemetry() TelemetryProvider

GetTelemetry returns the registered global telemetry provider, or nil.

type Tracer added in v0.1.155

type Tracer interface {
	// Start creates a new span. The returned context carries the span.
	Start(ctx context.Context, name string) (context.Context, Span)
}

Tracer creates spans.

type Unique added in v0.0.55

type Unique interface {
	Unique() string
}

type Wool

type Wool struct {
	// contains filtered or unexported fields
}

Wool is the main observability handle. It combines structured logging, optional tracing (via TelemetryProvider), and context propagation.

Usage:

w := wool.Get(ctx).In("MyService.Handle")
w.Info("processing request", wool.Field("id", 42))
if err != nil {
    return w.Wrapf(err, "cannot process")
}

func Get

func Get(ctx context.Context) *Wool

Get retrieves or creates a Wool instance from the context. If no provider is found, falls back to a Console logger.

func (*Wool) Catch added in v0.0.55

func (w *Wool) Catch()

Catch recovers from a panic and logs the error. Use with defer: defer w.Catch()

func (*Wool) Close added in v0.0.55

func (w *Wool) Close() error

Close implements io.Closer (no-op).

func (*Wool) Context

func (w *Wool) Context() context.Context

func (*Wool) Debug added in v0.0.55

func (w *Wool) Debug(msg string, fields ...*LogField)

func (*Wool) DisableCatch added in v0.1.89

func (w *Wool) DisableCatch()

func (*Wool) Error added in v0.0.55

func (w *Wool) Error(msg string, fields ...*LogField)

func (*Wool) Fatal added in v0.0.55

func (w *Wool) Fatal(msg string, fields ...*LogField)

func (*Wool) Focus added in v0.0.69

func (w *Wool) Focus(msg string, fields ...*LogField)

func (*Wool) Forward added in v0.0.71

func (w *Wool) Forward(p []byte) (n int, err error)

func (*Wool) Forwardf added in v0.1.89

func (w *Wool) Forwardf(msg string, args ...any)

func (*Wool) GRPC added in v0.1.123

func (w *Wool) GRPC() *GRPC

func (*Wool) HTTP added in v0.1.123

func (w *Wool) HTTP() *HTTP

func (*Wool) In added in v0.0.55

func (w *Wool) In(method string, fields ...*LogField) *Wool

In creates a scoped child Wool with a method name and optional fields.

func (*Wool) Info added in v0.0.55

func (w *Wool) Info(msg string, fields ...*LogField)

func (*Wool) Inject added in v0.0.67

func (w *Wool) Inject(ctx context.Context) context.Context

Inject stores the provider in the context for later retrieval via Get.

func (*Wool) LogLevel added in v0.1.20

func (w *Wool) LogLevel() Loglevel

LogLevel returns the effective log level for this Wool instance.

func (*Wool) Name added in v0.0.55

func (w *Wool) Name() string

func (*Wool) NewError added in v0.0.55

func (w *Wool) NewError(format string, args ...any) error

func (*Wool) OrgID added in v0.1.155

func (w *Wool) OrgID() (string, bool)

func (*Wool) Roles added in v0.1.155

func (w *Wool) Roles() (string, bool)

func (*Wool) Source added in v0.0.67

func (w *Wool) Source() *Identifier

func (*Wool) Trace added in v0.0.55

func (w *Wool) Trace(msg string, fields ...*LogField)

func (*Wool) UserAuthID added in v0.1.35

func (w *Wool) UserAuthID() (string, bool)

func (*Wool) UserEmail added in v0.1.18

func (w *Wool) UserEmail() (string, bool)

func (*Wool) UserID added in v0.1.18

func (w *Wool) UserID() (string, bool)

func (*Wool) UserName added in v0.1.35

func (w *Wool) UserName() (string, bool)

func (*Wool) Warn added in v0.0.55

func (w *Wool) Warn(msg string, fields ...*LogField)

func (*Wool) With added in v0.0.60

func (w *Wool) With(fields ...*LogField) *Wool

With appends fields to this Wool instance.

func (*Wool) WithLogger added in v0.0.55

func (w *Wool) WithLogger(l LogProcessor) *Wool

func (*Wool) WithLoglevel added in v0.0.87

func (w *Wool) WithLoglevel(level Loglevel)

func (*Wool) WithOrgID added in v0.1.155

func (w *Wool) WithOrgID(id string)

func (*Wool) WithUserAuthID added in v0.1.123

func (w *Wool) WithUserAuthID(authID string)

func (*Wool) WithUserEmail added in v0.1.123

func (w *Wool) WithUserEmail(s string)

func (*Wool) WithUserID added in v0.1.155

func (w *Wool) WithUserID(id string)

func (*Wool) Wrap added in v0.0.55

func (w *Wool) Wrap(err error) error

func (*Wool) Wrapf added in v0.0.55

func (w *Wool) Wrapf(err error, msg string, args ...any) error

func (*Wool) Write added in v0.0.67

func (w *Wool) Write(p []byte) (n int, err error)

Write implements io.Writer -- forwards bytes as FORWARD-level log entries.

Directories

Path Synopsis
Package otel provides an OpenTelemetry backend for wool.
Package otel provides an OpenTelemetry backend for wool.

Jump to

Keyboard shortcuts

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