Skip to content

requestlog: one structured line per HTTP request - #9

Merged
reoring merged 3 commits into
mainfrom
work/20261119/requestlog
Nov 18, 2026
Merged

reoring merged 3 commits into
mainfrom
work/20261119/requestlog

Conversation

@reoring

@reoring reoring commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Why

Built-in apps emit no access log. After container log collection was enabled on Craftsman (2026-11-18), opening the production Builder Console produced zero lines; typed 4xx/5xx, proxy 502s and provider failures leave no trace. There is no setting to turn logging on.

What

  • appserver/requestlog (new, stdlib only): Wrap(next, Options{Service}) writes one JSON line per request to stderr: ts, level, msg, service, method, path, route, status, bytes, duration_ms, request_id, plus error_code/error/outcome/reason/panic when present.
  • Never logged: query string, headers, bodies. error text only for 5xx/panic, cut at 512 bytes.
  • Successful GET /healthz and GET /readyz are skipped; failed probes are logged.
  • A handler panic is observed, not recovered: net/http keeps the original value and stack.
  • The writer forwards Flush, http.ResponseController, and Hijack when the underlying writer supports it.
  • httpx.WriteError keeps its signature. On a wrapped request it records the error on the line and no longer prints a second line; without the wrapper its behaviour is unchanged.
  • SetError, SetOutcome, ID let handlers annotate the current line; no-ops when unwrapped.

Additive; intended as v0.7.0. No platform minimum change.

Verification

  • go vet and go test ./... -count=1: pass. go test -race on requestlog and httpx: pass.
  • Throwaway server through httptest: 200 with ?token=… logs the path without the query; untyped error logs internal_error + text; typed 403 logs error_code only; /healthz logs nothing; 404 logs without route; panic logs panic:true and net/http prints the stack at the original handler line.

Adoption

Apps bump to v0.7.0 and wrap the handler once in cmd/server/main.go.

Built-in apps wrote no access log and only untyped handler errors, so a
collected container log could not show traffic or failures. Add
appserver/requestlog: Wrap emits one JSON line per request (method, path,
route, status, bytes, duration, request id) and never the query, headers
or body. Successful probes are skipped; a handler panic is observed, not
recovered. httpx.WriteError records its error on the wrapped request
instead of printing a second line; without the wrapper it is unchanged.
An app with its own X-Request-Id contract (AI Gateway's invocation ID)
replaces the header; log that value so header and line still match.
Some apps forward an error code taken from an upstream body; emit
error_code only when it is a safe token, otherwise "other".
http.MaxBytesReader signals the server through an unexported
ResponseWriter method that a wrapper cannot forward, so an oversized
body no longer closed the connection. Set the header when a handler
answers 413.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant