observe

package
v0.38.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package observe turns action lifecycle hooks into structured events and delivers them to pluggable sinks.

Use Hook to adapt a Sink into an action hook. Bundled sinks cover structured logging (SlogSink), JSON Lines (JSONLSink), Prometheus text exposition (PrometheusSink) and in-memory capture for tests (MemorySink). The slog, JSONL and Prometheus sinks do not serialize request or response payloads; MemorySink retains complete events and is intended for tests.

Index

Constants

View Source
const (
	KindSuccess      = "success"
	KindError        = "error"
	KindTimeout      = "timeout"
	KindRetry        = "retry"
	KindCacheHit     = "cache_hit"
	KindCacheMiss    = "cache_miss"
	KindCanceled     = "canceled"
	KindPanic        = "panic"
	KindCoalesced    = "coalesced"
	KindDeduplicated = "deduplicated"
)
View Source
const (
	// MaxMetricKeys bounds the number of distinct counters retained by MetricsSink.
	MaxMetricKeys = 10_000
	// MaxMetricLabelBytes bounds each action and kind label retained by MetricsSink.
	MaxMetricLabelBytes = 128
)

Variables

This section is empty.

Functions

func Hook

func Hook(sink Sink) action.AnyHook

Types

type Event

type Event struct {
	Time        time.Time
	Duration    time.Duration
	Kind        string
	Action      string
	RequestID   string
	ExecutionID string
	TraceID     string
	SpanID      string
	TenantID    string
	UserID      string
	Request     any
	Response    any
	Error       error
	Recovered   any
	Attempt     int
}

type JSONLSink

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

func NewJSONLSink

func NewJSONLSink(writer io.Writer, _ int) *JSONLSink

NewJSONLSink accepts but ignores the legacy size argument; raw diagnostics are redacted.

func (*JSONLSink) Emit

func (s *JSONLSink) Emit(_ context.Context, event Event)

type MemorySink

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

func NewMemorySink

func NewMemorySink(capacity int) *MemorySink

NewMemorySink creates a bounded in-memory event sink. It returns nil if the allocation exceeds the default memory budget; use NewMemorySinkChecked to inspect allocation errors or NewMemorySinkWithOptions to select an explicit budget.

func NewMemorySinkChecked added in v0.37.8

func NewMemorySinkChecked(capacity int) (*MemorySink, error)

NewMemorySinkChecked creates a bounded in-memory event sink using the default memory budget. Nonpositive capacities retain the default capacity of 100.

func NewMemorySinkWithOptions added in v0.37.8

func NewMemorySinkWithOptions(options ringbuf.BufferOptions) (*MemorySink, error)

NewMemorySinkWithOptions creates a bounded in-memory event sink using the supplied buffer options. Capacities below 1 are normalized to 1.

func (*MemorySink) Emit

func (s *MemorySink) Emit(_ context.Context, event Event)

func (*MemorySink) Events

func (s *MemorySink) Events() []Event

type MetricKey

type MetricKey struct {
	Action string
	Kind   string
}

MetricKey uniquely identifies a lifecycle event counter.

type MetricsSink

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

MetricsSink aggregates lifecycle event counts in memory. Events exceeding the key or label limits are counted by OverflowCount instead of retained.

func NewMetricsSink

func NewMetricsSink() *MetricsSink

NewMetricsSink creates an empty metrics sink.

func (*MetricsSink) Emit

func (s *MetricsSink) Emit(_ context.Context, event Event)

func (*MetricsSink) OverflowCount added in v0.37.0

func (s *MetricsSink) OverflowCount() uint64

OverflowCount returns events not retained because a key or label exceeded the configured metrics bounds.

func (*MetricsSink) Snapshot

func (s *MetricsSink) Snapshot() map[MetricKey]uint64

Snapshot returns an isolated thread-safe copy of all counters.

type PrometheusSink

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

func NewPrometheusSink

func NewPrometheusSink() *PrometheusSink

func (*PrometheusSink) Emit

func (s *PrometheusSink) Emit(ctx context.Context, event Event)

func (*PrometheusSink) OverflowCount added in v0.37.0

func (s *PrometheusSink) OverflowCount() uint64

OverflowCount returns events omitted from the per-key snapshot due to metrics bounds.

func (*PrometheusSink) Snapshot

func (s *PrometheusSink) Snapshot() map[MetricKey]uint64

func (*PrometheusSink) WritePrometheus

func (s *PrometheusSink) WritePrometheus(w io.Writer) error

type Sink

type Sink interface {
	Emit(context.Context, Event)
}

type SlogSink

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

SlogSink writes structured lifecycle events to a standard-library slog.Logger.

func NewSlogSink

func NewSlogSink(logger *slog.Logger) *SlogSink

NewSlogSink creates a sink using logger. If logger is nil, slog.Default() is used.

func (*SlogSink) Emit

func (s *SlogSink) Emit(ctx context.Context, event Event)

Jump to

Keyboard shortcuts

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