# Performance traces

The server can send OpenTelemetry traces of requests, sync and background jobs to a collector the operator runs or chooses. It's off unless the operator sets one up, and a person's own requests are traced only if they turn it on.

## Turning it on

The operator sets:

| Variable | What |
|---|---|
| `HATCH_OTEL_ENDPOINT` | The collector's OTLP over HTTP address, like `https://otlp.example.com` (a full `.../v1/traces` address works too). Spans go to `<endpoint>/v1/traces` as JSON. No user, password or query in it: put a key in the headers. |
| `HATCH_OTEL_HEADERS` | Optional `key=value,key=value` headers, the way `OTEL_EXPORTER_OTLP_HEADERS` has them, for the collector's API key |

Then:

- **Background jobs and scheduled tasks** are the server's own work, so they're traced.
- **A person's requests** are traced only when they turn on "Send performance traces" in Settings, Privacy, and only while signed in. Their app then sends `X-Hatch-Trace: 1` with each request to the server. Without it, nothing about their requests is recorded, not even child spans like sync. At most 50 request traces a second are kept.

The server says on every answer that it traces (`X-Hatch-Tracing: 1`). The switch shows only then, so an app never sends the header to a server that would refuse it. It's off until turned on, stays on that device, and turns off at sign-out.

Limits: background jobs a person's action started (a SIEM delivery, an email) are the server's own work and traced whenever a collector is set. Live editing runs over a WebSocket, which can't carry the header, so it isn't traced. A request's span ends when its handler returns, not when a long download finishes.

## What a span holds

- Its name: a route pattern (`PUT /api/sync/vaults/:vault/file`, never the real path), or a job or task name (`sync.write`, `activity.stream`).
- Start and end time, and whether it failed. A failure records only the kind of error (`TypeError`), never its message.
- The status code, and counts such as bytes written or changes returned.

Never a body, a note, a file or vault name, an email, an address, an account id, a request id or a header. Without a request id or account, a span can't be matched to the access log or a person.

## How it behaves

- Spans are sent in batches every 5 seconds, or sooner at 512.
- At most 2,048 wait at a time. If the collector is slow or down, new spans are dropped and the server logs how many (never the collector's address or the error's text). Tracing never holds up a request.
- At shutdown the last spans get two seconds, then the server exits.
- No OpenTelemetry SDK: the exporter is about 200 lines in `apps/server/src/tracing.ts`, so the server's dependencies stay as they are.

## Adding a span

Inside a route or job, use the tracer from the module context:

```ts
const result = await ctx.tracer.span('sites.publish', { 'hatch.files': files.length }, async (span) => {
  const r = await publish(files)
  span.set({ 'hatch.bytes': r.bytes })
  return r
})
```

Outside a traced request or job it just runs the function. Only put route patterns, counts and booleans in attributes.

Work a request starts that isn't that request's own (draining the queue) runs in `ctx.tracer.detach(fn)`, so it never joins that person's trace.
