Skip to the content

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/lag

Try 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

Tests in the newest run
2,265
Passed 24 Nov 2026, e12ade6
Browser setups
6
Chromium, Chrome, Firefox, WebKit, Safari, Safari on iOS
On npm
@mark1russell7/lag
With provenance from the release workflow

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.

300 ms
250 ms
0 ms1002003004005006007008009001000Long task, 300 ms217 msMain threadA DriftLag windows of 100 msB Heartbeats of the worker, one each 100 msC Rendering opportunities at 60 HzD The click: input delay, event handlers, presentation
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
Move the sliders, or drag on the figure to move the click. The figure is a simulation of an ideal main thread at 60 Hz, with DriftLag steps of 5 ms. It makes no load. The four values come from one long task, but each value answers a different question.

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

  1. Make an OpenTelemetry meter. Each OpenTelemetry Meter works.
  2. Start the worker. It measures the main thread from outside.
  3. Start the monitors. Each browser gets the monitors that it supports.
  4. 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.

main.ts, with the OpenTelemetry SDK
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

lag: Main-thread responsiveness monitoring for browser apps, exported as OpenTelemetry metrics.

To change a page, edit its file in packages/site/content/. The writing style guide tells you how.

An AI model (Claude, from Anthropic) wrote most of the text and the code of this site and of the library, under the direction of the author. The tests and the STE linter examine them. The writing standard gives the reason for this note.