JMR-devandClaude Opus 4.8 45ac7236ee #17 Observability: OpenTelemetry logging + tracing + alerting
Instrument the Worker with OpenTelemetry traces + structured logs over OTLP,
plus alertable signals, via a minimal hand-rolled OTLP/HTTP exporter that fits
the TinyGo/Wasm Worker build.

New internal/telemetry package (build-tag-free, host-tested):
- Span/log shim: Telemetry provider, Span (attrs/status/events/end), Log
  (Info/Warn/Error), trace/span-id correlation, W3C-style ids from crypto/rand.
- Exporter seam: MemoryExporter (in-memory, for tests) and OTLPHTTPExporter
  (OTLP/HTTP JSON over net/http). No go.opentelemetry.io/otel/sdk dependency:
  the full OTEL-Go SDK + OTLP exporters pull in a large, reflection-heavy tree
  (protobuf, grpc) that bloats the Wasm binary and is unreliable under TinyGo.
  The shim uses only stdlib already proven under this project's js/wasm target
  (net/http per #26, encoding/json, crypto/rand). OTLP is the wire format, so
  any OTLP backend can ingest it.
- Behaviour-preserving by construction: instrumentation is threaded through
  context. Instrumented code pulls an optional *Telemetry from ctx; absent (or
  nil exporter) => every method is a no-op. No public signatures change
  (NewHandler, handler.New, publish.New/Publish, schedule.Run are untouched), so
  parallel work built on the current APIs keeps compiling.

Instrumentation:
- ingest: an "ingest.request" server span + correlated log per request,
  classifying accepted / rejected / rate_limited / error. Observe-only (wraps the
  response writer to read the status); the HTTP contract is unchanged. A 5xx
  (e.g. 503 storage-unavailable) sets the span to Error and emits the alertable
  ingest.error signal; 4xx client rejections are INFO, not alerts.
- publish: a "publish.run" span with per-report "publish.report" child spans and
  a log per report (published/failed). A failed report/run sets Error and emits
  alert.type=publish.run_failed. The per-run cap-hit (folding in the #14
  follow-up) is now emitted as a structured, alertable OTEL signal
  (alert.type=publish.cap_hit + counts), not merely a log line.
- schedule: a "schedule.run" span parenting the publish run; a list/publish
  failure emits alert.type=schedule.run_failed.

Config (OTLP endpoint TBD, issue #17):
- OTEL_EXPORTER_OTLP_ENDPOINT (plain var) - base OTLP/HTTP URL; empty => telemetry
  disabled (Worker behaves as before). /v1/traces and /v1/logs are appended.
- OTEL_EXPORTER_OTLP_HEADERS (Secrets Store secret) - auth header(s), never
  committed. OTEL_SERVICE_NAME (plain var) - service.name override.
- worker/telemetry_wasm.go builds the exporter lazily per run and injects the
  provider into the request/scheduled context; wrangler.jsonc gains only these
  OTEL keys.

Alerting: run-failure, cap-hit, and elevated-ingest-error are emitted as span
status=Error and structured log records carrying alert=true + a specific
alert.type, so a backend alert rule can key on them once the OTLP endpoint is
chosen.

Tests: host unit tests with the in-memory exporter assert the ingest spans+logs
for accepted/rejected/error, the publish run span + per-report spans + the
cap-hit and run-failed signals, the schedule run span + list-error alert, and
the OTLP/JSON encoding + HTTP round trip (httptest, no real backend). No-op
default verified. go vet ./... and go test ./... green; GOOS=js GOARCH=wasm
go build ./... compiles.

Closes #17

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 17:14:21 -05:00

LibreMail Bug Report Ingest

Server-side infrastructure for LibreMail's debug bug-report pipeline. This repo is intentionally separate from the Android app repo — it owns the Cloudflare Worker and infrastructure-as-code, not the client.

What this is

Per JMR-dev/LibreMail#11:

  1. The LibreMail app lets a user opt in to submitting a debug report (LibreMail#33).
  2. A Cloudflare Worker in this repo receives the report over HTTPS, best-effort scrubs PII, and stores it encrypted in a Cloudflare R2 bucket (#34).
  3. Every Friday at 17:00 (Central Time, DST-aware), a scheduled job publishes any report not manually removed as a GitHub issue on the LibreMail repo (#35).

Stack

  • Worker: Go, compiled to WebAssembly with TinyGo and served through the syumai/workers runtime adapter
  • Infrastructure as code: Pulumi (Go)
  • Deployment: GitHub Actions
  • Secrets/key custody: Cloudflare Secret Manager
  • DNS: Google Cloud DNS

Build & run locally

The request-handling logic lives in internal/handler as plain, build-tag-free Go, so it is unit-tested and run locally with the standard Go toolchain — no TinyGo needed. Only the actual Wasm Worker build requires TinyGo.

Layout:

  • internal/handler/ — the core http.Handler (health/hello endpoints). No build tags; all request logic and its tests live here.
  • cmd/devserver/ — a plain net/http server that mounts the core handler for local dev without TinyGo.
  • worker/ — the Cloudflare Workers (Wasm) entrypoint, guarded by //go:build js && wasm, wiring the same core handler into the Workers runtime. Excluded from host builds and tests.

Test

go vet ./...
go test ./...

Run locally (no TinyGo)

go run ./cmd/devserver   # listens on :8787; override with ADDR, e.g. ADDR=:9000

Then, from another shell:

$ curl -s localhost:8787/
{"service":"libremail-bug-report-ingest","status":"ok","message":"hello from the LibreMail bug-report ingest Worker"}
$ curl -s localhost:8787/healthz
{"status":"ok"}

This runs the exact handler the deployed Worker uses, minus the Workers runtime.

Build & run the real Worker (requires TinyGo)

Node tooling is managed with pnpm; wrangler is a dev dependency. The Wasm build uses TinyGo 0.41.1 on the Go 1.26 toolchain.

Temporary toolchain patch. TinyGo 0.41.1 and earlier vendor a net/http js/wasm overlay (tinygo-org/net@e54965e) that fails to compile against Go 1.25+/1.26 with t.roundTrip undefined (see tinygo-org/tinygo#5467). CI applies the exact upstream fix (tinygo-org/net@1026408a, checked in as .ci/tinygo-net-roundtrip.patch) to the installed TinyGo before building. Building locally on Go 1.26 needs the same one-file patch until a TinyGo release later than 0.41.1 ships it, at which point the patch and the CI step are removed (tracked in #26).

pnpm install                 # install wrangler
pnpm run build               # workers-assets-gen + TinyGo -> ./build/app.wasm + ./build/worker.mjs
pnpm exec wrangler dev       # serve the Wasm Worker locally on :8787
pnpm exec wrangler deploy    # deploy (CI only)

pnpm run build runs, verbatim:

go run github.com/syumai/workers/cmd/workers-assets-gen && tinygo build -o ./build/app.wasm -target wasm -no-debug ./worker

TinyGo is not required for tests or the dev server; it is needed only for the Wasm build above and is installed in CI. The generated ./build/ output is git-ignored.

Why TinyGo + syumai/workers

Cloudflare Workers execute WebAssembly, not native binaries, so Go must be compiled to Wasm. Of the two options — the standard compiler's GOOS=js GOARCH=wasm output or TinyGo — TinyGo emits far smaller modules that sit comfortably inside the Worker size limit, which is why it is the standard path for Go on Workers. The syumai/workers package adapts Go's net/http handler model to the Workers fetch event, so a single http.Handler runs unchanged on the dev server and in the deployed Worker.

Status

Early bootstrap. See the project board and open issues for the current breakdown of work.

License

GNU AGPL v3.0.

S
Description
Server-side ingest pipeline (Cloudflare Worker + IaC) for LibreMail's opt-in bug-report feature. See JMR-dev/LibreMail#11.
Readme AGPL-3.0
467 KiB
Languages
Go 100%