feat(relay2): typed close codes and Sentry exception capture

Merged
SA
3mo ago
+95-22

Summary

The observability items from the Orca comparison — the two that are just better, no behavioral hardening:

Typed close codes (RELAY_CLOSE, 4400–4410, in the shared tunnel-v2 protocol): every WS close now carries a fixed recovery meaning — 4401 auth-expired (mint a fresh token, reconnect), 4403 forbidden (config problem, retry slowly), 4404 unknown-ticket (abandon the stream), 4408 stale-host, 4409 replaced, 4410 tunnel-gone, 4400 bad-request — and the host client logs them by name via describeRelayClose. The Town-Hall incident's anonymous 1008 Forbidden retry loop would have read 4401 auth-expired: mint a fresh token and reconnect in the log.

Sentry (@sentry/cloudflare 10.68.0, matching the repo's Sentry train): withSentry on the Worker plus instrumentDurableObjectWithSentry on HostTunnel, so thrown exceptions from both surface with stacks. Console/breadcrumb capture is explicitly filtered out — the v1 relay's memory leak was Sentry's console firehose — and the integration is a no-op until the SENTRY_DSN secret is set (wrangler secret put SENTRY_DSN, needs a Sentry project). Adds the nodejs_als compat flag Sentry requires.

Workers Logs was already enabled in wrangler.jsonc — persistent, queryable logs exist in the CF dashboard today; nothing to add there.

Test plan

  • typecheck clean (relay2, shared, host-service); lint clean
  • Deployed and probed live: bad token → 4401 "Unauthorized", missing hostId → 4400, bogus dial ticket → 4404 (this also exercises the Sentry-instrumented DO end-to-end)
  • Post-deploy health: both team hosts back online with fresh lastSeenAt via /presence
  • After a Sentry project + DSN secret exist: throw path visible in Sentry

Summary by cubic

Adds typed WebSocket close codes to the tunnel-v2 protocol and instruments relay2 with Sentry for exception capture, improving recovery guidance and error visibility. Previously the relay closed with generic 1008/1011; now it uses named RELAY_CLOSE codes (4400–4410) and the host logs them by name.

Review notes

  • Protocol: adds RELAY_CLOSE constants and describeRelayClose in packages/shared; callers no longer infer behavior from reason strings.
  • Relay: replaces all closes with typed codes (badRequest, authExpired, forbidden, unknownTicket, staleHost, replaced, tunnelGone) and updates the handshake helper to send a code + reason.
  • Host client: packages/host-service logs close codes with describeRelayClose; no reconnect logic changes.
  • Sentry: adds @sentry/cloudflare and wraps the Worker and HostTunnel with Sentry; disables console/breadcrumb capture; no-op until SENTRY_DSN is set; adds nodejs_als compat flag and SENTRY_DSN to RelayEnv.

Rollout

  • Set the SENTRY_DSN secret in Cloudflare to enable capture: wrangler secret put SENTRY_DSN.
  • Deploy the wrangler.jsonc change (includes compatibility_flags: ["nodejs_als"]).
  • No client action required; hosts will log the new codes automatically.