~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0

Logs as JSON lines in traces, kept for HUD's operators

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/hud:logs/telemetry

HUD logs like knotless now: JSON lines on stderr in OpenTelemetry's names, each request in a W3C trace that its tool call and every outbound call share, /metrics and PUT /log on a loopback listener, and with HUD_LOGS=on every line kept in HUD_DATA_DIR/logs.db for the operators to read back with place.hud.getLogs or admin_logs. A deploy then no longer erases the record of what the iOS app's send_interactions calls did.

Claude Opus 5.5 · high

What changes

Lines. hud_core::telemetry writes every event as one JSON line on stderr (timestamp, level, message, the event's fields, target, trace_id), at RUST_LOG's level, info unset. hud starting names the version, and the commit where git archive stamped it (crates/hud/commit.txt, export-subst: the deploy builds from git archive, so no infra change is needed). Panics are logged with where they happened.

Traces. A request is in the trace its valid traceparent names, or a new one; the answer gives it back as x-trace-id. The formatter adds trace_id to every line logged while serving it, the existing ones included, and tasks a request starts keep it (telemetry::spawn: the blob download, a space tile's settling). Every outbound call sends its own traceparent.

One line per request, tool call and outbound call. request (route template, each place.hud.* method by its path, path, query with secret-bearing values masked and none under /oauth, method, user agent, status, size, duration_ms): warn past HUD_SLOW_MS (1000) except the event stream, error with its cause for a 5xx, debug for /_health. mcp tool / mcp tool failed (gen_ai.tool.name, caller's DID, is_error, the error, duration_ms; never arguments or results). outgoing from hud-core's net client (host, path, rpc.method, status, error.type, including abandoned for a call dropped unanswered), debug, warn on failure or slowness. They replace the ad hoc xrpc, xrpc refused, xrpc stream opened and mcp info lines (XRPC errors now ride on the request line's error).

A summary each minute of requests, calls by host, and each cache::Ttl's hits and misses (now counted with two atomics).

HUD_METRICS (a loopback address, refused otherwise): GET /metrics (RED by route and by host, cache lookups, build info) and PUT /log with a RUST_LOG directive (the reload layer).

HUD_LOGS=on: every line also goes to SQLite at HUD_DATA_DIR/logs.db, mode 0600, written every second off the request path, the oldest pending dropped past 50,000, never blocking. Lines at info and below kept to the last HUD_LOGS_KEEP (500,000), every line at most 30 days, pruned each minute.

Read back by HUD_OPERATOR_DIDS: place.hud.getLogs (new lexicon; by web session or Bearer; trace, message, level, minMs, limit ≤ 500; Forbidden for anyone else, NotFound when nothing is kept), and the MCP tool admin_logs, listed to operators alone and refusing anyone else. Each read is logged as logs read with who asked and for what.

Docs: docs/observability.md (new: what is logged, field names, traces, metrics, the kept lines, jq/journald recipes, what isn't built), env vars in docs/running.md and hud --help, getLogs in docs/workspace-api.md, admin_logs in docs/tools.md, AGENTS.md's storage rule, and the privacy policy's Logs paragraph.

Why

Tonight the grain box's json-file log went with each deploy, which hid whether send_interactions had been called. This follows knotless's observability (docs/observability.md there) adapted to HUD: env vars, not flags; HUD's own operators by DID; its one outbound client.

How it was checked

  • just check passes: rustfmt, clippy -D warnings, just cov at 100% lines and functions, svelte-check, just lint, just lex (the getLogs answers through xrpc/tests.rs's router are held to the lexicon).
  • target/debug/hud serve with HUD_LOGS=on, HUD_METRICS=127.0.0.1:<port>, HUD_OPERATOR_DIDS: PUT /log info,hud::outgoing=debug,... answered logging …; a tools/call of describe_repo on bsky.app with a traceparent put its outgoing lines (PLC, the PDS's getRecord/describeRepo/listRecords, the AppView's getAuthorFeed), its mcp tool line and its request line all in that trace, x-trace-id echoed it; /metrics answered on the loopback port with routes, hosts and cache lookups; getLogs refused signed out and with a bogus token (AuthRequired); logs.db, -wal and -shm were 0600 and held the trace's lines after SIGTERM. The operator path is covered by router and MCP tests (no local sign-in).

Shortcomings

  • Left out, as knotless does: the OpenTelemetry SDK and OTLP export, sampling, per-call spans.
  • Lines logged before the store opens (hud starting, config errors) reach stderr only.
  • The commit shows only in builds from git archive; a checkout's build names none.
  • The journald driver and retention on the box are a separate change in hetzner-infra; nothing here touches production. HUD_LOGS, HUD_OPERATOR_DIDS and HUD_METRICS need setting there to take effect.
  • The image has no HTTP client, so /metrics and /log are reached from a container in its network namespace (docs show curlimages/curl).

Review

Start at crates/hud-core/src/telemetry.rs (the formatter, traces, lines, counts), then logs.rs, then hud-server/src/telemetry.rs and the mcp.rs/xrpc.rs call sites.

Approved

This moves HUD to JSON-line logs in W3C traces. It adds a loopback /metrics and PUT /log, and an opt-in SQLite store (HUD_LOGS=on). Operators read the store through place.hud.getLogs and admin_logs. I read logs.rs, the server's telemetry.rs, the mcp.rs and xrpc.rs call sites, and the query-masking part of core telemetry.rs. I did not read the rest of core telemetry.rs, net.rs, main.rs, the CLI tests, the schema or the docs. The pull body says just check passes and the traced path was run by hand, so I leave those to the checks.

The parts I read look right.

  • Access: getLogs and admin_logs are gated on the operator DID, and each read is logged.
  • Tool list: admin_logs is listed to operators only.
  • Store: the file is created 0600 before SQLite opens it. Writes go in one transaction. A failed flush keeps its batch ahead of newer lines. Pruning keeps the last N lines at info and below, and drops any line older than 30 days.
  • Tool calls: mcp tool lines never carry arguments or results.

Small things you may want to fix. None blocks the merge:

  • Masked query names: telemetry::query matches names against SECRETS as written. A percent-encoded name such as %63ode bypasses the mask. Everything outside /oauth is logged and then kept 30 days, so decode the name before comparing.
  • Flush on the request path: read_logs calls logs.flush() synchronously inside an async handler. The write can block a Tokio worker for a large pending batch. It also races the background flusher, so rows can land slightly out of order. spawn_blocking, or letting the flusher do the write, would avoid both.
  • Trace on streams: traced wraps only next.run(req). Work done while a streamed body is polled, such as the event stream, may log without the trace id.
crates/hud-core/src/logs.rsline 390
..Fields::default()
};
telemetry::note(Level::INFO, "logs read", &f);
let read = logs.flush().and_then(|()| logs.read(&asked));
review.knotless.dev

flush() here is blocking SQLite work on an async worker, and it can run at the same time as flushing's own flush, so ids can interleave out of order. Consider spawn_blocking, or flush under one lock.

Merged as 4ca69c5