observe

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package observe is the agent's observability layer: structured logging, tracing, and metrics, all reached through Flynn-owned ports so the agent never depends on a concrete backend. This mirrors OpenTelemetry's own API/SDK split: this package is a dependency-light API with no-op defaults, and an opt-in adapter (observe/otel) substitutes the real SDK without any call site changing.

The standalone build uses the no-op defaults here (logs discarded, spans and metrics dropped) so the agent runs with zero setup and near-zero overhead. A host such as an Ion Alpha instance injects a real slog.Handler plus Tracer and Meter adapters via New, without this package importing either. Only this package may import log/slog; every other package logs through the Logger port.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Into

Into returns a context carrying o, so deep call sites reach the active logger, tracer, and meter via FromContext instead of threading them as parameters. The dispatch waist binds the run's Observability once; leaf code reads it back.

Types

type Counter

type Counter interface {
	Add(ctx context.Context, n int64, fields ...Field)
}

Counter accumulates a monotonic total.

type Field

type Field struct {
	Key   string
	Value any
}

Field is a typed key/value attribute on a log record, span, or metric. Build one with the typed constructors (String, Int, Err, ...) so call sites never touch the logging backend's own attribute types.

func Any

func Any(key string, v any) Field

Any returns a Field with an arbitrary value.

func Bool

func Bool(key string, v bool) Field

Bool returns a bool-valued Field.

func Err

func Err(err error) Field

Err is a Field carrying an error under the conventional "error" key.

func Float64

func Float64(key string, v float64) Field

Float64 returns a float64-valued Field.

func Int

func Int(key string, v int) Field

Int returns an int-valued Field.

func Int64

func Int64(key string, v int64) Field

Int64 returns an int64-valued Field.

func String

func String(key, v string) Field

String returns a string-valued Field.

type Histogram

type Histogram interface {
	Record(ctx context.Context, value float64, fields ...Field)
}

Histogram records a distribution of values.

type Logger

type Logger interface {
	Debug(ctx context.Context, msg string, fields ...Field)
	Info(ctx context.Context, msg string, fields ...Field)
	Warn(ctx context.Context, msg string, fields ...Field)
	Error(ctx context.Context, msg string, fields ...Field)
	// With returns a Logger that includes fields on every record (a scoped logger),
	// e.g. binding run_id and scope once at the waist.
	With(fields ...Field) Logger
}

Logger is the agent's structured logging port. Call sites depend on this, never on log/slog, so the backend stays swappable and the dependency is lint-enforceable. slog backs the default; a host may inject any slog.Handler.

func NewSlogLogger

func NewSlogLogger(h slog.Handler) Logger

NewSlogLogger returns a Logger backed by the slog.Handler h.

type Meter

type Meter interface {
	// Counter returns a monotonic counter (requests, tokens, errors).
	Counter(name string) Counter
	// Histogram returns a distribution recorder (latency, sizes).
	Histogram(name string) Histogram
}

Meter creates metric instruments. Like the tracer it is a minimal port with a no-op default; an adapter (observe/otel) maps it onto a backend such as OpenTelemetry, exporting to VictoriaMetrics. Fetching an instrument is cheap, so call sites may fetch one per use.

type NopLogger

type NopLogger struct{}

NopLogger is a Logger that discards every record without touching its fields. It is the standalone Default so the disabled-log path allocates nothing; a host that wants real logging injects a slog.Handler through New instead.

func (NopLogger) Debug

func (NopLogger) Debug(context.Context, string, ...Field)

Debug implements Logger by discarding the record.

func (NopLogger) Error

func (NopLogger) Error(context.Context, string, ...Field)

Error implements Logger by discarding the record.

func (NopLogger) Info

func (NopLogger) Info(context.Context, string, ...Field)

Info implements Logger by discarding the record.

func (NopLogger) Warn

func (NopLogger) Warn(context.Context, string, ...Field)

Warn implements Logger by discarding the record.

func (NopLogger) With

func (NopLogger) With(...Field) Logger

With implements Logger by returning the same no-op logger.

type NopMeter

type NopMeter struct{}

NopMeter is a Meter whose instruments do nothing; the standalone default.

func (NopMeter) Counter

func (NopMeter) Counter(string) Counter

Counter implements Meter.

func (NopMeter) Histogram

func (NopMeter) Histogram(string) Histogram

Histogram implements Meter.

type NopTracer

type NopTracer struct{}

NopTracer is a Tracer that does nothing; it is the standalone default.

func (NopTracer) Start

func (NopTracer) Start(ctx context.Context, _ string) (context.Context, Span)

Start implements Tracer by returning the context unchanged and a no-op Span.

type Observability

type Observability struct {
	Log    Logger
	Tracer Tracer
	Meter  Meter
}

Observability bundles the logger, tracer, and meter threaded through the runtime. The zero value is not usable; construct one with Default or New.

func Default

func Default() *Observability

Default returns an Observability that discards everything: a no-op logger, a no-op tracer, and a no-op meter. It is the zero-setup standalone default. The logger is a true no-op (not slog over io.Discard), so a disabled log call on the standalone path builds no attributes and reaches no handler.

func FromContext

func FromContext(ctx context.Context) *Observability

FromContext returns the Observability bound to ctx, or the no-op default if none is present, so a caller can always log, trace, and measure without a nil check.

func New

func New(h slog.Handler, t Tracer, m Meter) *Observability

New builds an Observability from a slog.Handler, a Tracer, and a Meter. A nil handler discards logs; a nil tracer or meter falls back to the no-op. Hosts call this to inject their logging handler and telemetry backends.

type Span

type Span interface {
	// SetAttr records a key/value attribute on the span.
	SetAttr(key string, value any)
	// RecordError marks the span as failed and attaches err.
	RecordError(err error)
	// End closes the span.
	End()
}

Span is a single unit of traced work. End must be called exactly once; deferring it at the call site is the expected usage.

type Tracer

type Tracer interface {
	// Start begins a span named name and returns a context carrying it together
	// with the Span to end. The returned context must be used for nested work.
	Start(ctx context.Context, name string) (context.Context, Span)
}

Tracer starts spans around units of work. It is deliberately minimal: the agent depends on this interface, and an adapter maps it onto a backend such as OpenTelemetry. Implementations must be safe for concurrent use.

Jump to

Keyboard shortcuts

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