# OpenTelemetry

> Export zelk's traces and metrics over OTLP, configured with the standard OpenTelemetry variables.

With `-otel`, zelk exports traces and metrics over OTLP to a collector of
your choice: a span for each execution, model turn, model
call and tool call, and counters for tokens, tool calls, permission
decisions, compactions and retries. Every host takes the flag: the terminal
UI, `zelk -p`, `zelk exec` and `zelk acp`.

## Turn it on

Point the standard variables at your collector and pass `-otel`:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 zelk exec -otel "fix the build"
OTEL_EXPORTER_OTLP_PROTOCOL=grpc OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 zelk -otel
```

To export on every start, turn it on in the terminal UI's Settings ›
Telemetry, which also chooses the content spans carry, or save
`"otel": true` (and `"otel_capture"`) in `~/.config/zelk/config.json`. A
running terminal UI keeps the telemetry it started with; a change applies
from the next start.

zelk reads the variables that decide the pipeline's shape:

| Variable | Values |
|---|---|
| `OTEL_SDK_DISABLED` | `true` exports nothing |
| `OTEL_TRACES_EXPORTER`, `OTEL_METRICS_EXPORTER` | `otlp` or `none` |
| `OTEL_EXPORTER_OTLP_PROTOCOL`, and its per-signal forms | `http/protobuf` (the default) or `grpc` |
| `OTEL_SERVICE_NAME` | default `zelk`, or `zelk exec` and `zelk acp` for those commands |
| `OTEL_RESOURCE_ATTRIBUTES` | extra resource attributes |

The OpenTelemetry exporters read the rest themselves: endpoints, headers,
TLS, compression, timeouts, the sampler and the export interval. A value
zelk can't honour, such as the `console` exporter, a list of exporters or
`http/json`, is a usage error at start, not a quiet fallback.

Over OTLP/HTTP, the exporter uses the same proxy and extra certificates as
zelk's other connections ([Proxies and certificates](/cli/credentials/#proxies-and-certificates)),
unless `OTEL_EXPORTER_OTLP_CERTIFICATE` or a client certificate variable
names its own. Over gRPC, it follows `HTTPS_PROXY` itself.

Export errors never interrupt a run or write over the terminal UI: zelk
keeps them and reports how many there were, and the first, when it exits.

## Traces

```text
zelk.execution
├─ zelk.hook                       when no turn is open
├─ zelk.compaction                 when no turn is open
└─ zelk.turn                       one per model turn
   ├─ zelk.model_call
   ├─ zelk.compaction
   ├─ zelk.hook                    hooks outside a tool span
   └─ zelk.tool                    one per observed tool call
      ├─ zelk.hook                 PreToolUse, PermissionRequest
      ├─ zelk.classifier           auto mode's reviewer
      ├─ zelk.tool.awaiting_approval
      └─ zelk.tool.run             a subagent nests here
         └─ zelk.search            a native WebSearch's own model call
```

A subagent's execution nests under the `zelk.tool.run` that started it, so a
whole delegation tree is one trace. A session resumed later, in any
process, starts with a link to its previous execution. Spans carry
identifiers, models, outcomes and token counts.

Suspension, caller context cancellation or deadline expiry, and a recorded
interruption leave the execution span's status unset. Its `zelk.outcome` is
`suspended`, `stopped` or `interrupted`, respectively. Execution failures
such as a persistence error or lease loss set error status and record an
exception.

**Joining a trace.** Set `TRACEPARENT` (and `TRACESTATE`) to make zelk's
executions children of whatever started it, such as a CI job. zelk passes
`TRACEPARENT` on to the commands it runs, so a program that speaks W3C
Trace Context joins the trace too.

## Metrics

All are counters, under the scope `github.com/barevoidco/zelk`:

| Metric | Counts |
|---|---|
| `zelk.tokens.input`, `zelk.tokens.output` | reported token usage for model calls, including the reviewer and native web search |
| `zelk.tokens.cache_read`, `zelk.tokens.cache_write` | cached input tokens read and written |
| `zelk.tool.calls` | tool attempts that return, by tool and outcome |
| `zelk.permission.decisions` | `PermissionDecided` records by verdict and step. Verdicts are `ask`, `deny` and `classify` when an inconclusive review sends a call to approval. Allows add nothing to this counter |
| `zelk.compactions` | successful compactions |
| `zelk.retries` | retries of model and compaction provider calls |

## Content

Content capture is off by default. Error status and exception events can
still carry error messages from providers or hooks, so those messages may
include sensitive text. `-otel-capture` opts in to the following fields,
comma-separated or repeated.

| Value | Records |
|---|---|
| `prompts` | the system prompt and portable request history for model and compaction calls, as a `zelk.prompt` event |
| `tool-inputs` | each tool call's input |
| `tool-outputs` | each tool call's output text |
| `provider-bodies` | the provider's inline raw payload per response item, with items carrying reasoning redacted |

```sh
zelk exec -otel -otel-capture prompts,tool-inputs "fix the build"
```

`-otel-capture` requires `-otel`; `"otel_capture"` saves it.

## In Go

The `zelk` command builds its pipeline with the
[`adapters/otel`](/reference/go/adapters/otel/) module, which a Go host uses
with its own tracer and meter providers through `zelk.WithObserver`. zelk
never starts or stops them. The [`observe`](/reference/go/observe/) package
is the seam beneath it, for hosts with other telemetry.

## Where to go deeper

- [Settings](/reference/settings/): `otel` and `otel_capture`
- [The CLI reference](/reference/cli/): `-otel` and `-otel-capture` on each command
