# OTLP binding

> This document is part of the sessionpipe protocol and is licensed under [CC BY 4.0](https://sessionpipe.org/legal/#spec-license). The words MUST, MUST NOT, SHOULD, SHOULD NOT and MAY are used as in RFC 2119. Status: **Draft** (see [VERSIONING.md](https://sessionpipe.org/protocol/versioning/)).

sessionpipe events ([PROTOCOL.md](https://sessionpipe.org/protocol/)) can be exported directly to any OpenTelemetry Protocol (OTLP) receiver — an OpenTelemetry Collector, Grafana Alloy / Tempo / Loki, Datadog, Honeycomb, or Jaeger — over **OTLP/HTTP JSON** (`application/json`), with no custom sessionpipe receiver.

## 1. Privacy first

An OTLP sink is governed by the exact same privacy contract as any other sink ([PRIVACY.md](https://sessionpipe.org/protocol/privacy/)):

- Every event MUST pass through `filter(event, tier, pii)` before OTLP serialization.
- `secrets@1` runs above tier 0 and cannot be disabled; `pii@1` runs when `--pii` is enabled on the sink.
- Fields stripped by the tier allowlist or by `pii@1` (such as prompts below tier 2, tool inputs/outputs below tier 3, or `account_id` under `pii@1`) MUST NOT appear as OTLP attributes.

## 2. Sink URLs and signals

An OTLP sink URL uses one of two scheme forms (case-insensitive):

- `otlp:http://…` / `otlp:https://…`
- `otlp+http://…` / `otlp+https://…`

Embedded URL credentials (`otlp:https://user:pass@host`) MUST be rejected; pass credentials via `--token` or `--header` instead. Plain `http://` to a non-loopback host SHOULD trigger a warning.

The path selects which OTLP/HTTP JSON signals are exported:

| URL path | Signals exported |
|----------|------------------|
| Base URL (e.g. `otlp:http://localhost:4318`, `otlp:https://otlp.grafana.net/otlp`) | Both `/v1/logs` (all events) and `/v1/traces` (span-bearing events), appended to the base path. Batches with no span-bearing events POST only `/v1/logs`. |
| Ends with `/v1/logs` | Logs only, to that exact URL. |
| Ends with `/v1/traces` | Traces only, to that exact URL (use this for trace-only backends such as Jaeger). Every event in the batch is emitted as a span (instant span when no duration is present, with unique `spanId` for `*.started` events so `(traceId, spanId)` never collides with `*.ended`). |

OTLP sinks do not query `/.well-known/sessionpipe`.

## 3. Authentication, headers, and redirects

Every request sends `Content-Type: application/json` (which user headers MUST NOT override).

- If the sink has a `token` (stored in the OS keychain or `0600` config), the sender sets `Authorization: Bearer <token>`, unless `headers` explicitly provides an `authorization` header.
- Extra HTTP headers (`--header "Name: Value"` or `--header "Name=Value"`, OTLP sinks only) are sent on every request — supporting Grafana Cloud (`Authorization: Basic …`), Honeycomb (`x-honeycomb-team`, `x-honeycomb-dataset`), Datadog (`dd-api-key`), and custom collector headers. Header values live in the OS secret store (`sessionpipe secrets move keychain|secret-service`) alongside `token`, leaving only header names in `config.json`.
- Header names MUST match `[A-Za-z0-9-]+`, values MUST contain only printable ASCII and HTAB (`[\t\x20-\x7e]*`), and hop-by-hop / framing headers (`content-type`, `content-length`, `host`, `transfer-encoding`, `connection`) MUST be refused. Error messages MUST NOT echo header values.
- Senders MUST set `redirect: "manual"` and MUST NOT follow HTTP `3xx` redirects (preventing cross-origin credential leakage and SSRF). A `3xx` response pauses the sink.

## 4. Correlation: trace IDs, span IDs, and links

All logs and spans belonging to one session share deterministic, non-zero OTLP hex IDs so backends correlate logs and traces automatically without shared state across workers:

| Identifier | Derivation |
|------------|------------|
| `traceId` (32 hex) | First 16 bytes of `sha256("sessionpipe.trace:" + harness.name + ":" + session.id)` |
| Session root `spanId` (16 hex) | First 8 bytes of `sha256("sessionpipe.session-span:" + harness.name + ":" + session.id)` |
| Turn `spanId` (16 hex) | First 8 bytes of `sha256("sessionpipe.turn-span:" + harness.name + ":" + session.id + ":" + turn_id)` |
| Tool `spanId` (16 hex) | First 8 bytes of `sha256("sessionpipe.tool-span:" + harness.name + ":" + session.id + ":" + call_id)` |
| Delegation `spanId` (16 hex) | First 8 bytes of `sha256("sessionpipe.delegation-span:" + harness.name + ":" + session.id + ":" + ask_id)` |
| Other event `spanId` (16 hex) | First 8 bytes of `sha256("sessionpipe.span:" + event.id)` |

Span hierarchy and links:

- `session.started` and `session.backfill` are root spans (`spanId` = session root, no `parentSpanId`). When `session.parent_id`, `session.delegated_by`, or `session.launched_by` is present, they attach OTLP **Span Links** to the related session's `(traceId, sessionSpanId)` with `sessionpipe.link.kind` (`parent`, `delegated_by`, `launched_by`).
- `turn.ended` has `parentSpanId` = session root span and `kind = CLIENT (3)`.
- `tool.ended` has `parentSpanId` = turn span (when `turn_id` is present) else session root span, and `kind = INTERNAL (1)`.
- `delegation.ended` has `parentSpanId` = session root span, `kind = CLIENT (3)`, and links to the target session when `data.to` is `<harness>:<id>` (`sessionpipe.link.kind = "delegation_target"`).

## 5. Resource, scope, and attributes

Events in a batch are grouped into `resourceLogs` / `resourceSpans` by `(harness.name, harness.version, session.machine)`.

### Resource attributes

| Attribute | Value |
|-----------|-------|
| `service.name` | `"sessionpipe"` |
| `gen_ai.agent.name` | `harness.name` |
| `sessionpipe.protocol.version` | `1` (int) |
| `sessionpipe.harness.name` | `harness.name` |
| `sessionpipe.harness.version` | `harness.version` (when present) |
| `host.name` | `session.machine` (when present; hashed when `pii@1` ran) |

InstrumentationScope is `{ "name": "sessionpipe", "version": "<sender version>" }`.

### Common event attributes

| Attribute | Source |
|-----------|--------|
| `gen_ai.conversation.id` | `session.id` |
| `gen_ai.request.model` | `session.model` |
| `vcs.repository.url.full` | `session.repo` |
| `vcs.ref.head.name` | `session.branch` |
| `sessionpipe.event.id` | `event.id` |
| `sessionpipe.event.type` | `event.type` |
| `sessionpipe.event.tier` | `event.tier` (int) |
| `sessionpipe.harness.event` | `harness.event` |
| `sessionpipe.session.id` | `session.id` |
| `sessionpipe.session.short_id` | 8-hex short id ([PROTOCOL.md §3](https://sessionpipe.org/protocol/#3-the-session-block)) |
| `sessionpipe.session.seq` | `session.seq` (int) |
| `sessionpipe.session.cwd` | `session.cwd` |
| `sessionpipe.session.title`, `title_source`, `url`, `account_id`, `attended`, `effort`, `parent_id`, `launched_by`, `delegated_by` | matching `session.*` fields when present |
| `sessionpipe.privacy.pii` | `privacy.pii` (bool) |
| `sessionpipe.privacy.rulesets` | `privacy.rulesets` (string array, when non-empty) |

### Per-type mapping

- **`turn.started` / `turn.ended`**: `gen_ai.operation.name = "invoke_agent"`, `sessionpipe.turn.*`, `sessionpipe.duration_ms` (`ms`), `error.type` on error. `turn.ended` span is named `invoke_agent <harness>` with `startTimeUnixNano = time - ms` and `endTimeUnixNano = time`.
- **`turn.transcript` / `turn.progress`** (tier 2+): `gen_ai.operation.name = "invoke_agent"`, `sessionpipe.turn.number`, `gen_ai.input.messages` (`user`, redacted), `gen_ai.output.messages` (`assistant`, redacted).
- **`tool.started` / `tool.ended`** (tier 1+; input/output at tier 3): `gen_ai.operation.name = "execute_tool"`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `sessionpipe.tool.ok`, `sessionpipe.duration_ms`, `error.type` on failure, `gen_ai.tool.call.arguments` (`input`, tier 3), `gen_ai.tool.call.result` (`output`, tier 3). `tool.ended` span is named `execute_tool <tool>` with `startTimeUnixNano = time - ms`.
- **`files.changed`** (tier 1+): `sessionpipe.files.paths` (string array), `sessionpipe.files.count` (int).
- **`attention.needed` / `attention.cleared`**: `sessionpipe.attention.id`, `kind`, `clearance`, plus `gen_ai.tool.name`, `sessionpipe.attention.message`, and `sessionpipe.subagent.id` at tier 1+.
- **`subagent.started` / `subagent.ended`** (tier 1+): `sessionpipe.subagent.id`, `type`, `background`.
- **`context.compacted`** (tier 1+): `sessionpipe.compaction.trigger`.
- **`limits.observed`** (tier 1+): `sessionpipe.limits.plan`, `account_id` (absent under `pii@1`), and per-window `sessionpipe.limits.window.<id>.used_percent`, `resets_at`, `window_minutes`.
- **`delegation.started` / `delegation.ended`**: `gen_ai.operation.name = "invoke_agent"`, `sessionpipe.delegation.*`, `sessionpipe.duration_ms`, `error.type` on failure.
- **Unknown types**: each key in `data` is exported as `sessionpipe.data.<key>`.

### Wire encoding rules

All 64-bit integer timestamps (`timeUnixNano`, `observedTimeUnixNano`, `startTimeUnixNano`, `endTimeUnixNano`) are non-negative decimal strings (`>= 0`) and safe-integer attributes (`Number.isSafeInteger`) are encoded as `intValue` decimal strings (non-safe integers and floats as `doubleValue`, and `limits.observed` `used_percent` always as `doubleValue`) per the OTLP Protobuf JSON mapping. Non-finite numbers (`NaN`, `Infinity`) are omitted. LogRecords set `eventName = "sessionpipe." + event.type` and severity `DEBUG (5)` for `session.heartbeat`, `ERROR (17)` for failed tools/turns/delegations and error attentions, `WARN (13)` for non-error attentions and interrupted turns, and `INFO (9)` otherwise.

Conformance vectors: `conformance/otlp-mapping.json`.

## 6. Delivery, retries, and errors

OTLP sinks use the same outbox, cursor, pacing (1 request/second per machine), and retry behavior as HTTP sinks ([HTTP.md §2](https://sessionpipe.org/protocol/http/#2-events-post-events)). Response body reads are bounded to 64 KiB, and `Retry-After` waits are capped at 1 hour.

| Status | Sender behaviour |
|-------:|------------------|
| `200`, `202` | Advance cursor. Record any `partialSuccess.rejectedLogRecords` / `rejectedSpans` without retrying the batch. |
| `3xx` | Do not follow redirects; pause the sink (`doctor` reports it). |
| `400` | Drop the batch and log the error. |
| `401` | Pause the sink (`doctor` reports it). |
| `413` | Halve the batch and retry; drop a single event that still fails. |
| `429` | Wait per `Retry-After` (seconds or HTTP date, capped at 1 h; default 60 s) and retry. |
| `5xx`, timeout, network error | Retry with exponential backoff (2 s · 10 s · 60 s · 5 min · 30 min) without advancing the cursor. |

In dual-signal (base URL) mode, if one signal succeeds (`200`/`202`) and the other returns `404`, `405`, or `501` (endpoint does not implement that signal), the batch is acknowledged so the accepted signal is not duplicated in an infinite retry loop. When both signals fail, action precedence is `pause` > `retry` (taking the maximum `Retry-After`) > `split` > `drop`.
