# Debugger

The debugger takes traces, logs and crash reports from your own programs and ties each one to the commit and file it came from.

It is the first part of a larger plan: what is built is the receiving end and the lookup. Probes
that reach into a running program are not built.

## What is built

`ahto serve` listens for OpenTelemetry over OTLP/HTTP on `127.0.0.1:4318`, the port exporters
use by default, so a program that already exports traces needs no change:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 ./your-program
```

ahto keeps what it receives in its own DuckDB file, beside the lake and not in it, for 7 days.
It maps a build to a commit of the lake using the revision Go stamps in the binary (or the
`service.version` the program reports: a tag, a branch or a commit), and a stack frame to the
file and line in that commit.

| Call | What it returns |
|---|---|
| `GET /v1/debug/targets` | the services that sent something |
| `GET /v1/debug/traces`, `/traces/{trace_id}` | find traces, read one |
| `GET /v1/debug/logs` | logs |
| `POST /v1/debug/crashes` | send a crash report |
| `GET /v1/debug/crashes`, `/crashes/{id}` | list crash reports, read one with its code mapped |

## Try it without a program

With `ahto serve` running, send one span by hand and read it back:

```sh
curl -s -X POST http://127.0.0.1:4318/v1/traces -H 'Content-Type: application/json' -d '{"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"demo"}}]},"scopeSpans":[{"spans":[{"traceId":"5b8efff798038103d269b633813fc60c","spanId":"eee19b7ec3c1b174","name":"GET /hello","kind":2,"startTimeUnixNano":"1700000000000000000","endTimeUnixNano":"1700000000050000000"}]}]}]}'
curl -s http://127.0.0.1:8080/v1/debug/traces
```

```text
{}
{"traces":[{"trace_id":"5b8efff798038103d269b633813fc60c","service":"demo","root":"GET /hello","start":"2023-11-14T22:13:20Z","duration_us":"50000","spans":1,"error":false,"error_message":"","version":""}],"next_page_token":""}
```

## Settings

| Setting | Default | What it does |
|---|---|---|
| `--otlp-listen` (`AHTO_OTLP_LISTEN`) | `127.0.0.1:4318` | where `ahto serve` takes OTLP; `off` turns it off |
| `debug.path` | `debug/debug.duckdb` in ahto's data folder | the store; one process opens it, `ahto serve` |
| `debug.retention_days` | `7` | days kept, 1 to 90, counted from when ahto received the data |

The listener is on loopback only and is never started in public mode or with
`protocol.allow_remote`.

## Not built yet

Probes (breakpoints and snapshots in a running program) are planned. The design is
RFC 0021.