@libo11y/rum 0.0.0-stage → 0.4.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 +461 -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 +80 -0
- package/dist/config/remote.js +209 -0
- package/dist/context-manager.d.ts +8 -0
- package/dist/context-manager.js +29 -0
- package/dist/contracts/binding.d.ts +80 -0
- package/dist/contracts/binding.js +1 -0
- package/dist/contracts/retention.d.ts +28 -0
- package/dist/contracts/retention.js +1 -0
- package/dist/contracts/runtime.d.ts +154 -0
- package/dist/contracts/runtime.js +1 -0
- package/dist/contracts/session.d.ts +63 -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 +86 -0
- package/dist/core-setup.js +902 -0
- package/dist/core.d.ts +42 -0
- package/dist/core.js +35 -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 +28 -0
- package/dist/internal.js +26 -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 +25 -0
- package/dist/retention/buffer.js +104 -0
- package/dist/retention/exporters.d.ts +17 -0
- package/dist/retention/exporters.js +67 -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 +40 -0
- package/dist/setup.js +49 -0
- package/dist/span-processor.d.ts +38 -0
- package/dist/span-processor.js +125 -0
- package/dist/summary/accumulator.d.ts +25 -0
- package/dist/summary/accumulator.js +126 -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 +285 -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 +28 -0
- package/dist/views/tracker.js +117 -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 +7 -0
- package/dist/webext/config-storage.js +47 -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 +94 -0
- package/dist/webext/setup.d.ts +71 -0
- package/dist/webext/setup.js +193 -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 +57 -4
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vitals() collector: LCP, INP, CLS, FCP and TTFB from the web-vitals attribution build
|
|
3
|
+
* (decision P6), attributed to the view that loaded the page (decision P7, orchestrator Q5).
|
|
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,7 @@
|
|
|
1
|
+
import type { RumChromeLike } from "./chrome.ts";
|
|
2
|
+
/** The synchronous storage remote configuration caches in. */
|
|
3
|
+
export type ConfigStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;
|
|
4
|
+
/** A cache over chrome.storage.local once the stored value is read, or undefined without the area. Never rejects. */
|
|
5
|
+
export declare function chromeConfigStorage(chrome: RumChromeLike): Promise<(ConfigStorage & {
|
|
6
|
+
close(): void;
|
|
7
|
+
}) | undefined>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The remote configuration cache in an extension: chrome.storage.local, shared by every context of
|
|
3
|
+
* the extension and never the host page's localStorage. Remote configuration reads its cache
|
|
4
|
+
* synchronously, so this reads the stored value once, keeps it in memory and follows other
|
|
5
|
+
* contexts' writes through storage.onChanged. Writes go to memory at once and to storage after.
|
|
6
|
+
*/
|
|
7
|
+
import { diag } from "@opentelemetry/api";
|
|
8
|
+
import { CONFIG_STORAGE_KEY } from "../config/remote.js";
|
|
9
|
+
/** A cache over chrome.storage.local once the stored value is read, or undefined without the area. Never rejects. */
|
|
10
|
+
export async function chromeConfigStorage(chrome) {
|
|
11
|
+
const area = chrome.storage?.local;
|
|
12
|
+
if (area === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const values = new Map();
|
|
15
|
+
try {
|
|
16
|
+
const stored = (await area.get(CONFIG_STORAGE_KEY))[CONFIG_STORAGE_KEY];
|
|
17
|
+
if (typeof stored === "string")
|
|
18
|
+
values.set(CONFIG_STORAGE_KEY, stored);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
diag.debug("o11y: rum: remote configuration cache unreadable", err);
|
|
22
|
+
}
|
|
23
|
+
const onChanged = (changes, areaName) => {
|
|
24
|
+
if (areaName !== "local" || !(CONFIG_STORAGE_KEY in changes))
|
|
25
|
+
return;
|
|
26
|
+
const next = changes[CONFIG_STORAGE_KEY]?.newValue;
|
|
27
|
+
if (typeof next === "string")
|
|
28
|
+
values.set(CONFIG_STORAGE_KEY, next);
|
|
29
|
+
else
|
|
30
|
+
values.delete(CONFIG_STORAGE_KEY);
|
|
31
|
+
};
|
|
32
|
+
chrome.storage?.onChanged.addListener(onChanged);
|
|
33
|
+
const failed = (err) => diag.debug("o11y: rum: remote configuration cache write failed", err);
|
|
34
|
+
return {
|
|
35
|
+
getItem: (key) => values.get(key) ?? null,
|
|
36
|
+
setItem(key, value) {
|
|
37
|
+
values.set(key, value);
|
|
38
|
+
area.set({ [key]: value }).catch(failed);
|
|
39
|
+
},
|
|
40
|
+
removeItem(key) {
|
|
41
|
+
values.delete(key);
|
|
42
|
+
area.remove(key).catch(failed);
|
|
43
|
+
},
|
|
44
|
+
/** Stops following other contexts' writes. */
|
|
45
|
+
close: () => chrome.storage?.onChanged.removeListener(onChanged),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The content script rules (decision P20). A content script shares the host page's document, so
|
|
3
|
+
* RUM there records only what is the extension's own: actions inside the extension's UI root,
|
|
4
|
+
* errors from the extension's own scripts, and one fixed view whose span names the host only by
|
|
5
|
+
* url.domain.
|
|
6
|
+
*/
|
|
7
|
+
import type { RumScope } from "../contracts/runtime.ts";
|
|
8
|
+
/**
|
|
9
|
+
* Where an error event came from: a failed element's resource URL, a CSP violation's source file,
|
|
10
|
+
* an uncaught error's filename (else its stack's first frame), or a rejection reason's first frame.
|
|
11
|
+
*/
|
|
12
|
+
export declare function errorSourceUrl(e: Event): string | undefined;
|
|
13
|
+
export interface ContentScopeOptions {
|
|
14
|
+
/** The extension's base URL, chrome.runtime.getURL(""). */
|
|
15
|
+
readonly base: string;
|
|
16
|
+
/** The extension's own UI in the page. Without it, no actions. */
|
|
17
|
+
readonly uiRoot?: Node;
|
|
18
|
+
/** The host page's host name, for url.domain. */
|
|
19
|
+
readonly host: string;
|
|
20
|
+
}
|
|
21
|
+
/** The RumScope of a content script. */
|
|
22
|
+
export declare function contentScope(o: ContentScopeOptions): RumScope;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** An extension or web URL. */
|
|
2
|
+
const URL_IN = /\b(?:(?:chrome|moz|safari-web)-extension|https?|file):\/\/[^\s)'"]+/;
|
|
3
|
+
/** A stack frame line: V8's " at ..." or Firefox and Safari's "name@url". */
|
|
4
|
+
const FRAME_LINE = /^\s+at\s|^[^\s@]*@/;
|
|
5
|
+
/** The URL of the first frame of a stack trace, skipping the message lines above the frames. */
|
|
6
|
+
function firstFrame(stack) {
|
|
7
|
+
if (typeof stack !== "string")
|
|
8
|
+
return undefined;
|
|
9
|
+
for (const line of stack.split("\n")) {
|
|
10
|
+
if (FRAME_LINE.test(line))
|
|
11
|
+
return URL_IN.exec(line)?.[0];
|
|
12
|
+
}
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
function stackOf(v) {
|
|
16
|
+
return typeof v === "object" && v !== null ? v.stack : undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Where an error event came from: a failed element's resource URL, a CSP violation's source file,
|
|
20
|
+
* an uncaught error's filename (else its stack's first frame), or a rejection reason's first frame.
|
|
21
|
+
*/
|
|
22
|
+
export function errorSourceUrl(e) {
|
|
23
|
+
const target = e.target;
|
|
24
|
+
if (e.type === "error" && typeof target?.tagName === "string") {
|
|
25
|
+
for (const v of [target.currentSrc, target.src, target.href])
|
|
26
|
+
if (typeof v === "string" && v !== "")
|
|
27
|
+
return v;
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
if (e.type === "securitypolicyviolation") {
|
|
31
|
+
const file = e.sourceFile;
|
|
32
|
+
return typeof file === "string" && file !== "" ? file : undefined;
|
|
33
|
+
}
|
|
34
|
+
if (e.type === "unhandledrejection")
|
|
35
|
+
return firstFrame(stackOf(e.reason));
|
|
36
|
+
const ev = e;
|
|
37
|
+
if (typeof ev.filename === "string" && ev.filename !== "")
|
|
38
|
+
return ev.filename;
|
|
39
|
+
return firstFrame(stackOf(ev.error));
|
|
40
|
+
}
|
|
41
|
+
/** The RumScope of a content script. */
|
|
42
|
+
export function contentScope(o) {
|
|
43
|
+
const root = o.uiRoot;
|
|
44
|
+
return {
|
|
45
|
+
action: (e) => root !== undefined && typeof e.composedPath === "function" && e.composedPath().includes(root),
|
|
46
|
+
error: (e) => errorSourceUrl(e)?.startsWith(o.base) === true,
|
|
47
|
+
fixedView: true,
|
|
48
|
+
...(o.host === "" ? {} : { viewAttributes: { "url.domain": o.host } }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opening the extension's session area to content scripts. A module of its own, which imports
|
|
3
|
+
* nothing, so the @libo11y/rum/webext entry reaches it before the extension setup and the bundle
|
|
4
|
+
* keeps its module order.
|
|
5
|
+
*/
|
|
6
|
+
import type { ChromeSessionStorageLike } from "./chrome.ts";
|
|
7
|
+
/**
|
|
8
|
+
* Lets content scripts read and write chrome.storage.session, so they share the extension's RUM
|
|
9
|
+
* session. Call it in the service worker on every wake. It opens the whole session area to content
|
|
10
|
+
* scripts, so keep nothing there that the pages a content script runs on must not reach. Resolves
|
|
11
|
+
* where the browser has no setAccessLevel.
|
|
12
|
+
*/
|
|
13
|
+
export declare function allowRumSessionInContentScripts(chrome: ChromeSessionStorageLike): Promise<void>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lets content scripts read and write chrome.storage.session, so they share the extension's RUM
|
|
3
|
+
* session. Call it in the service worker on every wake. It opens the whole session area to content
|
|
4
|
+
* scripts, so keep nothing there that the pages a content script runs on must not reach. Resolves
|
|
5
|
+
* where the browser has no setAccessLevel.
|
|
6
|
+
*/
|
|
7
|
+
export async function allowRumSessionInContentScripts(chrome) {
|
|
8
|
+
await chrome.storage?.session?.setAccessLevel?.({ accessLevel: "TRUSTED_AND_UNTRUSTED_CONTEXTS" });
|
|
9
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SessionStore } from "../contracts/session.ts";
|
|
2
|
+
import type { ChromeSessionStorageLike } from "./chrome.ts";
|
|
3
|
+
export { allowRumSessionInContentScripts } from "./session-access.ts";
|
|
4
|
+
/** The extension SessionStore. Storage is reached on first use, which the session manager makes only after consent. */
|
|
5
|
+
export declare function chromeSessionStore(chrome: ChromeSessionStorageLike): SessionStore;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The extension session store (decision P20): the session record in chrome.storage.session, which
|
|
3
|
+
* every context of the extension shares and the browser clears when it closes, and
|
|
4
|
+
* storage.onChanged, which tells the other contexts of a save. Records are the web store's version
|
|
5
|
+
* 1 JSON plus w, a token naming the store that wrote it, so a store ignores its own saves when the
|
|
6
|
+
* change event comes back to it.
|
|
7
|
+
*
|
|
8
|
+
* Content scripts reach chrome.storage.session only once the service worker opened it to them
|
|
9
|
+
* (allowRumSessionInContentScripts). Until then each content script's store only keeps its session
|
|
10
|
+
* in memory, so it starts sessions of its own.
|
|
11
|
+
*/
|
|
12
|
+
import { diag } from "@opentelemetry/api";
|
|
13
|
+
import { randomUuid } from "../ids.js";
|
|
14
|
+
import { parseSessionState, SESSION_STORAGE_KEY } from "../session/local-store.js";
|
|
15
|
+
export { allowRumSessionInContentScripts } from "./session-access.js";
|
|
16
|
+
/** The extension SessionStore. Storage is reached on first use, which the session manager makes only after consent. */
|
|
17
|
+
export function chromeSessionStore(chrome) {
|
|
18
|
+
const token = randomUuid();
|
|
19
|
+
const area = chrome.storage?.session;
|
|
20
|
+
const listeners = new Set();
|
|
21
|
+
let warned = false;
|
|
22
|
+
const failed = (what, err) => {
|
|
23
|
+
if (warned) {
|
|
24
|
+
diag.debug(`o11y: rum: chrome.storage.session ${what} failed`, err);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
warned = true;
|
|
28
|
+
diag.warn(`o11y: rum: chrome.storage.session ${what} failed; in a content script, call allowRumSessionInContentScripts in the worker`, err);
|
|
29
|
+
};
|
|
30
|
+
const onChanged = (changes, areaName) => {
|
|
31
|
+
if (areaName !== "session" || !(SESSION_STORAGE_KEY in changes))
|
|
32
|
+
return;
|
|
33
|
+
const next = changes[SESSION_STORAGE_KEY]?.newValue;
|
|
34
|
+
if (next === undefined) {
|
|
35
|
+
for (const l of listeners)
|
|
36
|
+
l(undefined);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (next?.w === token)
|
|
40
|
+
return; // this store's own save
|
|
41
|
+
const s = parseSessionState(next);
|
|
42
|
+
if (s === undefined)
|
|
43
|
+
return;
|
|
44
|
+
for (const l of listeners)
|
|
45
|
+
l(s);
|
|
46
|
+
};
|
|
47
|
+
let listening = false;
|
|
48
|
+
const listen = () => {
|
|
49
|
+
if (listening)
|
|
50
|
+
return;
|
|
51
|
+
listening = true;
|
|
52
|
+
chrome.storage?.onChanged.addListener(onChanged);
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
async load() {
|
|
56
|
+
listen();
|
|
57
|
+
if (area === undefined)
|
|
58
|
+
return undefined;
|
|
59
|
+
try {
|
|
60
|
+
const items = await area.get(SESSION_STORAGE_KEY);
|
|
61
|
+
return parseSessionState(items[SESSION_STORAGE_KEY]);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
failed("read", err);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
save(state) {
|
|
69
|
+
listen();
|
|
70
|
+
if (area === undefined)
|
|
71
|
+
return;
|
|
72
|
+
try {
|
|
73
|
+
const write = state === undefined
|
|
74
|
+
? area.remove(SESSION_STORAGE_KEY)
|
|
75
|
+
: area.set({ [SESSION_STORAGE_KEY]: { v: 1, ...state, w: token } });
|
|
76
|
+
write.catch((err) => failed("write", err));
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
failed("write", err);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
onChange(listener) {
|
|
83
|
+
listen();
|
|
84
|
+
listeners.add(listener);
|
|
85
|
+
return () => listeners.delete(listener);
|
|
86
|
+
},
|
|
87
|
+
close() {
|
|
88
|
+
if (listening)
|
|
89
|
+
chrome.storage?.onChanged.removeListener(onChanged);
|
|
90
|
+
listening = false;
|
|
91
|
+
listeners.clear();
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|