# OpenTelemetry

> Export sandbox metrics, lifecycle events and command spans to any OTLP/HTTP collector — Datadog, Grafana, Honeycomb, New Relic or your own.

Sandboxer pushes your account's telemetry to one OTLP/HTTP endpoint you choose. Nothing to install in your sandboxes.

## What is exported

| Signal | Path | When |
| - | - | - |
| Sandbox and account metrics | `<endpoint>/v1/metrics` | Every 60 seconds |
| Lifecycle events, as log records | `<endpoint>/v1/logs` | When each event occurs |
| Command and agent prompt spans | `<endpoint>/v1/traces` | When each command or prompt finishes |

Data is sent as OTLP/JSON. Every resource has `service.name=sandboxer`; each sandbox is its own resource with
`service.instance.id`, `sandboxer.sandbox.id`, `sandboxer.sandbox.name`, `sandboxer.sandbox.size` and
`sandboxer.account.id`, so each sandbox is its own series in every vendor.

### Metrics

All metrics are gauges. Each export carries the 30-second samples taken since the previous one.

| Metric | Unit | Meaning |
| - | - | - |
| `sandboxer.sandbox.cpu.utilization` | `1` | Share of CPU time busy, 0 to 1 |
| `sandboxer.sandbox.memory.utilization` | `1` | Share of memory in use, 0 to 1 |
| `sandboxer.sandbox.memory.usage` | `By` | Memory in use |
| `sandboxer.sandbox.filesystem.utilization` | `1` | Share of the disk in use, 0 to 1 |
| `sandboxer.sandbox.filesystem.usage` | `By` | Disk space in use |
| `sandboxer.sandbox.io.pressure` | `1` | Share of time stalled on disk I/O, 0 to 1 |
| `sandboxer.account.sandboxes.active` | `{sandbox}` | Sandboxes running now |
| `sandboxer.account.sandboxes.limit` | `{sandbox}` | Sandboxes your plan lets run at once (not on unlimited plans) |
| `sandboxer.account.credit.balance` | `s` | Machine time left (not on unlimited plans) |

Prometheus turns `sandboxer.sandbox.cpu.utilization` into `sandboxer_sandbox_cpu_utilization`.

### Events

Each [lifecycle event](/docs/webhooks) (`sandbox.ready`, `sandbox.hydrated`, `sandbox.error`, `sandbox.archived`,
`sandbox.degraded`, `sandbox.recovered`) is one log record. The event name is in the record's event name and in the
`event.name` attribute. Severity is `INFO`, except `ERROR` for `sandbox.error` and `WARN` for `sandbox.degraded`.
Attributes: `sandboxer.sandbox.id`, `sandboxer.sandbox.name`, `sandboxer.sandbox.state`,
`sandboxer.sandbox.previous_state`, `sandboxer.sandbox.reason` and `sandboxer.event.id` (the webhook event id, to
remove duplicates).

### Spans

- `sandbox.command`: one span per finished command, with `process.command_line` (first 1 KiB), `process.exit.code`,
  `sandboxer.command.mode` and `sandboxer.command.status`. A non-zero exit, timeout or lost command is an error span.
- `agent.prompt`: one span per finished agent prompt, one trace per conversation, with `gen_ai.system` (the agent),
  `gen_ai.request.model` and `sandboxer.prompt.status`. The prompt text is never sent.

A sandbox with monitoring off sends no metrics and no spans; its lifecycle events are still sent.

## Set it up

```bash title="CLI"
sbx telemetry set https://otlp.datadoghq.com -H dd-api-key=$DD_API_KEY
sbx telemetry                    # show the export and its last result
sbx telemetry unset
```

```bash title="curl"
curl -sS -X PUT https://sandboxer.com/api/v1/telemetry \
  -H "Authorization: Bearer $SANDBOXER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"endpoint":"https://otlp.example.com","headers":{"authorization":"Bearer abc123"}}'
```

- The endpoint must be HTTPS on port 443 or 4318 and resolve only to public addresses. A trailing `/v1/metrics`, `/v1/logs` or `/v1/traces` you include is removed.
- Headers (up to 20) are encrypted at rest and never shown again — only their names.
- Setting the export sends one **test span** (`sandboxer.telemetry.test`) immediately; `test` in the response is the collector's answer, so you see a wrong key or endpoint before you wait for data.

## Delivery and errors

| Collector answer | What Sandboxer does |
| - | - |
| `2xx` | Done. A partial-success message shows in `lastError`. |
| `429`, `502`, `503`, `504`, or no answer | Sends the same data again later, after `Retry-After` when the collector sends one. |
| Any other status | Drops that data and shows the error in `lastError`. |

After an outage, at most the last 10 minutes of metrics are sent. Events and spans are retried like
[webhook deliveries](/docs/webhooks), up to 8 attempts. Removing the export drops anything still queued.

## Status

`GET /telemetry` returns `lastExportAt` (the last export the collector accepted) and `lastError` (message, HTTP status, time) so you can tell when credentials expire or a collector rejects data. The dashboard shows the same on **Webhooks & OTel**.

## Vendor examples

| Vendor | Endpoint | Header |
| - | - | - |
| Datadog | `https://otlp.datadoghq.com` (or your site) | `dd-api-key` |
| Grafana Cloud | your stack's OTLP gateway URL | `authorization: Basic <instance:token base64>` |
| Honeycomb | `https://api.honeycomb.io` | `x-honeycomb-team` |
| New Relic | `https://otlp.nr-data.net` | `api-key` |
| Self-hosted collector | `https://otel.example.com:4318` | whatever your collector expects |
