# Observability

> Trace and count what sessions do with OpenTelemetry or your own observer, and route zelk's logs.

zelk records nothing outside the session log unless you give it somewhere
to report. It never starts, configures or stops your telemetry.

## OpenTelemetry

The adapter is its own module, `github.com/barevoidco/zelk/adapters/otel`, so
core doesn't depend on OpenTelemetry. Pass it your tracer and meter
providers:

```go
// adapters/otel/example_test.go
spans := tracetest.NewSpanRecorder()
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spans))
ctx := context.Background()

p, err := replay.New(provider.Caps{}, replay.Reported("met", "Hi."))
if err != nil {
	panic(err)
}
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeBypass),
	zelk.WithObserver(zelkotel.New(tp, nil)))
if err != nil {
	panic(err)
}
s, err := a.Session(ctx, "traced-1")
if err != nil {
	panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Say hi.")))
if err != nil {
	panic(err)
}
if _, err := x.Wait(ctx); err != nil {
	panic(err)
}
if err := a.Close(ctx); err != nil {
	panic(err)
}
if err := tp.Shutdown(ctx); err != nil {
	panic(err)
}
var names []string
for _, s := range spans.Ended() {
	names = append(names, s.Name())
}
slices.Sort(names)
fmt.Println(slices.Compact(names))
// Output: [zelk.execution zelk.model_call zelk.turn]
```

Spans start from the context you pass, so a span your program started
becomes the execution's parent. A resumed execution links back to the
earlier one through IDs recorded in the log, even in another process.
Subagents' spans join their parent's trace. Suspension, caller context
cancellation or deadline expiry, and recorded interruption leave the
execution span's status unset and set `zelk.outcome` to `suspended`,
`stopped` or `interrupted`, respectively. Execution failures such as
persistence errors and lease loss set error status and record an exception.

`WithCapture` opts in separately to prompts, tool inputs, tool output text
and raw provider response items. Raw items that carry reasoning are redacted.
Capture is off by default. Error messages are reported independently of
capture. `WithTraceAttrs` adds your own span attributes.

## Your own observer

`WithObserver` takes any [`observe.Observer`](/reference/go/observe/): two
methods, one to start spans and one to add to counters. Observing never
fails a session.

## Logs

`WithLogger` takes a `*slog.Logger` for what zelk has to say outside a
session, such as a background failure. Without one, zelk logs nothing; it
never writes to standard output or error.

## The log itself

Everything a session did is in its log, and the log is the audit record:
`Session.Events` reads it, and [`transcript`](/reference/go/transcript/)
renders it as text or Markdown.

## Next

[Testing your agent](/go/testing/) checks it without a key or a network.
