The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S32 · V2·32

See What Users See

Volume 2, chapter 32. Every measurement so far was taken in the lab: one browser, one machine, a test that knew what to look for. This stage adds real-user monitoring: a small RUM client on every page sends Web Vitals, the build's own tb:* measures, long tasks, the outbox's length and every error a page catches or misses, tagged with the release that served it; the server keeps percentiles per release and page from an endpoint that anyone can post to, so it trusts nothing. Releases now build source maps that are never served, and a frame reported from a minified, hashed chunk symbolicates back to the line that threw. Measured on the way: a kitchen display is never hidden, so "send on hide" alone sent nothing until it navigated away; sendBeacon refuses a payload over its 64 KB budget; and the first INP was the worst tap of the day rather than the 98th percentile.

6 parts · ~5 min to read
Updated Suggest a correction

Stage 32: see what users see

The brief

What you'll build.

  • web-src/telemetry.ts: LCP, CLS and INP (estimated as web-vitals does, from the ten longest interactions), every tb:* measure, long tasks, errors (uncaught, unhandled rejections, and ones the code reports), and counts; sent with sendBeacon as a small line protocol when the page goes away and every 60 s; started by every kitchen view (through pageSender) and the menu search;
  • the release in every page: <meta name="tb-release">, which the server fills from the release it's serving (dev otherwise);
  • Rum.java: POST /rum (16 KB, strictly parsed, bounded series, counted drops, nothing echoed raw) and GET /rum/summary (nearest-rank p50, p75 and p95 per release, page and metric; errors grouped by release, place, message and first frame); 4 tests;
  • source maps: release.mjs builds them, keeps them in DIST/sourcemaps/<release>/, outside the served directory; web-tests/symbolicate.mjs decodes VLQ mappings without dependencies;
  • tests: telemetry.test.mjs (3) and suite/test_rum.py (a release, a broken stream message, the summary, and the symbolicated frame).

Why a real company needs this. The lab says the kitchen display renders 500 orders in under a second on a throttled CPU. It can't say how it behaves on a three-year-old tablet on a kitchen's Wi-Fi, or how often a contract error happens in the field. Without field data, regressions are found by customers, and "it's slow" can't be answered with a number.

Why now. Chapter 32 is frontend observability: error tracking, source maps, real-user monitoring and Web Vitals in the field, logs, traces and correlation IDs, sessions and release health. Stage 27 promised field data to complement its lab budgets; stage 29's outbox needed its queue length reported; stage 31 gave every asset a release.

What you'll have at the end.

  • percentiles per release, from real pages;
  • errors grouped and traceable to source lines;
  • ADR-0034: what's collected, what isn't, and why.

Theory to own

  1. Lab and field data. What can each tell you that the other can't? (The chapter, "Real-user monitoring"; Companion, Explain it differently 1.)
  2. Percentiles. Why p75 for Web Vitals and p95 for latency, and why not averages? (The chapter's lab, Experiment 2.)
  3. When to send. What does a page get to do when the user leaves it, and what if they never do? (Companion, Gaps 1.)
  4. Source maps. What's in one, and why shouldn't production serve it? (The chapter, "Source maps"; Companion, Code walkthrough.)
  5. Untrusted telemetry. What can go wrong with an endpoint every visitor can post to? (Companion, Gaps 3.)

Build it

Step 1: the client

Write telemetry.ts. Observe largest-contentful-paint, layout-shift (without recent input), event (with durationThreshold: 16, grouped by interactionId), longtask and measure (names starting tb:), all with buffered: true. Listen for error and unhandledrejection. Send on visibilitychange to hidden, on pagehide (once: both fire), and every 60 s. Each line is fields joined by tabs; replace tabs and newlines inside each field.

Checkpoint. A kitchen page sends vitals and measures when it's closed, and something every 60 s while it's open.

Step 2: the endpoint

Write Rum.java. Accept only known line shapes: release and page (validated with tight patterns; the page path without its query, with the kitchen id collapsed to kN), vital|measure|count with a bounded name and a number, error with four fields. Keep at most 500 series and 5,000 values each; count everything dropped. Return 204. Summarise with nearest-rank percentiles.

Checkpoint. RumTest: percentiles per release and page; bad lines dropped and counted, never stored; oversize and empty bodies refused.

Step 3: source maps that aren't served

In release.mjs, build with sourcemap: 'external' (no sourceMappingURL comment), move the maps to DIST/sourcemaps/<release>/, and rewrite each map's sources relative to the repository. Write symbolicate.mjs: decode the mappings field (base64 VLQ; every field is a delta, and source fields carry over from line to line) and find the last segment at or before the reported column.

Checkpoint. Hashed names are unchanged by turning maps on; a reported frame symbolicates; /assets/…js.map is a 404.

Step 4: from the browser to a source line

Write test_rum.py: release, serve, open a kitchen page, then give the reloaded page a stream whose snapshot is missing fields; navigate away and read the summary; symbolicate the error's frame.

Checkpoint. The summary has vitals, measures and the error for this release; the frame maps to web/contract.js.

Senior and architect lens

1. Telemetry is a product with users and costs. Its users are engineers and on-call; its costs are bytes on users' connections, storage, and privacy risk. Collect what answers a question you have.

2. ADR-0034: real-user monitoring. Write it with these points:

  • Collected: Web Vitals (p75), tb:* measures, long-task counts, outbox length, errors with message and one frame, release, page type.
  • Not collected: query strings, order contents, customer or staff identifiers, IP addresses (not logged by /rum), full stacks, session replays.
  • Transport: sendBeacon, text/plain (no preflight), on hide, on page hide and every 60 s; 400 lines per batch.
  • Endpoint: strict parsing, 16 KB, 500 series, 5,000 values per series, drops counted; summaries in text, never HTML.
  • Source maps: built per release, never served, kept with the release and pruned with it.
  • Next: sampling when traffic grows; storage beyond memory (Volume 9); tracing from browser to database with traceparent (Volume 3).

3. Group errors by what distinguishes them. Message, place and release first, frame second; a helper that throws for many reasons (fail) makes the first frame useless as a key.

4. Release health is per release. Every series carries the release, so a regression after a deploy is visible as a difference between two rows, and stage 31's rollback is the response.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
traffic every page sends; memory grows to the bounds, then drops Volume 9 (sampling, a time-series store)
a restart summaries are in memory and lost Volume 8
the lab budget for long tasks is zero noisy on shared machines (stage 29), while the field shows p50 0 stage 33 (the release check reads field percentiles)
errors across systems a browser error can't be joined to the server request that caused it Volume 3 (request ids, traceparent)
frames point at compiled JS one step from TypeScript stage 33 (tsc maps folded into the release's maps)

Evidence

  • test_rum.py passes and prints a summary and a symbolicated frame; the suite passes (44 passed), and the perf budgets pass.
  • 40 server tests (4 in RumTest) and 41 JS tests pass.
  • No .map file in any release's assets/; bundles.mjs finds no sourceMappingURL.
  • ADR-0034 is committed.

Carry forward

  • /rum/summary is what stage 33's release check reads before promoting a release.
  • The release meta tag is how errors, timings and (in Volume 3) requests are joined to a release.
  • symbolicate.mjs gains TypeScript positions in stage 33.
  • reportError(e, where) is the one way code reports a caught error.