observability

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package observability turns cross-cutting instrumentation into an ai.Middleware via a set of callback hooks. It is dependency-free: it emits structured events but does not bind to any telemetry backend, leaving OpenTelemetry or logging integration to the caller (or a future subpackage).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(h Hooks) ai.Middleware

Middleware returns an ai.Middleware that invokes h around each call.

Types

type CallInfo

type CallInfo struct {
	Provider ai.Provider
	ModelID  string
	Request  ai.Request
	// Streaming reports whether the call is Stream (true) or Generate.
	Streaming bool
}

CallInfo describes an in-flight request passed to the hooks.

type Hooks

type Hooks struct {
	// OnStart fires before the underlying call. The returned context is used
	// for the call, so a hook may attach a span or values; return ctx
	// unchanged to opt out.
	OnStart func(ctx context.Context, info CallInfo) context.Context
	// OnStreamEvent fires for each streaming event (never for Generate).
	OnStreamEvent func(ctx context.Context, info CallInfo, ev ai.StreamEvent)
	// OnFinish fires after a call completes without error.
	OnFinish func(ctx context.Context, result Result)
	// OnError fires when a call (or stream) fails.
	OnError func(ctx context.Context, info CallInfo, err error)
}

Hooks receives lifecycle callbacks. Every field is optional; nil hooks are skipped. Hooks run on the calling goroutine, so keep them fast and non-blocking.

type Result

type Result struct {
	CallInfo
	// Response is set for Generate calls; nil for Stream (use OnStreamEvent
	// and the message_end usage for streaming accounting).
	Response *ai.Response
	// Usage is the token usage observed (from the response, or accumulated
	// from stream events).
	Usage ai.Usage
	// FinishReason is the normalized stop reason.
	FinishReason ai.FinishReason
	// Duration is the wall-clock time from call start to completion.
	Duration time.Duration
}

Result is passed to Hooks.OnFinish after a successful call.

Jump to

Keyboard shortcuts

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