# OpenTelemetry export

The worker can send one trace per turn, with token, cost and tool metrics, to a collector the user names. The code is `openprogram/operations/telemetry/`; the user page is [OpenTelemetry export](https://openprogram.io/docs/server/opentelemetry.html).

## From events to spans

1. **Event bus and usage hook** turn, model response and tool events; one UsageEvent per model call
2. **Recorder** spans and metrics
3. **Collector** over OTLP

| Span | Opens on | Closes on | Parent |
| --- | --- | --- | --- |
| `invoke_agent openprogram` | `turn.start` | `turn.end` or `turn.failed` | Root |
| `chat <model>` | `model.response_started`; renamed and filled by the usage hook | `model.response_completed` | The turn in the event's `turn` metadata, else the session's open turn |
| `chat <model>`, outside the loop | One usage event whose call kind is not the loop's (title, compaction, memory, a model asked from a tool) | One usage event whose call kind is not the loop's (title, compaction, memory, a model asked from a tool) | The session's open turn, else root |
| `execute_tool <tool>` | `tool.before` | `tool.after` | The turn |

Event timestamps become span times, so a span measures what the runtime saw. The usage hook runs inside the provider request's context, which is how it finds the open `chat` span: the current turn ContextVar, then the session. `turn.failed` was added to the registry for this: the loop's error path had no event.

## Decisions

| Decision | Follows | Why |
| --- | --- | --- |
| Off by default; nothing has a default destination | Claude Code (`CLAUDE_CODE_ENABLE_TELEMETRY`), Cline, Hermes (no default endpoint ships) | Codex ships a built-in metrics destination on by default; a local single-user tool has no business sending anything unasked |
| A setting and an environment variable turn it on; the standard `OTEL_*` variables keep working | Codex (`[otel]` in config), Claude Code (environment) | The App starts the worker without the user's shell environment, so a setting is needed; collectors are documented in `OTEL_*` terms, so those must work too. `OTEL_SDK_DISABLED` wins over both |
| OTLP over HTTP/protobuf by default; gRPC as a separate extra | Claude Code (defaults to `http/protobuf`); Codex and Cline support both | Every common collector listens on 4318; grpcio is a large native wheel for five platforms. The Python SDK has no OTLP JSON exporter, so `http/json` falls back to protobuf |
| Traces and metrics; no logs signal | Codex (trace and metrics exporters) | Claude Code sends its events as logs. A turn, its model calls and its tool calls are a tree, and spans carry the same attributes with the parent link; a logs copy would duplicate them |
| Span names and `gen_ai.*` attributes from the GenAI semantic conventions | OpenTelemetry GenAI conventions; no peer | Peers use their own names; the conventions make collectors' GenAI views (Langfuse, Phoenix, Datadog) read the trace as it is |
| `token.usage` with a `type` label, `cost.usage` in USD | Claude Code (`claude_code.token.usage`, `claude_code.cost.usage`) | Counters sum directly in dashboards; one metric with a type label keeps cache tokens beside input and output. Every metric name starts with `openprogram.` |
| `tool.call.count` and `.duration` by tool and outcome | Codex (`codex.tool.call`, `codex.tool.call.duration_ms`) | The two questions asked of tools: how often, how slow, how often failing |
| No content by default; one opt-in for prompts, one for tool arguments and results | Claude Code (`OTEL_LOG_USER_PROMPTS`, `OTEL_LOG_TOOL_DETAILS`), Codex (`log_user_prompt`) | Not Gemini CLI, which logs prompts unless told not to. The variables are `OPENPROGRAM_OTEL_LOG_*`, so a Claude Code variable in the shell does not opt OpenProgram in |
| Opted-in text loses secrets and is cut at 64 KiB | Codex (64 KiB cap on exported text), Hermes (`redact_bounded`) | Reuses `state/secret_removal`: field names, bearer tokens, `sk-` keys, URL credentials, private keys, password assignments |
| A subscriber on the bus and the usage hook, not calls in the loop | Hermes (a fail-isolated subscriber on its monitoring events) | Codex threads a telemetry object through the session; here the bus and the ledger already carry every fact, so the loop stays free of OpenTelemetry and the recorder sits in `operations`, above everything it reads |
| SDK in the optional `telemetry` extra, imported only when on; the App carries it through `all` | Hermes (`hermes-agent[otlp]`, lazy import) | A worker with export off never imports it; a missing package logs one line and export stays off |
| Providers of its own, not the global ones | Hermes | A library that calls `opentelemetry.trace` does not start exporting through ours |
| Fail open everywhere | All peers | Handlers catch everything; at most 512 open spans per kind, the oldest ended as `abandoned`; at stop, open spans end as `abandoned` and the flush waits at most 1.5 s of the 5 s stop budget |
| A collector on this computer is reached without the user's proxy | Claude Code (fixed export to `localhost:4318` going through a proxy) | A corporate `HTTPS_PROXY` cannot reach the user's loopback collector |
| Resource: `service.name=openprogram` and the version; no host attributes | Claude Code (`OTEL_RESOURCE_INCLUDE_HOST` off by default) | `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` still apply |

## Implementation status

- Prompt text comes from `chat.before_send`, which only OpenProgram's own windows emit; a channel turn carries its prompt length only.
- Settings apply at the next worker start.
- The App gets the SDK packages with the next feature release, since they change the runtime layer.
