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

The admin reads the log and stats over HTTP, the CLI and MCP

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:ops/admin-reads

The admin reads what the server logged and counted without reaching its machine: pds admin logs and pds admin stats, or the MCP tools admin_logs and admin_stats, over GET /xrpc/_logs and /xrpc/_stats with the admin's Basic auth. With PDS_LOG_DIR set, the log is kept there too, within PDS_LOG_KEEP_BYTES, across restarts and deploys.

Claude Opus 5.5

What changes

spec/OPERATIONS.md. Two variables: PDS_LOG_DIR, a directory the log is kept in too, unset by default, and PDS_LOG_KEEP_BYTES, the most its files take, 64 MiB unless set, at least 65536. "Kept": each line goes to standard output, then the same bytes to a file in PDS_LOG_DIR by one write of its own, before the next line; the files never take more than PDS_LOG_KEEP_BYTES, and the newest lines of half of it are always kept; a start carries on after what is there. "The admin's reads", new: _logs answers the last limit kept lines matching trace, msg, method, level and above, and minMs, oldest first, 404 LogNotKept with no PDS_LOG_DIR; _stats answers what /metrics counts since the start, as JSON (routes by count with their statuses and mean times, outbound requests, syncs, accounts, firehose), whether or not PDS_METRICS_PORT is set. Both check the admin's auth first, cost a point of global, and have routes _logs and _stats.

spec/COMMANDS.md. "The admin's reads": the one exception to tools and commands being made from lexicons. admin_logs then admin_stats after every lexicon tool, with their schemas and descriptions; pds admin logs then pds admin stats in the group admin.

spec/LIMITS.md, spec/PDS.md, spec/COMPAT.md. The admin's auth and global name the two reads; PDS.md lists them among what the server serves; PDS_LOG_DIR is a setting another implementation may not be given.

harness. lib/admin.mjs, new, and the admin step of checks/operations.mjs: the 401s, the 400s, _logs held line for line to what standard output holds, by each filter and together, across a kill and a start, the kept files within PDS_LOG_KEEP_BYTES, _stats between two scrapes of /metrics, a start with no PDS_LOG_DIR, and three bad starts. checks/commands.mjs: the two tools in tools/list in each era, pds admin --help, and each tool and command with the admin's auth, a token and none answering as the routes do. checks/logging.mjs: no secret in the kept files. The server is started with PDS_LOG_DIR apart from its data directory and PDS_LOG_KEEP_BYTES=262144; the backup check's copy keeps none.

crates/pds-server. ops/kept.rs, new: two files, pds.log and pds.log.1, each at most half the keep, rotated by rename; a read takes the files' handles and lengths under the log's lock and scans backwards off it. ops/admin.rs, new: the two reads, shared by XRPC and MCP. ops.rs writes each line to the kept file after standard output; metrics.rs answers _stats from the counters /metrics renders; mcp.rs lists and calls the two tools.

crates/pds-cli. pds admin logs and pds admin stats, with the lexicon commands' flag parsing and help.

docs/guide. Operating: keeping the log and reading it and the stats. Deploying: the two variables. Calling and Comparing: the commands and the checks.

Why

The log is on standard output alone, so reading it means reaching the machine, and a deploy that makes the container again loses it. knotless's appview has the same reads (knotless admin logs, knotless admin stats), kept in a SQLite file; here the server keeps no database, so the log is a file of capped size, and the stats are the metrics it already counts, since its start. They have no lexicon, as _health has none: they are this server's own, for its admin.

The kept log is apart from PDS_DATA_DIR: in it, its 64 MiB would break STORE.md's settled bound on the data directory's size, and no backup needs it.

How it was checked

  • Against the server as it was (0a88af0), compare.mjs --only admin,lifecycle,log fails at the first read, _logs with no auth answering 400 Not a method: _logs for 401; with --commands, tools/list lacks the two tools.
  • Planted, _logs answering its lines newest first: _logs differs ... at the end of the run.
  • On this branch: compare.mjs --steps 10 --only admin,lifecycle,log and --commands --only admin pass, each tool in the three eras and each command.
  • cargo test -p pds-server -p pds-cli, with new unit tests of the kept files' bound, rotation, a torn tail and the reads' filters; clippy and fmt clean. The docs site builds.
  • just quick was not run here: other harness runs were on this machine. The gates are the gates workflow's, on the pull.

Shortcomings

  • _stats is since the start: a restart begins it again, as the metrics do. No counts by day are kept.
  • A line is matched by scanning the files backwards: a filter that matches nothing reads all of PDS_LOG_KEEP_BYTES, 64 MiB by default.
  • Where the spec was silent the server chose: the point of global is spent once the parameters parse, so a 404 LogNotKept costs one; HEAD is a method other than GET, 400; a repeated parameter counts its first value; an MCP answer's line has its fields in sorted order, where XRPC keeps them as written.
  • The staged server (spec-pds.chadtmiller.com) needs PDS_LOG_DIR on a volume of its own to keep its log.

Review

crates/pds-server/src/ops/kept.rs: that the files never pass the keep, under rotation and a failed rename, and that a read racing a rotation reads what it should. Then harness/lib/admin.mjs's holdLines, which says which lines a read may answer.

Approved

This adds GET /xrpc/_logs and /xrpc/_stats behind the admin's Basic auth, with the CLI commands pds admin logs and pds admin stats and the MCP tools admin_logs and admin_stats. With PDS_LOG_DIR set, the server also keeps its log in two rotated files bounded by PDS_LOG_KEEP_BYTES. I found nothing that must change.

Your review ask:

  • kept.rs: I read the bound and rotation as you asked. Each file stays at or under half the keep, so the two together never pass the whole. The newest lines of half the keep are always in the two files, because current + previous always exceeds half after a rotation. A failed rename puts the file back, and a failed reopen leaves current as None, which the next write retries. A read racing a rotation works from cloned handles and the lengths taken under the lock, and reads with read_exact_at. So it sees what was kept when it began, and rotation unlinking the older file doesn't affect it.
  • _logs and _stats: auth comes first, then the method, then the parameters, then the global point, as OPERATIONS.md says. The CLI, MCP and XRPC paths share ops::read. The tool descriptions match COMMANDS.md.
  • Spec and docs: OPERATIONS.md, COMMANDS.md, LIMITS.md, PDS.md, COMPAT.md and the guides all move with the code.
  • Harness: the new checks hold the server tighter. Nothing is loosened, and no conformance excuse is added. No atproto or MCP crates are added.
  • Gates: the docs and citations workflows passed. gates was still running when I read this.

Two small things, neither blocking:

  • crates/pds/src/main.rs has help text listing PDS_LOG_LEVEL, PDS_METRICS_PORT and the OTEL_* variables. It doesn't name PDS_LOG_DIR or PDS_LOG_KEEP_BYTES.
  • The admin_stats description and the guide say "its syncs", but the answer's field is fsync. It might read better as "its fsyncs".

I did not read harness/checks/operations.mjs, harness/checks/commands.mjs or harness/lib/via.mjs. They are large and I spent the review on the server code, the spec and harness/lib/admin.mjs's holdLines.

Merged as adb4603