Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

04_observability.md

61 lines5.0 KB

1# Observability
2​
3Server observability has three outputs: pretty stdout logs (not persisted locally, except SSH-managed launches at `~/.t3/ssh-launch/<state>/server.log`), a local NDJSON trace file (always on), and optional OTLP export (traces, metrics, logs).
4​
5## Local traces and event-loop stalls
6​
7Default trace paths (`server.trace.ndjson`):
8​
9- Production / explicit `--home-dir` / `T3CODE_HOME`: `<home>/userdata/logs/server.trace.ndjson`
10- Linked worktree dev run: `<worktree>/.t3/userdata/logs/server.trace.ndjson`
11- Non-worktree dev run: `~/.t3/dev/logs/server.trace.ndjson`
12​
13Summarize counts, rates, and latency percentiles offline (even while a server is stalled or stopped):
14​
15```sh
16t3 trace summary --since 30m --limit 40
17# Set T3CODE_TRACE_FILE=/path/to/server.trace.ndjson for dev or copied traces
18```
19​
20Logs emitted inside an active span (`Effect.log*`) attach as span events unless `T3CODE_OTLP_LOGS_URL` is configured (which exports all log records directly to avoid duplicates).
21​
22Event-loop stalls (>2 s) write `server.eventLoop.stall` spans and append to `event-loop-stalls.ndjson` beside the trace file. SQLite spans carry `db.connection` (`writer`/`reader`) and `db.lock_wait_ms` (queue wait off the event loop—subtract it before blaming query speed). Metrics live only in-process unless OTLP export is enabled.
23​
24At trace level Info or above (the default), with timing enabled, the local server file samples routine spans: SQLite, workflow runs (step code, retry schedules, cleanup and the agent threads a run starts are excluded; spans an author adds in the workflow body itself are sampled), and spans marked with `routineSpan` from `@t3tools/shared/observability` (high-volume lookups and sweeps). A routine span that succeeds in under 10 ms without events is kept only when its own span ID ends in `00` (about one in 256). The key is per span, not per trace or parent, because background fibers share the server's boot trace and recurring ticks reuse one parent span. Counts and averages for these spans in the local file and Settings → Diagnostics are therefore a biased sample. `T3CODE_TRACE_MIN_LEVEL=Debug` keeps everything. This sampling does not apply to OTLP export.
25​
26## Local OTLP stack (Grafana LGTM)
27​
28```sh
29docker run --name lgtm -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti grafana/otel-lgtm
30```
31​
32Open `http://localhost:3000` (`admin`/`admin`), export OTLP URLs in the shell, and restart the server from that same shell (packaged desktop apps launched from Finder/Dock/Start Menu do not inherit shell env vars):
33​
34```sh
35export T3CODE_OTLP_TRACES_URL=http://localhost:4318/v1/traces
36export T3CODE_OTLP_METRICS_URL=http://localhost:4318/v1/metrics
37export T3CODE_OTLP_LOGS_URL=http://localhost:4318/v1/logs
38export OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=development
39vp run dev
40```
41​
42## OTLP configuration and kill switches
43​
44- **Endpoints:** `T3CODE_OTLP_{TRACES,METRICS,LOGS}_URL` outrank standard `OTEL_EXPORTER_OTLP_{TRACES,METRICS,LOGS}_ENDPOINT` and `OTEL_EXPORTER_OTLP_ENDPOINT`. `T3CODE_OTLP_HEADERS` / `T3CODE_OTLP_PROTOCOL` (`http/json` default) apply only to `T3CODE_OTLP_*` endpoints; `OTEL_EXPORTER_OTLP_*` endpoints read `OTEL_EXPORTER_OTLP_*` headers/protocol (`http/protobuf` default). See [user telemetry](../user/telemetry.md#export-diagnostics-to-your-own-receiver) for Settings precedence.
45- **Service names are fixed:** `t3code-server` (backend), `t3code-desktop` (Electron main, traces/logs only), and `t3code-web` (UI). `OTEL_SERVICE_NAME` is ignored—distinguish installs with `OTEL_RESOURCE_ATTRIBUTES` (e.g. `deployment.environment.name=staging`).
46- **Kill switches:** `T3CODE_OTEL_SDK_DISABLED` (outranks `OTEL_SDK_DISABLED`, accepts standard boolean spellings; `OTEL_SDK_DISABLED` only accepts `true`) disables all OTLP export in server and desktop while keeping local traces and stdout intact. `OTEL_{TRACES,METRICS,LOGS}_EXPORTER=none` disables a single signal unless overridden by `T3CODE_OTLP_*_URL`.
47- **Trace tuning:** `T3CODE_TRACE_FILE`, `T3CODE_TRACE_MAX_BYTES`, `T3CODE_TRACE_MAX_FILES`, `T3CODE_TRACE_MIN_LEVEL`, `T3CODE_TRACE_TIMING_ENABLED`.
48- **Instrumentation rules:** Trace operation boundaries via `Effect.fn("Service.operation")` (see [Effect services](../internals/02_effect-services.md)). Put high-cardinality IDs/paths on span attributes, never on metric labels.
49​
50## Heap snapshots
51​
52On macOS and Linux (not Windows), send `SIGUSR2` to the server PID from `server-runtime.json` (in the server's state dir beside `logs/`) to write `<logsDir>/server-<pid>-<timestamp>.heapsnapshot` for Chrome DevTools Memory tab:
53​
54```sh
55pid="$(jq .pid "${T3CODE_HOME:-$HOME/.t3}/userdata/server-runtime.json")"
56ps -p "$pid" -o command= # verify it is the T3 server—never signal desktop or launcher
57kill -USR2 "$pid"
58```
59​
60- Writing pauses the server (up to a minute+ on large heaps) and needs roughly as much free RAM as the heap—send once, and avoid on a swapping host.
61- Snapshots contain tokens, secrets, and thread content. Never share them publicly, and delete them manually when finished.