observability

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package observability provides unified configuration and initialization for the four observability pillars: logging, metrics, tracing, and profiling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcknowledgeError

func AcknowledgeError(err error, logger logging.Logger, span tracing.Span, descriptionFmt string, descriptionArgs ...any)

AcknowledgeError standardizes our error handling by logging and tracing consistently.

func ObserveValues

func ObserveValues(values map[string]any, span tracing.Span, logger logging.Logger) logging.Logger

func PrepareAndLogError

func PrepareAndLogError(err error, logger logging.Logger, span tracing.Span, descriptionFmt string, descriptionArgs ...any) error

PrepareAndLogError standardizes our error handling by logging, tracing, and formatting an error consistently.

func PrepareAndLogGRPCStatus

func PrepareAndLogGRPCStatus(err error, logger logging.Logger, span tracing.Span, code codes.Code, descriptionFmt string, descriptionArgs ...any) error

PrepareAndLogGRPCStatus standardizes our error handling by logging, tracing, and formatting an error consistently.

func PrepareError

func PrepareError(err error, span tracing.Span, descriptionFmt string, descriptionArgs ...any) error

PrepareError standardizes our error handling by logging, tracing, and formatting an error consistently.

func RegisterO11yConfigs

func RegisterO11yConfigs(i do.Injector)

RegisterO11yConfigs registers sub-configs extracted from *Config with the injector. This extracts sub-configs from the parent *Config and registers them with the injector. Prerequisite: *Config must be registered in the injector before calling this.

Types

type Config

type Config struct {
	Profiling profilingcfg.Config `envPrefix:"PROFILING_" json:"profiling"`
	Logging   loggingcfg.Config   `envPrefix:"LOGGING_"   json:"logging"`
	Metrics   metricscfg.Config   `envPrefix:"METRICS_"   json:"metrics"`
	Tracing   tracingcfg.Config   `envPrefix:"TRACING_"   json:"tracing"`
	// contains filtered or unexported fields
}

Config contains settings about how we report our metrics.

func (*Config) ProvidePillars

func (cfg *Config) ProvidePillars(ctx context.Context) (*Pillars, error)

ProvidePillars creates and returns all four observability pillars.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a Config struct.

type Matcher

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

Matcher describes a predicate over a single Observation, used by the ordered and per-operation assertion helpers.

func ObservedKey

func ObservedKey(key string) Matcher

ObservedKey matches any observation with the given key, whatever its value or pillar. Use it when a test knows a key was observed but not the value.

func ObservedKeyFunc

func ObservedKeyFunc(key string, pred func(value any) bool) Matcher

ObservedKeyFunc matches an observation with the given key whose value satisfies the predicate. Use it to assert a key was observed with a value of some shape without pinning the exact value.

func ObservedKeyValue

func ObservedKeyValue(key string, value any) Matcher

ObservedKeyValue matches an observation with the given key and a value that is deeply equal to the given value.

func ObservedValue

func ObservedValue(value any) Matcher

ObservedValue matches an observation with a value deeply equal to the given value, under any key.

func (Matcher) OnLog

func (m Matcher) OnLog() Matcher

OnLog refines a matcher to require the observation reached the logger pillar (Set or LogOnly).

func (Matcher) OnSpan

func (m Matcher) OnSpan() Matcher

OnSpan refines a matcher to require the observation reached the span pillar (Set or SpanOnly).

type Observation

type Observation struct {
	Value  any
	Key    string
	Seq    int
	Pillar Pillar
}

Observation is a single recorded attachment, in the order it occurred. Seq is a monotonic counter shared across every Operation of the owning observer, so the relative order of observations from different operations is recoverable.

type Observer

type Observer interface {
	Begin(ctx context.Context) (context.Context, Operation)
	BeginCustom(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Operation)
	Logger() logging.Logger
	Tracer() tracing.Tracer
}

Observer bundles a named logger and tracer for a single component, so that a component holds one observability field instead of a logger/tracer pair. Each traced operation begins via Begin, which returns an Operation that records selected values to the active span and a span-linked logger simultaneously.

It is an interface so that unit tests can substitute a recording implementation (see NewRecordingObserver) and assert which fields a unit observed.

func NewObserver

func NewObserver(name string, logger logging.Logger, tracerProvider tracing.TracerProvider) Observer

NewObserver builds the production Observer from the standard DI dependencies. The name is applied to both the logger and the tracer, mirroring the prior logging.NewNamedLogger / tracing.NewNamedTracer pair.

func NewObserverForTest

func NewObserverForTest(name string) Observer

NewObserverForTest builds an Observer backed by a noop logger and tracer, for code that just needs a functioning Observer in tests. To assert which values a unit attaches, use NewRecordingObserver instead.

func NewObserverWithTracer

func NewObserverWithTracer(name string, logger logging.Logger, tracer tracing.Tracer) Observer

NewObserverWithTracer builds an Observer from an already-constructed Tracer, for the few constructors that receive a tracing.Tracer rather than a TracerProvider (e.g. the embeddings providers). The name is applied to the logger; the tracer is used as given (a nil tracer becomes a noop).

type Operation

type Operation interface {
	Set(key string, value any) Operation
	SetValues(values map[string]any) Operation
	SpanOnly(key string, value any) Operation
	LogOnly(key string, value any) Operation
	Logger() logging.Logger
	Span() tracing.Span
	Error(err error, descriptionFmt string, descriptionArgs ...any) error
	Acknowledge(err error, descriptionFmt string, descriptionArgs ...any)
	GRPCStatus(err error, code codes.Code, descriptionFmt string, descriptionArgs ...any) error
	End()
}

Operation is the per-call observability bag returned by Observer.Begin. A value recorded via Set lands on both the active span and the running logger, so a value selected once is available to either pillar later. SpanOnly and LogOnly are escape hatches for the occasions where a value belongs to just one.

It is an interface so that a recording Observer can hand back an Operation a test can read values off of (see RecordingOperation).

type Pillar

type Pillar uint8

Pillar identifies where an observation landed.

const (
	// PillarBoth marks a value recorded via Set (span and logger).
	PillarBoth Pillar = iota
	// PillarSpan marks a value recorded via SpanOnly.
	PillarSpan
	// PillarLog marks a value recorded via LogOnly.
	PillarLog
)

type Pillars

type Pillars struct {
	Logger          logging.Logger
	TracerProvider  tracing.TracerProvider
	MetricsProvider metrics.Provider
	Profiler        profiling.Provider
}

Pillars holds the four observability pillars: logging, tracing, metrics, and profiling.

type RecordingObserver

type RecordingObserver struct {
	Operations []*RecordingOperation
	// contains filtered or unexported fields
}

RecordingObserver is an Observer implementation for unit tests. It captures, in order, every value attached to the Operations it hands out, so a test can assert which fields a unit observed, in what order, and on which pillar. It performs no real logging or tracing.

func NewRecordingObserver

func NewRecordingObserver() *RecordingObserver

NewRecordingObserver builds a RecordingObserver for use in unit tests.

func (*RecordingObserver) Begin

Begin returns a RecordingOperation and leaves the context untouched.

func (*RecordingObserver) BeginCustom

BeginCustom returns a RecordingOperation and leaves the context untouched.

func (*RecordingObserver) Logger

func (o *RecordingObserver) Logger() logging.Logger

Logger returns a noop logger.

func (*RecordingObserver) ObservedInOrder

func (o *RecordingObserver) ObservedInOrder(t TestingT, matchers ...Matcher)

ObservedInOrder asserts that the given matchers occur, in order, somewhere in the global observation stream. Gaps between matches are allowed, so it reads as "this happened, then later that happened" across any operations.

func (*RecordingObserver) ObservedOperationWithData

func (o *RecordingObserver) ObservedOperationWithData(t TestingT, data map[string]any) *RecordingOperation

ObservedOperationWithData asserts that some recorded operation observed all of the given key/value pairs and that that operation ended, returning the matched operation. It does not care whether the operation also recorded an error, so it holds equally on success and failure paths; assert on the returned op's Errors to verify an error path specifically.

func (*RecordingObserver) ObservedOperationWithKeys

func (o *RecordingObserver) ObservedOperationWithKeys(t TestingT, keys ...string) *RecordingOperation

ObservedOperationWithKeys asserts that some recorded operation observed all of the given keys (on either pillar) and that that operation ended, returning the matched operation so callers can make further assertions (e.g. on its Errors).

func (*RecordingObserver) ObservedOperationWithValues

func (o *RecordingObserver) ObservedOperationWithValues(t TestingT, values ...any) *RecordingOperation

ObservedOperationWithValues asserts that some recorded operation observed all of the given values (under any key) and that that operation ended, returning the matched operation.

func (*RecordingObserver) Stream

func (o *RecordingObserver) Stream() []Observation

Stream returns every observation across all operations, in global order.

func (*RecordingObserver) Tracer

func (o *RecordingObserver) Tracer() tracing.Tracer

Tracer returns a noop tracer.

type RecordingOperation

type RecordingOperation struct {
	Observations []Observation
	Values       map[string]any
	SpanValues   map[string]any
	LogValues    map[string]any
	// Errors holds every error passed to Error, Acknowledge, or GRPCStatus.
	Errors []error
	// Ended reports whether End was called.
	Ended bool
	// contains filtered or unexported fields
}

RecordingOperation is the Operation handed out by RecordingObserver. Its exported maps record which keys reached which pillar (Values = Set, SpanValues = Set + SpanOnly, LogValues = Set + LogOnly), and Observations records the same attachments in order for precise, order-sensitive assertions.

func (*RecordingOperation) Acknowledge

func (op *RecordingOperation) Acknowledge(err error, _ string, _ ...any)

Acknowledge records err.

func (*RecordingOperation) End

func (op *RecordingOperation) End()

End marks the operation ended.

func (*RecordingOperation) Error

func (op *RecordingOperation) Error(err error, descriptionFmt string, descriptionArgs ...any) error

Error records err and returns it wrapped, matching the production Operation's returned-error shape (without logging or tracing).

func (*RecordingOperation) GRPCStatus

func (op *RecordingOperation) GRPCStatus(err error, code codes.Code, descriptionFmt string, descriptionArgs ...any) error

GRPCStatus records err and returns it as a gRPC status error, matching the production Operation's returned-error shape.

func (*RecordingOperation) LogOnly

func (op *RecordingOperation) LogOnly(key string, value any) Operation

LogOnly records a value to the logger pillar only.

func (*RecordingOperation) Logger

func (op *RecordingOperation) Logger() logging.Logger

Logger returns a noop logger.

func (*RecordingOperation) Observed

func (op *RecordingOperation) Observed(t TestingT, matchers ...Matcher)

Observed asserts that each matcher matches some observation in this operation, regardless of order.

func (*RecordingOperation) ObservedInOrder

func (op *RecordingOperation) ObservedInOrder(t TestingT, matchers ...Matcher)

ObservedInOrder asserts that the given matchers occur, in order, within this operation. Gaps between matches are allowed.

func (*RecordingOperation) Set

func (op *RecordingOperation) Set(key string, value any) Operation

Set records a value to both pillars.

func (*RecordingOperation) SetValues

func (op *RecordingOperation) SetValues(values map[string]any) Operation

SetValues records every value via Set.

func (*RecordingOperation) Span

func (op *RecordingOperation) Span() tracing.Span

Span returns nil; recording operations have no real span.

func (*RecordingOperation) SpanOnly

func (op *RecordingOperation) SpanOnly(key string, value any) Operation

SpanOnly records a value to the span pillar only.

type TestingT

type TestingT interface {
	Helper()
	Fatalf(format string, args ...any)
}

TestingT is the minimal subset of *testing.T the assertion helpers need. It lets RecordingObserver offer ergonomic assertions without importing the testing package into production builds; *testing.T satisfies it structurally.

Directories

Path Synopsis
zap
Package metrics provides a metrics-tracking implementation for the service.
Package metrics provides a metrics-tracking implementation for the service.
mock
Package mockmetrics provides moq-generated mocks for the metrics package.
Package mockmetrics provides moq-generated mocks for the metrics package.
Package tracing provides distributed tracing utilities.
Package tracing provides distributed tracing utilities.
cloudtrace
Package cloudtrace provides common functions for attaching values to trace spans
Package cloudtrace provides common functions for attaching values to trace spans
oteltrace
Package oteltrace provides common functions for attaching values to trace spans
Package oteltrace provides common functions for attaching values to trace spans
Package o11yutils offers observability utility functions.
Package o11yutils offers observability utility functions.

Jump to

Keyboard shortcuts

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