@libo11y/rum 0.0.0-stage → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +478 -2
- package/dist/actions/collector.d.ts +29 -0
- package/dist/actions/collector.js +260 -0
- package/dist/actions/naming.d.ts +15 -0
- package/dist/actions/naming.js +65 -0
- package/dist/actions/record.d.ts +13 -0
- package/dist/actions/record.js +28 -0
- package/dist/actions/settle.d.ts +28 -0
- package/dist/actions/settle.js +53 -0
- package/dist/attrs.d.ts +10 -0
- package/dist/attrs.js +12 -0
- package/dist/bind.d.ts +23 -0
- package/dist/bind.js +55 -0
- package/dist/bootstrap/buffer.d.ts +72 -0
- package/dist/bootstrap/buffer.js +95 -0
- package/dist/bootstrap/replay.d.ts +10 -0
- package/dist/bootstrap/replay.js +11 -0
- package/dist/bootstrap.d.ts +17 -0
- package/dist/bootstrap.js +33 -0
- package/dist/bus.d.ts +3 -0
- package/dist/bus.js +21 -0
- package/dist/config/clamp.d.ts +34 -0
- package/dist/config/clamp.js +79 -0
- package/dist/config/remote.d.ts +103 -0
- package/dist/config/remote.js +241 -0
- package/dist/context-manager.d.ts +8 -0
- package/dist/context-manager.js +29 -0
- package/dist/contracts/binding.d.ts +82 -0
- package/dist/contracts/binding.js +1 -0
- package/dist/contracts/retention.d.ts +32 -0
- package/dist/contracts/retention.js +1 -0
- package/dist/contracts/runtime.d.ts +164 -0
- package/dist/contracts/runtime.js +1 -0
- package/dist/contracts/session.d.ts +68 -0
- package/dist/contracts/session.js +1 -0
- package/dist/contracts/summary.d.ts +28 -0
- package/dist/contracts/summary.js +1 -0
- package/dist/contracts/views.d.ts +71 -0
- package/dist/contracts/views.js +1 -0
- package/dist/core-setup.d.ts +121 -0
- package/dist/core-setup.js +1117 -0
- package/dist/core.d.ts +46 -0
- package/dist/core.js +36 -0
- package/dist/errors/breadcrumbs.d.ts +16 -0
- package/dist/errors/breadcrumbs.js +27 -0
- package/dist/errors/collector.d.ts +17 -0
- package/dist/errors/collector.js +186 -0
- package/dist/errors/normalize.d.ts +7 -0
- package/dist/errors/normalize.js +31 -0
- package/dist/errors/record.d.ts +20 -0
- package/dist/errors/record.js +46 -0
- package/dist/frustration/detect.d.ts +38 -0
- package/dist/frustration/detect.js +62 -0
- package/dist/ids.d.ts +2 -0
- package/dist/ids.js +2 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +12 -0
- package/dist/internal.d.ts +34 -0
- package/dist/internal.js +25 -0
- package/dist/loaf.d.ts +15 -0
- package/dist/loaf.js +27 -0
- package/dist/options.d.ts +84 -0
- package/dist/options.js +1 -0
- package/dist/pending-spans.d.ts +29 -0
- package/dist/pending-spans.js +92 -0
- package/dist/query.d.ts +2 -0
- package/dist/query.js +19 -0
- package/dist/resources/collector.d.ts +76 -0
- package/dist/resources/collector.js +270 -0
- package/dist/resources/server-timing.d.ts +18 -0
- package/dist/resources/server-timing.js +21 -0
- package/dist/retention/buffer.d.ts +29 -0
- package/dist/retention/buffer.js +130 -0
- package/dist/retention/exporters.d.ts +71 -0
- package/dist/retention/exporters.js +281 -0
- package/dist/retention/sessions.d.ts +4 -0
- package/dist/retention/sessions.js +30 -0
- package/dist/session/local-store.d.ts +25 -0
- package/dist/session/local-store.js +91 -0
- package/dist/session/manager.d.ts +36 -0
- package/dist/session/manager.js +169 -0
- package/dist/session/sampling.d.ts +4 -0
- package/dist/session/sampling.js +20 -0
- package/dist/session/state.d.ts +4 -0
- package/dist/session/state.js +28 -0
- package/dist/settings.d.ts +8 -0
- package/dist/settings.js +36 -0
- package/dist/setup.d.ts +42 -0
- package/dist/setup.js +57 -0
- package/dist/span-processor.d.ts +38 -0
- package/dist/span-processor.js +125 -0
- package/dist/summary/accumulator.d.ts +30 -0
- package/dist/summary/accumulator.js +128 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/dist/views/collector.d.ts +40 -0
- package/dist/views/collector.js +286 -0
- package/dist/views/history.d.ts +18 -0
- package/dist/views/history.js +42 -0
- package/dist/views/naming.d.ts +6 -0
- package/dist/views/naming.js +12 -0
- package/dist/views/paint.d.ts +12 -0
- package/dist/views/paint.js +15 -0
- package/dist/views/tracker.d.ts +37 -0
- package/dist/views/tracker.js +114 -0
- package/dist/vitals/attribution.d.ts +14 -0
- package/dist/vitals/attribution.js +60 -0
- package/dist/vitals/collector.d.ts +24 -0
- package/dist/vitals/collector.js +232 -0
- package/dist/vitals/lcp.d.ts +32 -0
- package/dist/vitals/lcp.js +49 -0
- package/dist/webext/chrome.d.ts +45 -0
- package/dist/webext/chrome.js +13 -0
- package/dist/webext/config-storage.d.ts +18 -0
- package/dist/webext/config-storage.js +98 -0
- package/dist/webext/scope.d.ts +22 -0
- package/dist/webext/scope.js +50 -0
- package/dist/webext/session-access.d.ts +13 -0
- package/dist/webext/session-access.js +9 -0
- package/dist/webext/session-store.d.ts +5 -0
- package/dist/webext/session-store.js +104 -0
- package/dist/webext/setup.d.ts +74 -0
- package/dist/webext/setup.js +206 -0
- package/dist/webext.d.ts +10 -0
- package/dist/webext.js +8 -0
- package/dist/wire.d.ts +13 -0
- package/dist/wire.js +30 -0
- package/package.json +60 -4
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { randomUuid } from "../ids.js";
|
|
2
|
+
const LOAD_KINDS = new Set(["initial", "prerender", "bfcache_restore"]);
|
|
3
|
+
export function createViewTracker(opts) {
|
|
4
|
+
const randomId = opts.randomId ?? randomUuid;
|
|
5
|
+
const listeners = new Set();
|
|
6
|
+
let current;
|
|
7
|
+
let load;
|
|
8
|
+
let appLoad;
|
|
9
|
+
const emit = (e) => {
|
|
10
|
+
for (const l of listeners)
|
|
11
|
+
l(e);
|
|
12
|
+
};
|
|
13
|
+
const endCurrent = () => {
|
|
14
|
+
const ended = current;
|
|
15
|
+
if (ended === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
current = undefined;
|
|
18
|
+
opts.summary.close(ended.id);
|
|
19
|
+
emit({ type: "end", view: ended });
|
|
20
|
+
return ended;
|
|
21
|
+
};
|
|
22
|
+
const later = opts.viewKind ?? "soft";
|
|
23
|
+
/** Starts a view; asLoad makes it the load view. */
|
|
24
|
+
const start = (o, asLoad = LOAD_KINDS.has(o.kind)) => {
|
|
25
|
+
const previous = endCurrent();
|
|
26
|
+
const session = opts.session.current();
|
|
27
|
+
const view = {
|
|
28
|
+
id: randomId(),
|
|
29
|
+
name: o.name ?? opts.viewName(new URL(opts.location())),
|
|
30
|
+
kind: o.kind,
|
|
31
|
+
startTime: o.startTime ?? opts.now(),
|
|
32
|
+
sessionId: session?.id ?? "",
|
|
33
|
+
};
|
|
34
|
+
if (session === undefined)
|
|
35
|
+
return view; // no consent: nothing is tracked
|
|
36
|
+
current = view;
|
|
37
|
+
if (asLoad)
|
|
38
|
+
load = view;
|
|
39
|
+
opts.summary.open(view);
|
|
40
|
+
emit({
|
|
41
|
+
type: "start",
|
|
42
|
+
view,
|
|
43
|
+
...(previous === undefined ? {} : { previous }),
|
|
44
|
+
...(o.cause === undefined ? {} : { cause: o.cause }),
|
|
45
|
+
...(o.rendered === true ? { rendered: true } : {}),
|
|
46
|
+
});
|
|
47
|
+
return view;
|
|
48
|
+
};
|
|
49
|
+
const onPageShow = (e) => {
|
|
50
|
+
if (e.persisted === true && current !== undefined) {
|
|
51
|
+
start({ kind: "bfcache_restore", name: current.name });
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
opts.window?.addEventListener("pageshow", onPageShow);
|
|
55
|
+
const unsubscribe = opts.session.subscribe((e) => {
|
|
56
|
+
if (e.type !== "start" || e.previous === undefined || current === undefined)
|
|
57
|
+
return;
|
|
58
|
+
if (current.sessionId !== e.session.id)
|
|
59
|
+
start({ kind: later, name: current.name, rendered: true });
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
current: () => current,
|
|
63
|
+
loadView: () => load,
|
|
64
|
+
start: (o) => start(o),
|
|
65
|
+
rename(name) {
|
|
66
|
+
if (current === undefined)
|
|
67
|
+
return;
|
|
68
|
+
current = { ...current, name };
|
|
69
|
+
if (load?.id === current.id)
|
|
70
|
+
load = current;
|
|
71
|
+
opts.summary.rename(current.id, name);
|
|
72
|
+
emit({ type: "rename", view: current });
|
|
73
|
+
},
|
|
74
|
+
rendered(at) {
|
|
75
|
+
if (current !== undefined)
|
|
76
|
+
emit({ type: "rendered", view: current, ...(at === undefined ? {} : { at }) });
|
|
77
|
+
},
|
|
78
|
+
appLoadStarted(ctx) {
|
|
79
|
+
appLoad = ctx;
|
|
80
|
+
},
|
|
81
|
+
appLoad: () => appLoad,
|
|
82
|
+
attachSpan(viewId, ctx) {
|
|
83
|
+
if (current?.id === viewId)
|
|
84
|
+
current = { ...current, spanContext: ctx };
|
|
85
|
+
if (load?.id === viewId)
|
|
86
|
+
load = { ...load, spanContext: ctx };
|
|
87
|
+
},
|
|
88
|
+
begin() {
|
|
89
|
+
if (current !== undefined)
|
|
90
|
+
return;
|
|
91
|
+
// The initial view starts at time origin: a view covers getting into it from navigation start.
|
|
92
|
+
// A later begin (consent granted again) is a new session on a page already loaded, so its
|
|
93
|
+
// view is soft and starts now.
|
|
94
|
+
if (load === undefined) {
|
|
95
|
+
const kind = opts.viewKind ?? (opts.prerendered() ? "prerender" : "initial");
|
|
96
|
+
start({ kind, startTime: opts.initialStart?.() ?? 0 }, true);
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
start({ kind: later, name: load.name });
|
|
100
|
+
},
|
|
101
|
+
end() {
|
|
102
|
+
endCurrent();
|
|
103
|
+
},
|
|
104
|
+
subscribe(listener) {
|
|
105
|
+
listeners.add(listener);
|
|
106
|
+
return () => listeners.delete(listener);
|
|
107
|
+
},
|
|
108
|
+
close() {
|
|
109
|
+
opts.window?.removeEventListener("pageshow", onPageShow);
|
|
110
|
+
unsubscribe();
|
|
111
|
+
listeners.clear();
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { VitalName } from "../contracts/summary.ts";
|
|
2
|
+
/** One web-vitals report, reduced to what RUM reads. Times are milliseconds. */
|
|
3
|
+
export interface VitalMetric {
|
|
4
|
+
readonly name: "LCP" | "INP" | "CLS" | "FCP" | "TTFB";
|
|
5
|
+
readonly value: number;
|
|
6
|
+
readonly delta: number;
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly navigationType: string;
|
|
9
|
+
readonly attribution: Readonly<Record<string, unknown>>;
|
|
10
|
+
}
|
|
11
|
+
/** The metric's value in the spec's unit: seconds for lcp, inp, fcp and ttfb, unitless for cls. */
|
|
12
|
+
export declare function vitalValue(name: VitalName, valueMillisOrScore: number): number;
|
|
13
|
+
/** The attribution attributes and o11y.rum.element.selector for one web-vitals report. */
|
|
14
|
+
export declare function attributionAttributes(metric: VitalMetric): Record<string, string | number>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vital values and attribution in the spec's units (spec 1.3.0, registry.o11y.rum.detail).
|
|
3
|
+
* web-vitals reports milliseconds; the spec records seconds, and CLS has no unit. Attribution
|
|
4
|
+
* fields map to o11y.rum.vital.attribution.* and the element to o11y.rum.element.selector. A field
|
|
5
|
+
* that is missing, not finite, empty or outside its enum is left out. No rating is ever produced:
|
|
6
|
+
* the gateway rates each vital.
|
|
7
|
+
*/
|
|
8
|
+
import { ATTR_RUM_ELEMENT_SELECTOR, ATTR_RUM_VITAL_ATTRIBUTION_ELEMENT_RENDER_DELAY, ATTR_RUM_VITAL_ATTRIBUTION_INPUT_DELAY, ATTR_RUM_VITAL_ATTRIBUTION_INTERACTION_TYPE, ATTR_RUM_VITAL_ATTRIBUTION_LARGEST_SHIFT_TIME, ATTR_RUM_VITAL_ATTRIBUTION_LARGEST_SHIFT_VALUE, ATTR_RUM_VITAL_ATTRIBUTION_LOAD_STATE, ATTR_RUM_VITAL_ATTRIBUTION_PRESENTATION_DELAY, ATTR_RUM_VITAL_ATTRIBUTION_PROCESSING_DURATION, ATTR_RUM_VITAL_ATTRIBUTION_RESOURCE_LOAD_DELAY, ATTR_RUM_VITAL_ATTRIBUTION_RESOURCE_LOAD_DURATION, ATTR_RUM_VITAL_ATTRIBUTION_TIME_TO_FIRST_BYTE, RUM_VITAL_ATTRIBUTION_INTERACTION_TYPE_VALUES, RUM_VITAL_ATTRIBUTION_LOAD_STATE_VALUES, } from "@libo11y/spec";
|
|
9
|
+
const MAX_SELECTOR = 200;
|
|
10
|
+
/** The metric's value in the spec's unit: seconds for lcp, inp, fcp and ttfb, unitless for cls. */
|
|
11
|
+
export function vitalValue(name, valueMillisOrScore) {
|
|
12
|
+
return name === "cls" ? valueMillisOrScore : valueMillisOrScore / 1000;
|
|
13
|
+
}
|
|
14
|
+
const FIELDS = {
|
|
15
|
+
LCP: [
|
|
16
|
+
["target", ATTR_RUM_ELEMENT_SELECTOR, "selector"],
|
|
17
|
+
["timeToFirstByte", ATTR_RUM_VITAL_ATTRIBUTION_TIME_TO_FIRST_BYTE, "ms"],
|
|
18
|
+
["resourceLoadDelay", ATTR_RUM_VITAL_ATTRIBUTION_RESOURCE_LOAD_DELAY, "ms"],
|
|
19
|
+
["resourceLoadDuration", ATTR_RUM_VITAL_ATTRIBUTION_RESOURCE_LOAD_DURATION, "ms"],
|
|
20
|
+
["elementRenderDelay", ATTR_RUM_VITAL_ATTRIBUTION_ELEMENT_RENDER_DELAY, "ms"],
|
|
21
|
+
],
|
|
22
|
+
INP: [
|
|
23
|
+
["interactionTarget", ATTR_RUM_ELEMENT_SELECTOR, "selector"],
|
|
24
|
+
[
|
|
25
|
+
"interactionType",
|
|
26
|
+
ATTR_RUM_VITAL_ATTRIBUTION_INTERACTION_TYPE,
|
|
27
|
+
RUM_VITAL_ATTRIBUTION_INTERACTION_TYPE_VALUES,
|
|
28
|
+
],
|
|
29
|
+
["inputDelay", ATTR_RUM_VITAL_ATTRIBUTION_INPUT_DELAY, "ms"],
|
|
30
|
+
["processingDuration", ATTR_RUM_VITAL_ATTRIBUTION_PROCESSING_DURATION, "ms"],
|
|
31
|
+
["presentationDelay", ATTR_RUM_VITAL_ATTRIBUTION_PRESENTATION_DELAY, "ms"],
|
|
32
|
+
["loadState", ATTR_RUM_VITAL_ATTRIBUTION_LOAD_STATE, RUM_VITAL_ATTRIBUTION_LOAD_STATE_VALUES],
|
|
33
|
+
],
|
|
34
|
+
CLS: [
|
|
35
|
+
["largestShiftTarget", ATTR_RUM_ELEMENT_SELECTOR, "selector"],
|
|
36
|
+
["largestShiftValue", ATTR_RUM_VITAL_ATTRIBUTION_LARGEST_SHIFT_VALUE, "score"],
|
|
37
|
+
["largestShiftTime", ATTR_RUM_VITAL_ATTRIBUTION_LARGEST_SHIFT_TIME, "ms"],
|
|
38
|
+
["loadState", ATTR_RUM_VITAL_ATTRIBUTION_LOAD_STATE, RUM_VITAL_ATTRIBUTION_LOAD_STATE_VALUES],
|
|
39
|
+
],
|
|
40
|
+
FCP: [["timeToFirstByte", ATTR_RUM_VITAL_ATTRIBUTION_TIME_TO_FIRST_BYTE, "ms"]],
|
|
41
|
+
TTFB: [],
|
|
42
|
+
};
|
|
43
|
+
/** The attribution attributes and o11y.rum.element.selector for one web-vitals report. */
|
|
44
|
+
export function attributionAttributes(metric) {
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const [source, attr, kind] of FIELDS[metric.name]) {
|
|
47
|
+
const v = metric.attribution[source];
|
|
48
|
+
if (kind === "ms" || kind === "score") {
|
|
49
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
50
|
+
out[attr] = kind === "ms" ? v / 1000 : v;
|
|
51
|
+
}
|
|
52
|
+
else if (typeof v === "string" && v !== "") {
|
|
53
|
+
if (kind === "selector")
|
|
54
|
+
out[attr] = v.slice(0, MAX_SELECTOR);
|
|
55
|
+
else if (kind.includes(v))
|
|
56
|
+
out[attr] = v;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RumCollector } from "../contracts/runtime.ts";
|
|
2
|
+
import { type VitalMetric } from "./attribution.ts";
|
|
3
|
+
export type { VitalMetric } from "./attribution.ts";
|
|
4
|
+
/** semconv's event for one web vital report. */
|
|
5
|
+
export declare const EVENT_BROWSER_WEB_VITAL = "browser.web_vital";
|
|
6
|
+
/** Registers report with the vitals source, once. */
|
|
7
|
+
export type VitalRegistrar = (report: (metric: VitalMetric) => void) => void;
|
|
8
|
+
/** For tests: the vitals collector over an injected registration function and task scheduler. */
|
|
9
|
+
export declare function vitalsWith(register: VitalRegistrar, defer?: (cb: () => void) => void): RumCollector;
|
|
10
|
+
/**
|
|
11
|
+
* Element selectors RUM built. web-vitals calls generateTarget only for an element still in the
|
|
12
|
+
* page; otherwise it falls back to "#" plus the element's raw ID (LCP) or the browser's own
|
|
13
|
+
* targetSelector (INP), which skip RUM's rules for generated IDs. clean keeps a selector field only
|
|
14
|
+
* when generateTarget returned that selector recently, and drops it otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export declare function trustedTargets(select: (node: Node | null) => string | undefined, max?: number): {
|
|
17
|
+
generateTarget(node: Node | null): string | undefined;
|
|
18
|
+
clean(attribution: Readonly<Record<string, unknown>>): Record<string, unknown>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Records LCP, INP, CLS, FCP and TTFB with attribution on the view that loaded the page. Every
|
|
22
|
+
* call returns the same collector, because web-vitals observes a page once.
|
|
23
|
+
*/
|
|
24
|
+
export declare function vitals(): RumCollector;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vitals() collector: LCP, INP, CLS, FCP and TTFB from the web-vitals attribution build,
|
|
3
|
+
* attributed to the view that loaded the page.
|
|
4
|
+
*
|
|
5
|
+
* Rules. The web-vitals callbacks are registered once per page, on the first start, with
|
|
6
|
+
* reportAllChanges true, and forward to the running collector: a revoked and granted page does not
|
|
7
|
+
* register twice, and reports while stopped are dropped. A report with navigationType
|
|
8
|
+
* back-forward-cache targets the current bfcache_restore view; any other report targets the load
|
|
9
|
+
* view current when the collector started, provided it belongs to the session of that start (after
|
|
10
|
+
* a revoke and grant, the old page load's vitals are not reported).
|
|
11
|
+
*
|
|
12
|
+
* FCP and TTFB are final when reported and are committed at once. LCP, CLS and INP change while the
|
|
13
|
+
* page is visible: the collector keeps the latest report of each and commits it when the page is
|
|
14
|
+
* hidden (runtime.onHide), which runs before the summaries are emitted and exported. web-vitals
|
|
15
|
+
* reports its own hide values from a capture listener on window, which runs before RUM's, and on a
|
|
16
|
+
* navigation RUM's hide runs at pagehide, before web-vitals sees visibilitychange, so the values
|
|
17
|
+
* committed are the latest ones known then. Committing calls summary.vital, which keeps the first
|
|
18
|
+
* value per view and vital, so a CLS or INP change after the page returns from hidden stays out of
|
|
19
|
+
* the summary (spec 1.3.0, once per view). Every commit of a changed value also goes out as the
|
|
20
|
+
* semconv browser.web_vital event, detail that only kept sessions send: its body carries name
|
|
21
|
+
* (lowercase), value, delta since the previous commit, and id, in the spec's units; its attributes
|
|
22
|
+
* the target view, its session and the attribution. No rating is ever sent. Stop forgets the
|
|
23
|
+
* values not yet committed.
|
|
24
|
+
*/
|
|
25
|
+
import { ATTR_RUM_VIEW_ID } from "@libo11y/spec";
|
|
26
|
+
import { onCLS, onFCP, onINP, onLCP, onTTFB } from "web-vitals/attribution";
|
|
27
|
+
import { elementSelector } from "../actions/naming.js";
|
|
28
|
+
import { ATTR_SESSION_ID } from "../attrs.js";
|
|
29
|
+
import { attributionAttributes, vitalValue } from "./attribution.js";
|
|
30
|
+
import { staleLcpAttribution } from "./lcp.js";
|
|
31
|
+
/** semconv's event for one web vital report. */
|
|
32
|
+
export const EVENT_BROWSER_WEB_VITAL = "browser.web_vital";
|
|
33
|
+
/** Vitals that are final when first reported. */
|
|
34
|
+
const FINAL_AT_REPORT = new Set(["FCP", "TTFB"]);
|
|
35
|
+
/** How long the views a page loaded are remembered as already hidden once. */
|
|
36
|
+
const MAX_HIDDEN_VIEWS = 16;
|
|
37
|
+
const CHANGE_AFTER_HIDE = new Set(["inp", "cls"]);
|
|
38
|
+
/** For tests: the vitals collector over an injected registration function and task scheduler. */
|
|
39
|
+
export function vitalsWith(register, defer = (cb) => setTimeout(cb, 0)) {
|
|
40
|
+
let registered = false;
|
|
41
|
+
let running;
|
|
42
|
+
let load;
|
|
43
|
+
/** Bumped by every start and stop, so a deferred report of an earlier run is dropped. */
|
|
44
|
+
let generation = 0;
|
|
45
|
+
/** The latest uncommitted report of each metric instance, by web-vitals id. */
|
|
46
|
+
const held = new Map();
|
|
47
|
+
/** The value last committed for each metric instance, in web-vitals units. */
|
|
48
|
+
const committed = new Map();
|
|
49
|
+
/** Load views the page has been hidden on at least once: later INP and CLS values are detail only. */
|
|
50
|
+
const hiddenOnce = new Set();
|
|
51
|
+
const target = (rt, m) => {
|
|
52
|
+
if (m.navigationType !== "back-forward-cache")
|
|
53
|
+
return load;
|
|
54
|
+
const v = rt.views.loadView();
|
|
55
|
+
return v?.kind === "bfcache_restore" ? v : undefined;
|
|
56
|
+
};
|
|
57
|
+
const commit = (rt, { metric: m, view }) => {
|
|
58
|
+
const previous = committed.get(m.id);
|
|
59
|
+
if (previous === m.value)
|
|
60
|
+
return;
|
|
61
|
+
committed.set(m.id, m.value);
|
|
62
|
+
const name = m.name.toLowerCase();
|
|
63
|
+
const value = vitalValue(name, m.value);
|
|
64
|
+
if (!(CHANGE_AFTER_HIDE.has(name) && hiddenOnce.has(view.id)))
|
|
65
|
+
rt.summary.vital(view.id, name, value);
|
|
66
|
+
rt.emit(EVENT_BROWSER_WEB_VITAL, { ...attributionAttributes(m), [ATTR_SESSION_ID]: view.sessionId, [ATTR_RUM_VIEW_ID]: view.id }, undefined, { name, value, delta: vitalValue(name, m.value - (previous ?? 0)), id: m.id });
|
|
67
|
+
};
|
|
68
|
+
const accept = (rt, m) => {
|
|
69
|
+
const view = target(rt, m);
|
|
70
|
+
if (view === undefined)
|
|
71
|
+
return;
|
|
72
|
+
if (FINAL_AT_REPORT.has(m.name)) {
|
|
73
|
+
commit(rt, { metric: m, view });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
held.set(m.id, { metric: m, view });
|
|
77
|
+
// Pinned until committed, so the load view is not forgotten after many soft views.
|
|
78
|
+
rt.summary.pin(view.id, true);
|
|
79
|
+
};
|
|
80
|
+
const report = (m) => {
|
|
81
|
+
const rt = running;
|
|
82
|
+
if (rt === undefined || !rt.gate.recording())
|
|
83
|
+
return;
|
|
84
|
+
if (m.navigationType !== "back-forward-cache") {
|
|
85
|
+
accept(rt, m);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
// web-vitals reports a restore from its own pageshow listener, which may run before the view
|
|
89
|
+
// tracker's has started the bfcache_restore view, so a restore's report waits a task.
|
|
90
|
+
const gen = generation;
|
|
91
|
+
defer(() => {
|
|
92
|
+
if (gen === generation && running === rt && rt.gate.recording())
|
|
93
|
+
accept(rt, m);
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
const unpinHeld = (rt) => {
|
|
97
|
+
for (const h of held.values())
|
|
98
|
+
rt.summary.pin(h.view.id, false);
|
|
99
|
+
held.clear();
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
name: "vitals",
|
|
103
|
+
start(rt) {
|
|
104
|
+
generation++;
|
|
105
|
+
const v = rt.views.loadView();
|
|
106
|
+
load = v !== undefined && v.sessionId === rt.session.current()?.id ? v : undefined;
|
|
107
|
+
running = rt;
|
|
108
|
+
const offHide = rt.onHide(() => {
|
|
109
|
+
if (running !== rt || !rt.gate.recording())
|
|
110
|
+
return;
|
|
111
|
+
const due = [...held.values()];
|
|
112
|
+
unpinHeld(rt);
|
|
113
|
+
for (const h of due)
|
|
114
|
+
commit(rt, h);
|
|
115
|
+
for (const view of [load, rt.views.loadView()]) {
|
|
116
|
+
if (view === undefined)
|
|
117
|
+
continue;
|
|
118
|
+
hiddenOnce.delete(view.id);
|
|
119
|
+
hiddenOnce.add(view.id);
|
|
120
|
+
}
|
|
121
|
+
while (hiddenOnce.size > MAX_HIDDEN_VIEWS)
|
|
122
|
+
hiddenOnce.delete(hiddenOnce.values().next().value);
|
|
123
|
+
});
|
|
124
|
+
if (!registered) {
|
|
125
|
+
registered = true;
|
|
126
|
+
register(report);
|
|
127
|
+
}
|
|
128
|
+
return () => {
|
|
129
|
+
generation++;
|
|
130
|
+
offHide();
|
|
131
|
+
unpinHeld(rt);
|
|
132
|
+
if (running === rt)
|
|
133
|
+
running = undefined;
|
|
134
|
+
load = undefined;
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** The attribution fields that hold an element selector. */
|
|
140
|
+
const TARGET_FIELDS = ["target", "interactionTarget", "largestShiftTarget"];
|
|
141
|
+
const MAX_TRUSTED_TARGETS = 64;
|
|
142
|
+
/**
|
|
143
|
+
* Element selectors RUM built. web-vitals calls generateTarget only for an element still in the
|
|
144
|
+
* page; otherwise it falls back to "#" plus the element's raw ID (LCP) or the browser's own
|
|
145
|
+
* targetSelector (INP), which skip RUM's rules for generated IDs. clean keeps a selector field only
|
|
146
|
+
* when generateTarget returned that selector recently, and drops it otherwise.
|
|
147
|
+
*/
|
|
148
|
+
export function trustedTargets(select, max = MAX_TRUSTED_TARGETS) {
|
|
149
|
+
const made = new Set();
|
|
150
|
+
return {
|
|
151
|
+
generateTarget(node) {
|
|
152
|
+
const selector = select(node);
|
|
153
|
+
if (selector === undefined)
|
|
154
|
+
return undefined;
|
|
155
|
+
made.delete(selector);
|
|
156
|
+
made.add(selector);
|
|
157
|
+
while (made.size > max)
|
|
158
|
+
made.delete(made.values().next().value);
|
|
159
|
+
return selector;
|
|
160
|
+
},
|
|
161
|
+
clean(attribution) {
|
|
162
|
+
const out = { ...attribution };
|
|
163
|
+
for (const field of TARGET_FIELDS) {
|
|
164
|
+
const v = out[field];
|
|
165
|
+
if (v !== undefined && !(typeof v === "string" && made.has(v)))
|
|
166
|
+
delete out[field];
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const registerWebVitals = (report) => {
|
|
173
|
+
const targets = trustedTargets((node) => typeof Element !== "undefined" && node instanceof Element ? elementSelector(node) : undefined);
|
|
174
|
+
const opts = { reportAllChanges: true, generateTarget: targets.generateTarget };
|
|
175
|
+
const forward = (m) => report({
|
|
176
|
+
name: m.name,
|
|
177
|
+
value: m.value,
|
|
178
|
+
delta: m.delta,
|
|
179
|
+
id: m.id,
|
|
180
|
+
navigationType: m.navigationType,
|
|
181
|
+
attribution: targets.clean(m.attribution),
|
|
182
|
+
});
|
|
183
|
+
// A later LCP candidate of the same paint (Firefox) updates the metric's entries without a
|
|
184
|
+
// report (see lcp.ts). web-vitals names each candidate's element before it processes the entry,
|
|
185
|
+
// so once a batch of entries is processed, a metric whose latest entry is not the one attributed
|
|
186
|
+
// is attributed again and reported again with the same value.
|
|
187
|
+
const lcpTargets = new WeakMap();
|
|
188
|
+
let lastLcp;
|
|
189
|
+
let catchUpQueued = false;
|
|
190
|
+
const catchUp = () => {
|
|
191
|
+
catchUpQueued = false;
|
|
192
|
+
const m = lastLcp;
|
|
193
|
+
if (m === undefined)
|
|
194
|
+
return;
|
|
195
|
+
const attribution = staleLcpAttribution(m, (e) => (e.element ? lcpTargets.get(e.element) : undefined), (url) => performance.getEntriesByType("resource").find((r) => r.name === url));
|
|
196
|
+
if (attribution === undefined)
|
|
197
|
+
return;
|
|
198
|
+
// As web-vitals does on each report, so the next batch compares against this entry.
|
|
199
|
+
Object.assign(m, { attribution });
|
|
200
|
+
forward(m);
|
|
201
|
+
};
|
|
202
|
+
const lcpOpts = {
|
|
203
|
+
reportAllChanges: true,
|
|
204
|
+
generateTarget: (node) => {
|
|
205
|
+
const selector = targets.generateTarget(node);
|
|
206
|
+
if (node !== null && selector !== undefined)
|
|
207
|
+
lcpTargets.set(node, selector);
|
|
208
|
+
if (!catchUpQueued) {
|
|
209
|
+
catchUpQueued = true;
|
|
210
|
+
queueMicrotask(catchUp);
|
|
211
|
+
}
|
|
212
|
+
return selector;
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
onLCP((m) => {
|
|
216
|
+
lastLcp = m;
|
|
217
|
+
forward(m);
|
|
218
|
+
}, lcpOpts);
|
|
219
|
+
onINP(forward, opts);
|
|
220
|
+
onCLS(forward, opts);
|
|
221
|
+
onFCP(forward, opts);
|
|
222
|
+
onTTFB(forward, opts);
|
|
223
|
+
};
|
|
224
|
+
let shared;
|
|
225
|
+
/**
|
|
226
|
+
* Records LCP, INP, CLS, FCP and TTFB with attribution on the view that loaded the page. Every
|
|
227
|
+
* call returns the same collector, because web-vitals observes a page once.
|
|
228
|
+
*/
|
|
229
|
+
export function vitals() {
|
|
230
|
+
shared ??= vitalsWith(registerWebVitals);
|
|
231
|
+
return shared;
|
|
232
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LCP attribution for a candidate web-vitals did not report.
|
|
3
|
+
*
|
|
4
|
+
* Firefox reports every LCP candidate of one paint, smaller first, all with the same render time.
|
|
5
|
+
* With reportAllChanges, web-vitals reports only a change in value, so the larger candidates of
|
|
6
|
+
* that paint update the metric's entries but not its attribution, which still names the first,
|
|
7
|
+
* smaller element. staleLcpAttribution rebuilds the attribution for the latest entry the way
|
|
8
|
+
* web-vitals' attribution build does (web-vitals 5.3.0, attribution/onLCP).
|
|
9
|
+
*/
|
|
10
|
+
/** What RUM reads of a largest-contentful-paint entry. */
|
|
11
|
+
export interface LcpEntry {
|
|
12
|
+
readonly element?: Element | null;
|
|
13
|
+
readonly id?: string;
|
|
14
|
+
readonly url?: string;
|
|
15
|
+
}
|
|
16
|
+
/** What RUM reads of a resource timing entry. */
|
|
17
|
+
export interface LcpResource {
|
|
18
|
+
readonly requestStart: number;
|
|
19
|
+
readonly startTime: number;
|
|
20
|
+
readonly responseEnd: number;
|
|
21
|
+
}
|
|
22
|
+
/** A web-vitals LCP metric with attribution, as web-vitals keeps it between reports. */
|
|
23
|
+
export interface LcpMetric {
|
|
24
|
+
readonly value: number;
|
|
25
|
+
readonly entries: readonly LcpEntry[];
|
|
26
|
+
readonly attribution: Readonly<Record<string, unknown>>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The attribution of m's latest entry, or undefined when m's attribution already describes it.
|
|
30
|
+
* targetOf names an entry's element; resourceOf finds the resource timing entry for a URL.
|
|
31
|
+
*/
|
|
32
|
+
export declare function staleLcpAttribution(m: LcpMetric, targetOf: (entry: LcpEntry) => string | undefined, resourceOf: (url: string) => LcpResource | undefined): Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LCP attribution for a candidate web-vitals did not report.
|
|
3
|
+
*
|
|
4
|
+
* Firefox reports every LCP candidate of one paint, smaller first, all with the same render time.
|
|
5
|
+
* With reportAllChanges, web-vitals reports only a change in value, so the larger candidates of
|
|
6
|
+
* that paint update the metric's entries but not its attribution, which still names the first,
|
|
7
|
+
* smaller element. staleLcpAttribution rebuilds the attribution for the latest entry the way
|
|
8
|
+
* web-vitals' attribution build does (web-vitals 5.3.0, attribution/onLCP).
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* The attribution of m's latest entry, or undefined when m's attribution already describes it.
|
|
12
|
+
* targetOf names an entry's element; resourceOf finds the resource timing entry for a URL.
|
|
13
|
+
*/
|
|
14
|
+
export function staleLcpAttribution(m, targetOf, resourceOf) {
|
|
15
|
+
const entry = m.entries.at(-1);
|
|
16
|
+
if (entry === undefined || entry === m.attribution.lcpEntry)
|
|
17
|
+
return undefined;
|
|
18
|
+
const { target: _t, url: _u, lcpResourceEntry: _r, ...rest } = m.attribution;
|
|
19
|
+
const out = { ...rest, lcpEntry: entry };
|
|
20
|
+
const target = targetOf(entry);
|
|
21
|
+
if (target !== undefined)
|
|
22
|
+
out.target = target;
|
|
23
|
+
const resource = entry.url ? resourceOf(entry.url) : undefined;
|
|
24
|
+
if (entry.url)
|
|
25
|
+
out.url = entry.url;
|
|
26
|
+
if (resource !== undefined)
|
|
27
|
+
out.lcpResourceEntry = resource;
|
|
28
|
+
const nav = m.attribution.navigationEntry;
|
|
29
|
+
if (nav === undefined) {
|
|
30
|
+
return {
|
|
31
|
+
...out,
|
|
32
|
+
timeToFirstByte: 0,
|
|
33
|
+
resourceLoadDelay: 0,
|
|
34
|
+
resourceLoadDuration: 0,
|
|
35
|
+
elementRenderDelay: m.value,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const activationStart = nav.activationStart || 0;
|
|
39
|
+
const ttfb = Math.max(0, nav.responseStart - activationStart);
|
|
40
|
+
const requestStart = Math.max(ttfb, resource ? (resource.requestStart || resource.startTime) - activationStart : 0);
|
|
41
|
+
const responseEnd = Math.min(m.value, Math.max(requestStart, resource ? resource.responseEnd - activationStart : 0));
|
|
42
|
+
return {
|
|
43
|
+
...out,
|
|
44
|
+
timeToFirstByte: ttfb,
|
|
45
|
+
resourceLoadDelay: requestStart - ttfb,
|
|
46
|
+
resourceLoadDuration: responseEnd - requestStart,
|
|
47
|
+
elementRenderDelay: m.value - responseEnd,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slice of the extension API RUM uses in extension contexts, typed by hand as @libo11y/webext
|
|
3
|
+
* types its own, so tests can pass a fake.
|
|
4
|
+
*/
|
|
5
|
+
/** A storage area: chrome.storage.local or chrome.storage.session. */
|
|
6
|
+
export interface StorageAreaLike {
|
|
7
|
+
get(key: string): Promise<Record<string, unknown>>;
|
|
8
|
+
set(items: Record<string, unknown>): Promise<void>;
|
|
9
|
+
remove(key: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
/** A storage change, as storage.onChanged reports it. */
|
|
12
|
+
export type StorageChanges = Record<string, {
|
|
13
|
+
readonly oldValue?: unknown;
|
|
14
|
+
readonly newValue?: unknown;
|
|
15
|
+
}>;
|
|
16
|
+
/** A storage.onChanged listener. */
|
|
17
|
+
export type StorageChangeListener = (changes: StorageChanges, areaName: string) => void;
|
|
18
|
+
/** chrome.storage.session and the change event, as the session store uses them. */
|
|
19
|
+
export interface ChromeSessionStorageLike {
|
|
20
|
+
readonly storage?: {
|
|
21
|
+
readonly session?: StorageAreaLike & {
|
|
22
|
+
/** Chrome 102 and Firefox 129 on; absent before. */
|
|
23
|
+
setAccessLevel?(o: {
|
|
24
|
+
readonly accessLevel: "TRUSTED_AND_UNTRUSTED_CONTEXTS" | "TRUSTED_CONTEXTS";
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
readonly onChanged: {
|
|
28
|
+
addListener(listener: StorageChangeListener): void;
|
|
29
|
+
removeListener(listener: StorageChangeListener): void;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** The extension API slice setupRumExtension reads. */
|
|
34
|
+
export interface RumChromeLike extends ChromeSessionStorageLike {
|
|
35
|
+
readonly runtime: {
|
|
36
|
+
readonly id: string;
|
|
37
|
+
/** The URL of a file in the extension, getURL("") its base. */
|
|
38
|
+
getURL?(path: string): string;
|
|
39
|
+
};
|
|
40
|
+
readonly storage?: ChromeSessionStorageLike["storage"] & {
|
|
41
|
+
readonly local?: StorageAreaLike;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** globalThis.chrome, else globalThis.browser, as @libo11y/webext finds it. */
|
|
45
|
+
export declare function getRumChrome(): RumChromeLike | undefined;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slice of the extension API RUM uses in extension contexts, typed by hand as @libo11y/webext
|
|
3
|
+
* types its own, so tests can pass a fake.
|
|
4
|
+
*/
|
|
5
|
+
/** globalThis.chrome, else globalThis.browser, as @libo11y/webext finds it. */
|
|
6
|
+
export function getRumChrome() {
|
|
7
|
+
const g = globalThis;
|
|
8
|
+
return g.chrome?.runtime !== undefined
|
|
9
|
+
? g.chrome
|
|
10
|
+
: g.browser?.runtime !== undefined
|
|
11
|
+
? g.browser
|
|
12
|
+
: undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type RemoteConfigStorage } from "../config/remote.ts";
|
|
2
|
+
import type { RumChromeLike } from "./chrome.ts";
|
|
3
|
+
/** The synchronous storage remote configuration caches in, with flush for its writes to chrome.storage.local. */
|
|
4
|
+
export type ConfigStorage = RemoteConfigStorage & {
|
|
5
|
+
/** Resolves once every earlier write reached storage. Never rejects. */
|
|
6
|
+
flush(): Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
/** The slice of navigator.locks an update runs under. */
|
|
9
|
+
export interface ConfigLocks {
|
|
10
|
+
request(name: string, cb: () => Promise<void>): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A cache over chrome.storage.local once the stored values are read, or undefined without the area.
|
|
14
|
+
* locks null updates without a lock. Never rejects.
|
|
15
|
+
*/
|
|
16
|
+
export declare function chromeConfigStorage(chrome: RumChromeLike, locks?: ConfigLocks | null | undefined): Promise<(ConfigStorage & {
|
|
17
|
+
close(): void;
|
|
18
|
+
}) | undefined>;
|