Real user monitoring of the main thread, for OpenTelemetry
Measure how long the main thread makes users wait
lag is a set of monitors for browser apps, for real user monitoring of the main thread. Each monitor measures one signal of main-thread responsiveness, for example blocked time, long tasks or input latency (INP), and records it as an OpenTelemetry metric. The monitors calibrate their probes and discard the samples that are not valid. A Web Worker also measures the main thread from outside.
Quick startOpen the playground
npm install @mark1russell7/lagTry it in this tab: press a button in the panel. The first dot stops, because the main thread is busy. The second dot continues, because the compositor moves it.
The evidence
- Browser setups
- 6
- Chromium, Chrome, Firefox, WebKit, Safari, Safari on iOS
One block, four meanings of lag
A long task blocks the main thread. Four monitors measure it, and each monitor gives a different value, because each one answers a different question. Move the click and change the task: the figure calculates what each monitor sees. The event loop page tells you more.
- Blocked time in a window
- 307 ms
- The lag of one window: each window takes longer than 100 ms by the time that tasks blocked it. DriftLag
- Queueing delay
- 210 ms
- The longest wait of a heartbeat. 3 of 10 heartbeats waited. A message at a random time waits 47 ms on average. Worker lag
- Frame delay
- 333 ms
- The longest time between two frames. It drops round(333 ms / 16.7 ms) − 1 = 19 frames. Frame timing
- Input latency
- 217 ms
- Input delay 200 ms, event handlers 12 ms, presentation 4.7 ms. INP: needs improvement. Event timing
What the monitors measure
- Main-thread lag
- DriftLag measures how much of each 100 ms window other tasks block the main thread, with a timer baseline that it calibrates. DriftLag
- Long tasks and Long Animation Frames
- The monitor records each frame of 50 ms or more, its blocking duration and, in Chromium, the scripts that caused it. Long Animation Frames
- INP and the Web Vitals
- The page-view vitals give INP, CLS, LCP, FCP and TTFB for each page view, with the rules of web-vitals. Page-view vitals
- Hangs, seen from a Web Worker
- A worker measures main-thread blocks while they occur, and reports the hangs that a page did not survive. Worker lag
- Event loop lag
- Blocked time, queueing delay, frame delay and input latency are four different meanings of lag. Each has its monitor. The event loop
- OpenTelemetry browser metrics
- The monitors record counters and histograms that aggregate correctly over many browsers, for example in Grafana Mimir. Metrics model
Add the monitors to an app
- Make an OpenTelemetry meter. Each OpenTelemetry Meter works.
- Start the worker. It measures the main thread from outside.
- Start the monitors. Each browser gets the monitors that it supports.
- When the page becomes hidden, record the final values of the page view, then export them.
The quick start adds the events, the spans and the hang reports of the worker. The API reference lists each export.
import { MeterProvider, PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { createBrowserDeps, setupAllMonitors } from "@mark1russell7/lag";
import { createLagWorker } from "@mark1russell7/lag/worker";
const provider = new MeterProvider({
resource : resourceFromAttributes({ "service.name" : "shop", "service.instance.id" : crypto.randomUUID() }),
readers : [new PeriodicExportingMetricReader({ exporter : new OTLPMetricExporter({ url : "https://collector.example.com/v1/metrics" }) })],
});
const worker = createLagWorker();
const monitors = setupAllMonitors(createBrowserDeps(window, { meter : provider.getMeter("lag"), logger : console, worker }));
// When the page becomes hidden: record the final values of the page view, then send them
document.addEventListener("visibilitychange", (event) => {
if (document.visibilityState !== "hidden") return;
monitors.flush(event);
void provider.forceFlush();
});What this site holds
Docs
How to install and use the monitors, what each monitor measures, and how the parts fit together.
42 pages. Start with:
Thesis
The argument of the project: the problem, the claims, the method and the evidence.
Research
Notes on browser support, clocks and timers, OpenTelemetry in the browser and related tools.
9 pages. Start with:
Results
The results of the test program: tests in each browser, coverage, mutation scores, measurements and budgets.