Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/observe"

Package observe is zelk’s neutral observability seam: a small Observer interface for spans and metrics that core calls and a host implements. Core has no OpenTelemetry dependency; the adapter in the separate github.com/barevoidco/zelk/adapters/otel module builds an Observer from the host’s own providers. With no Observer, zelk records nothing.

const (
SpanExecution = "zelk.execution"
// SpanTurn covers one model turn: its model call and what the turn's
// tool calls did, under the execution.
SpanTurn = "zelk.turn"
SpanModelCall = "zelk.model_call"
SpanCompaction = "zelk.compaction"
// SpanTool groups the observed steps of a tool call, starting with its
// first observed hook, review, approval wait or run.
SpanTool = "zelk.tool"
SpanAwaitingApproval = "zelk.tool.awaiting_approval"
SpanToolRun = "zelk.tool.run"
SpanHook = "zelk.hook"
SpanClassifier = "zelk.classifier"
// SpanSearch covers one native web search under the WebSearch tool's
// run: a model call of its own, whose tokens count with the turn's.
SpanSearch = "zelk.search"
)

The span names zelk uses.

const (
EventPrompt = "zelk.prompt"
EventProviderResponse = "zelk.provider.response"
AttrToolInput = "zelk.tool.input"
AttrToolOutput = "zelk.tool.output"
)

The content events and attributes Capture opts in to.

const (
MetricInputTokens = "zelk.tokens.input"
MetricOutputTokens = "zelk.tokens.output"
MetricCacheReadTokens = "zelk.tokens.cache_read"
MetricCacheWriteTokens = "zelk.tokens.cache_write"
MetricToolCalls = "zelk.tool.calls"
MetricPermissions = "zelk.permission.decisions"
MetricCompactions = "zelk.compactions"
MetricRetries = "zelk.retries"
)

The metrics zelk counts.

func WithAttrs(ctx context.Context, attrs ...Attr) context.Context

WithAttrs returns ctx carrying attrs, added to every span zelk starts under it, after any ctx already carries. They are for identity, such as the end user or tenant an execution acts for, so an audit trail can find every span of their work; they aren’t added to metrics, whose cardinality they would explode.

func WithSpanContext(ctx context.Context, c SpanContext) context.Context

WithSpanContext returns ctx carrying the span zelk is in, so code that only sees a context, such as a tool starting a command, can pass the trace on (TRACEPARENT).

type Attr struct {
Key string
Value any
}

Attr is an attribute: a key and a string, int64, float64 or bool value.

func Attrs(ctx context.Context) []Attr

Attrs returns the attributes ctx carries for spans.

func Bool(k string, v bool) Attr

Bool is a bool attribute.

func Int(k string, v int64) Attr

Int is an int64 attribute.

func String(k, v string) Attr

String is a string attribute.

type Capture struct {
// Prompts records the system prompt and portable request history for
// the driver's model and compaction calls, as a zelk.prompt event.
Prompts bool
// ToolInputs records each tool call's input as zelk.tool.input on its
// zelk.tool span.
ToolInputs bool
// ToolOutputs records each tool call's output text as
// zelk.tool.output on its zelk.tool.run span.
ToolOutputs bool
// ProviderBodies records the provider's raw payload of each item a
// model call returns, as a zelk.provider.response event on its span.
// An item carrying reasoning is redacted.
ProviderBodies bool
}

Capture opts in to recording content on spans. Content can carry anything a user, a file or a model said, so each kind is off until the host turns it on.

type Observer interface {
// StartSpan starts a span named name and returns a context carrying
// it. Its parent is opts.Parent if valid, else the span in ctx, if
// any, so a span the host started before calling zelk becomes zelk's
// root's parent.
StartSpan(ctx context.Context, name string, opts SpanOptions) (context.Context, Span)
// Add adds n to the counter named metric.
Add(ctx context.Context, metric string, n int64, attrs ...Attr)
}

Observer receives zelk’s spans and metrics. zelk calls it only for live work, never while replaying a log. Implementations must be safe for concurrent use and must not block: they run on zelk’s own goroutines.

type Span interface {
// SetAttrs adds attributes.
SetAttrs(attrs ...Attr)
// AddEvent records a named event at the current time.
AddEvent(name string, attrs ...Attr)
// End ends the span. A non-nil err sets error status. Nil leaves the
// status unset.
End(err error)
// Context identifies the span.
Context() SpanContext
}

Span is a started span.

type SpanContext struct {
TraceID [16]byte
SpanID [8]byte
}

SpanContext identifies a span within its trace, as W3C Trace Context does: a 16-byte trace ID and an 8-byte span ID.

func FromContext(ctx context.Context) (SpanContext, bool)

FromContext returns the span ctx carries, if any.

func ParseIDs(traceID, spanID string) (SpanContext, error)

ParseIDs parses hex trace and span IDs, as a log records them.

func (c SpanContext) IDs() (traceID, spanID string)

IDs returns the hex trace and span IDs.

func (c SpanContext) Traceparent() string

Traceparent renders the span as a W3C traceparent header value, sampled.

func (c SpanContext) Valid() bool

Valid reports whether both IDs are non-zero.

type SpanOptions struct {
// Parent, if valid, is the span's parent, overriding the span in the
// context. zelk sets it for spans whose parent outlives one call, such
// as a tool call's span, whose permission check, approval wait and run
// are separate steps.
Parent SpanContext
Attrs []Attr
// Links are spans this one follows from, such as the execution a
// resumed execution takes over from.
Links []SpanContext
}

SpanOptions are a span’s parent, attributes and links when it starts.