@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,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* views(): view spans and soft navigations.
|
|
3
|
+
*
|
|
4
|
+
* Every view the tracker starts gets a view span named "view <o11y.rum.view.name>" with
|
|
5
|
+
* o11y.rum.span.shape view, the platform, the view's name, ID and kind, and its session.id. It
|
|
6
|
+
* starts at the view's startTime. A soft or bfcache_restore view span is a root, linked to the
|
|
7
|
+
* action that caused it when there is one. An initial or prerender view span is a child of
|
|
8
|
+
* app_load (web's documentLoad span). Its creation waits for the span processor to report
|
|
9
|
+
* app_load, and once the view span is due to end it waits at most APP_LOAD_WAIT_MILLIS more, then
|
|
10
|
+
* starts as a root. The tracker learns the span's context (attachSpan), so actions link to it.
|
|
11
|
+
*
|
|
12
|
+
* A view span ends at the first paint after its view rendered: rum.viewRendered() (router
|
|
13
|
+
* adapters) or, in history mode, by itself. In history mode a soft view rendered once no DOM
|
|
14
|
+
* mutation happened for RENDER_IDLE_MILLIS, a bfcache_restore view at once, and the initial view
|
|
15
|
+
* at loadEventEnd when the page has loaded already, else at the first paint after the load event.
|
|
16
|
+
* Every view span ends VIEW_SPAN_CAP_MILLIS after it started at the latest, and at once when its
|
|
17
|
+
* view ends first. In history mode, every path change through pushState, replaceState or popstate
|
|
18
|
+
* starts a soft view named by settings().viewName, caused by the open action. With history off,
|
|
19
|
+
* nothing renders by itself and router adapters call rum.startView and rum.viewRendered. A runtime
|
|
20
|
+
* scope with fixedView (a content script) follows no URL change, and its viewAttributes go on every
|
|
21
|
+
* view span. Stop
|
|
22
|
+
* ends every open view span and removes every listener, observer and timer.
|
|
23
|
+
*/
|
|
24
|
+
import { ATTR_RUM_PLATFORM, ATTR_RUM_SPAN_SHAPE, ATTR_RUM_VIEW_ID, ATTR_RUM_VIEW_KIND, ATTR_RUM_VIEW_NAME, RUM_SPAN_SHAPE_VALUE_VIEW, } from "@libo11y/spec";
|
|
25
|
+
import { ROOT_CONTEXT, trace } from "@opentelemetry/api";
|
|
26
|
+
import { ATTR_SESSION_ID } from "../attrs.js";
|
|
27
|
+
import { watchHistory } from "./history.js";
|
|
28
|
+
import { afterNextPaint } from "./paint.js";
|
|
29
|
+
/** A view span ends this long after it started at the latest. */
|
|
30
|
+
export const VIEW_SPAN_CAP_MILLIS = 10_000;
|
|
31
|
+
/** In history mode, a soft view rendered once the DOM was quiet this long. */
|
|
32
|
+
export const RENDER_IDLE_MILLIS = 100;
|
|
33
|
+
/** How long a load view span due to end waits for app_load before it starts as a root. */
|
|
34
|
+
export const APP_LOAD_WAIT_MILLIS = 1000;
|
|
35
|
+
const APP_LOAD_POLL_MILLIS = 100;
|
|
36
|
+
/** The DOM mutations that count as rendering. */
|
|
37
|
+
export const MUTATIONS = {
|
|
38
|
+
subtree: true,
|
|
39
|
+
childList: true,
|
|
40
|
+
attributes: true,
|
|
41
|
+
characterData: true,
|
|
42
|
+
};
|
|
43
|
+
const LOAD_SPAN_KINDS = new Set(["initial", "prerender"]);
|
|
44
|
+
export function viewsWith(opts, env) {
|
|
45
|
+
const followHistory = opts.history !== false;
|
|
46
|
+
return {
|
|
47
|
+
name: "views",
|
|
48
|
+
start(rt) {
|
|
49
|
+
// A fixed view scope (a content script) keeps its one view whatever the host page's URL does.
|
|
50
|
+
const history = followHistory && rt.scope?.fixedView !== true;
|
|
51
|
+
let stopped = false;
|
|
52
|
+
const open = new Map();
|
|
53
|
+
const timer = (o, cb, ms) => {
|
|
54
|
+
const h = env.setTimeout(() => {
|
|
55
|
+
o.timers.delete(h);
|
|
56
|
+
cb();
|
|
57
|
+
}, ms);
|
|
58
|
+
o.timers.add(h);
|
|
59
|
+
return h;
|
|
60
|
+
};
|
|
61
|
+
const quiet = (o) => {
|
|
62
|
+
for (const h of o.timers)
|
|
63
|
+
env.clearTimeout(h);
|
|
64
|
+
o.timers.clear();
|
|
65
|
+
o.observer?.disconnect();
|
|
66
|
+
o.observer = undefined;
|
|
67
|
+
o.offLoad?.();
|
|
68
|
+
o.offLoad = undefined;
|
|
69
|
+
};
|
|
70
|
+
// A fixed view (a content script) has no app_load: the host page's load is not its own.
|
|
71
|
+
const waitsForAppLoad = (o) => LOAD_SPAN_KINDS.has(o.view.kind) && rt.views.appLoad() === undefined && rt.scope?.fixedView !== true;
|
|
72
|
+
const create = (o) => {
|
|
73
|
+
if (o.span !== undefined)
|
|
74
|
+
return o.span;
|
|
75
|
+
const v = o.view;
|
|
76
|
+
const appLoad = LOAD_SPAN_KINDS.has(v.kind) ? rt.views.appLoad() : undefined;
|
|
77
|
+
const span = rt.tracer.startSpan(`view ${v.name}`, {
|
|
78
|
+
startTime: env.timeOrigin + v.startTime,
|
|
79
|
+
attributes: {
|
|
80
|
+
...rt.scope?.viewAttributes,
|
|
81
|
+
[ATTR_RUM_SPAN_SHAPE]: RUM_SPAN_SHAPE_VALUE_VIEW,
|
|
82
|
+
[ATTR_RUM_PLATFORM]: rt.platform,
|
|
83
|
+
[ATTR_RUM_VIEW_NAME]: v.name,
|
|
84
|
+
[ATTR_RUM_VIEW_ID]: v.id,
|
|
85
|
+
[ATTR_RUM_VIEW_KIND]: v.kind,
|
|
86
|
+
[ATTR_SESSION_ID]: v.sessionId,
|
|
87
|
+
},
|
|
88
|
+
...(o.cause === undefined ? {} : { links: [{ context: o.cause }] }),
|
|
89
|
+
}, appLoad === undefined ? ROOT_CONTEXT : trace.setSpanContext(ROOT_CONTEXT, appLoad));
|
|
90
|
+
o.span = span;
|
|
91
|
+
rt.views.attachSpan(v.id, span.spanContext());
|
|
92
|
+
return span;
|
|
93
|
+
};
|
|
94
|
+
const close = (o) => {
|
|
95
|
+
quiet(o);
|
|
96
|
+
open.delete(o.view.id);
|
|
97
|
+
create(o).end(env.timeOrigin + (o.endAt ?? rt.now()));
|
|
98
|
+
};
|
|
99
|
+
const finish = (o, at) => {
|
|
100
|
+
if (o.endAt !== undefined)
|
|
101
|
+
return;
|
|
102
|
+
o.endAt = at;
|
|
103
|
+
if (o.span !== undefined || !waitsForAppLoad(o)) {
|
|
104
|
+
close(o);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// The load view is due but app_load is not known yet: wait a little more for it.
|
|
108
|
+
o.observer?.disconnect();
|
|
109
|
+
o.observer = undefined;
|
|
110
|
+
o.waitUntil = rt.now() + APP_LOAD_WAIT_MILLIS;
|
|
111
|
+
};
|
|
112
|
+
const poll = (o) => {
|
|
113
|
+
timer(o, () => {
|
|
114
|
+
if (rt.views.appLoad() !== undefined) {
|
|
115
|
+
create(o);
|
|
116
|
+
if (o.endAt !== undefined)
|
|
117
|
+
close(o);
|
|
118
|
+
}
|
|
119
|
+
else if (o.waitUntil !== undefined && rt.now() >= o.waitUntil)
|
|
120
|
+
close(o);
|
|
121
|
+
else
|
|
122
|
+
poll(o);
|
|
123
|
+
}, APP_LOAD_POLL_MILLIS);
|
|
124
|
+
};
|
|
125
|
+
const paintThen = (o) => {
|
|
126
|
+
if (o.rendered || o.endAt !== undefined)
|
|
127
|
+
return;
|
|
128
|
+
o.rendered = true;
|
|
129
|
+
o.observer?.disconnect();
|
|
130
|
+
o.observer = undefined;
|
|
131
|
+
afterNextPaint(env.paint, rt.now, (at) => {
|
|
132
|
+
if (!stopped && open.get(o.view.id) === o)
|
|
133
|
+
finish(o, at);
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
const detectRender = (o) => {
|
|
137
|
+
let idle;
|
|
138
|
+
const arm = () => {
|
|
139
|
+
if (idle !== undefined) {
|
|
140
|
+
env.clearTimeout(idle);
|
|
141
|
+
o.timers.delete(idle);
|
|
142
|
+
}
|
|
143
|
+
idle = timer(o, () => paintThen(o), RENDER_IDLE_MILLIS);
|
|
144
|
+
};
|
|
145
|
+
if (env.MutationObserver !== undefined && env.root !== undefined) {
|
|
146
|
+
o.observer = new env.MutationObserver(arm);
|
|
147
|
+
o.observer.observe(env.root, MUTATIONS);
|
|
148
|
+
}
|
|
149
|
+
arm();
|
|
150
|
+
};
|
|
151
|
+
const start = (view, cause, atSetup, onScreen = false) => {
|
|
152
|
+
const o = {
|
|
153
|
+
view,
|
|
154
|
+
...(cause === undefined ? {} : { cause }),
|
|
155
|
+
rendered: false,
|
|
156
|
+
timers: new Set(),
|
|
157
|
+
observer: undefined,
|
|
158
|
+
offLoad: undefined,
|
|
159
|
+
};
|
|
160
|
+
open.set(view.id, o);
|
|
161
|
+
if (waitsForAppLoad(o))
|
|
162
|
+
poll(o);
|
|
163
|
+
else
|
|
164
|
+
create(o);
|
|
165
|
+
const capAt = view.startTime + VIEW_SPAN_CAP_MILLIS;
|
|
166
|
+
timer(o, () => finish(o, Math.min(rt.now(), capAt)), Math.max(0, capAt - rt.now()));
|
|
167
|
+
// A restored page, or a view a session change started, is on screen already.
|
|
168
|
+
if (onScreen || view.kind === "bfcache_restore")
|
|
169
|
+
paintThen(o);
|
|
170
|
+
else if (!history)
|
|
171
|
+
return;
|
|
172
|
+
else if (view.kind === "soft")
|
|
173
|
+
detectRender(o);
|
|
174
|
+
else if (atSetup) {
|
|
175
|
+
const loadEnd = env.navigationLoadEnd();
|
|
176
|
+
if (loadEnd > 0)
|
|
177
|
+
finish(o, Math.max(view.startTime, loadEnd));
|
|
178
|
+
else
|
|
179
|
+
o.offLoad = env.whenLoaded(() => paintThen(o));
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const offViews = rt.views.subscribe((e) => {
|
|
183
|
+
if (stopped)
|
|
184
|
+
return;
|
|
185
|
+
const o = open.get(e.view.id);
|
|
186
|
+
if (e.type === "start")
|
|
187
|
+
start(e.view, e.cause, false, e.rendered === true);
|
|
188
|
+
else if (o === undefined)
|
|
189
|
+
return;
|
|
190
|
+
else if (e.type === "rename") {
|
|
191
|
+
o.view = e.view;
|
|
192
|
+
o.span?.updateName(`view ${e.view.name}`);
|
|
193
|
+
o.span?.setAttribute(ATTR_RUM_VIEW_NAME, e.view.name);
|
|
194
|
+
}
|
|
195
|
+
else if (e.type === "rendered") {
|
|
196
|
+
// A render reported before the view started (before consent) means the view was on
|
|
197
|
+
// screen when it started; one reported during it ended getting into it.
|
|
198
|
+
if (e.at !== undefined && e.at >= o.view.startTime && !o.rendered && o.endAt === undefined) {
|
|
199
|
+
o.rendered = true;
|
|
200
|
+
finish(o, Math.min(e.at, rt.now()));
|
|
201
|
+
}
|
|
202
|
+
else
|
|
203
|
+
paintThen(o);
|
|
204
|
+
}
|
|
205
|
+
else if (e.type === "end")
|
|
206
|
+
finish(o, rt.now());
|
|
207
|
+
});
|
|
208
|
+
const current = rt.views.current();
|
|
209
|
+
if (current !== undefined)
|
|
210
|
+
start(current, undefined, true);
|
|
211
|
+
const unwatch = history && env.history !== undefined
|
|
212
|
+
? watchHistory(env.history, (url) => {
|
|
213
|
+
if (stopped)
|
|
214
|
+
return;
|
|
215
|
+
const action = rt.activeAction();
|
|
216
|
+
rt.views.start({
|
|
217
|
+
kind: "soft",
|
|
218
|
+
name: rt.settings().viewName(url),
|
|
219
|
+
...(action?.isRecording() === true ? { cause: action.spanContext() } : {}),
|
|
220
|
+
});
|
|
221
|
+
})
|
|
222
|
+
: undefined;
|
|
223
|
+
return () => {
|
|
224
|
+
stopped = true;
|
|
225
|
+
offViews();
|
|
226
|
+
unwatch?.();
|
|
227
|
+
for (const o of [...open.values()])
|
|
228
|
+
close(o);
|
|
229
|
+
};
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function browserViewsEnv() {
|
|
234
|
+
const g = globalThis;
|
|
235
|
+
const doc = g.document;
|
|
236
|
+
const loadEnd = () => {
|
|
237
|
+
const nav = performance.getEntriesByType?.("navigation")[0];
|
|
238
|
+
return nav?.loadEventEnd ?? 0;
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
timeOrigin: performance.timeOrigin,
|
|
242
|
+
paint: {
|
|
243
|
+
...(typeof g.requestAnimationFrame === "function"
|
|
244
|
+
? { requestAnimationFrame: (cb) => g.requestAnimationFrame(cb) }
|
|
245
|
+
: {}),
|
|
246
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
247
|
+
hidden: () => doc?.visibilityState === "hidden",
|
|
248
|
+
},
|
|
249
|
+
...(g.history === undefined || g.location === undefined || typeof g.addEventListener !== "function"
|
|
250
|
+
? {}
|
|
251
|
+
: {
|
|
252
|
+
history: {
|
|
253
|
+
history: g.history,
|
|
254
|
+
location: g.location,
|
|
255
|
+
addEventListener: (t, l) => g.addEventListener(t, l),
|
|
256
|
+
removeEventListener: (t, l) => g.removeEventListener(t, l),
|
|
257
|
+
},
|
|
258
|
+
}),
|
|
259
|
+
...(typeof g.MutationObserver === "function" ? { MutationObserver: g.MutationObserver } : {}),
|
|
260
|
+
...(doc?.documentElement == null ? {} : { root: doc.documentElement }),
|
|
261
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
262
|
+
clearTimeout: (h) => clearTimeout(h),
|
|
263
|
+
navigationLoadEnd: loadEnd,
|
|
264
|
+
whenLoaded(cb) {
|
|
265
|
+
let timer;
|
|
266
|
+
// loadEventEnd is set once the load handlers ran, so wait a task after the event.
|
|
267
|
+
const onLoad = () => {
|
|
268
|
+
timer = setTimeout(cb, 0);
|
|
269
|
+
};
|
|
270
|
+
if (doc?.readyState === "complete")
|
|
271
|
+
onLoad();
|
|
272
|
+
else
|
|
273
|
+
g.addEventListener?.("load", onLoad, { once: true });
|
|
274
|
+
return () => {
|
|
275
|
+
g.removeEventListener?.("load", onLoad);
|
|
276
|
+
if (timer !== undefined)
|
|
277
|
+
clearTimeout(timer);
|
|
278
|
+
};
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/** The views collector: view spans for every view, and soft navigations in history mode. */
|
|
283
|
+
export function views(opts = {}) {
|
|
284
|
+
return { name: "views", start: (rt) => viewsWith(opts, browserViewsEnv()).start(rt) };
|
|
285
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft navigation detection without a router adapter: pushState, replaceState and popstate, by
|
|
3
|
+
* path. A change of only the query or the fragment is not a navigation.
|
|
4
|
+
*/
|
|
5
|
+
export interface HistoryHost {
|
|
6
|
+
readonly history: Pick<History, "pushState" | "replaceState">;
|
|
7
|
+
readonly location: {
|
|
8
|
+
readonly href: string;
|
|
9
|
+
};
|
|
10
|
+
addEventListener(type: "popstate", l: () => void): void;
|
|
11
|
+
removeEventListener(type: "popstate", l: () => void): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Calls onChange with the new URL whenever the path changes. Returns the uninstall, which restores
|
|
15
|
+
* the original methods only when nothing patched over RUM's; otherwise RUM's wrapper stays in the
|
|
16
|
+
* chain and reports nothing.
|
|
17
|
+
*/
|
|
18
|
+
export declare function watchHistory(host: HistoryHost, onChange: (url: URL) => void): () => void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft navigation detection without a router adapter: pushState, replaceState and popstate, by
|
|
3
|
+
* path. A change of only the query or the fragment is not a navigation.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Calls onChange with the new URL whenever the path changes. Returns the uninstall, which restores
|
|
7
|
+
* the original methods only when nothing patched over RUM's; otherwise RUM's wrapper stays in the
|
|
8
|
+
* chain and reports nothing.
|
|
9
|
+
*/
|
|
10
|
+
export function watchHistory(host, onChange) {
|
|
11
|
+
let installed = true;
|
|
12
|
+
let path = new URL(host.location.href).pathname;
|
|
13
|
+
const check = () => {
|
|
14
|
+
if (!installed)
|
|
15
|
+
return;
|
|
16
|
+
const url = new URL(host.location.href);
|
|
17
|
+
if (url.pathname === path)
|
|
18
|
+
return;
|
|
19
|
+
path = url.pathname;
|
|
20
|
+
onChange(url);
|
|
21
|
+
};
|
|
22
|
+
const h = host.history;
|
|
23
|
+
const wrap = (original) => function (...args) {
|
|
24
|
+
original.apply(this, args);
|
|
25
|
+
check();
|
|
26
|
+
};
|
|
27
|
+
const push = h.pushState;
|
|
28
|
+
const replace = h.replaceState;
|
|
29
|
+
const pushWrapper = wrap(push);
|
|
30
|
+
const replaceWrapper = wrap(replace);
|
|
31
|
+
h.pushState = pushWrapper;
|
|
32
|
+
h.replaceState = replaceWrapper;
|
|
33
|
+
host.addEventListener("popstate", check);
|
|
34
|
+
return () => {
|
|
35
|
+
installed = false;
|
|
36
|
+
if (h.pushState === pushWrapper)
|
|
37
|
+
h.pushState = push;
|
|
38
|
+
if (h.replaceState === replaceWrapper)
|
|
39
|
+
h.replaceState = replace;
|
|
40
|
+
host.removeEventListener("popstate", check);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view name when no router adapter names the view (decision P17): the path, with every
|
|
3
|
+
* segment that holds a digit or is longer than 24 characters replaced by $id. Query and fragment
|
|
4
|
+
* are dropped, so a raw URL never becomes a view name.
|
|
5
|
+
*/
|
|
6
|
+
export declare function defaultViewName(url: URL): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view name when no router adapter names the view (decision P17): the path, with every
|
|
3
|
+
* segment that holds a digit or is longer than 24 characters replaced by $id. Query and fragment
|
|
4
|
+
* are dropped, so a raw URL never becomes a view name.
|
|
5
|
+
*/
|
|
6
|
+
export function defaultViewName(url) {
|
|
7
|
+
const path = url.pathname
|
|
8
|
+
.split("/")
|
|
9
|
+
.map((s) => (s !== "" && (/\d/.test(s) || s.length > 24) ? "$id" : s))
|
|
10
|
+
.join("/");
|
|
11
|
+
return path === "" ? "/" : path;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The first paint after now, for view spans that end when the routed view is on screen. */
|
|
2
|
+
export interface PaintHost {
|
|
3
|
+
requestAnimationFrame?(cb: () => void): number;
|
|
4
|
+
setTimeout(cb: () => void, ms: number): unknown;
|
|
5
|
+
readonly hidden: () => boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Calls cb with now() at the first paint after now: rendering follows the next animation frame,
|
|
9
|
+
* so a task queued in that frame runs after the paint. A hidden page renders no frames, so it
|
|
10
|
+
* waits for a task only.
|
|
11
|
+
*/
|
|
12
|
+
export declare function afterNextPaint(host: PaintHost, now: () => number, cb: (at: number) => void): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The first paint after now, for view spans that end when the routed view is on screen. */
|
|
2
|
+
/**
|
|
3
|
+
* Calls cb with now() at the first paint after now: rendering follows the next animation frame,
|
|
4
|
+
* so a task queued in that frame runs after the paint. A hidden page renders no frames, so it
|
|
5
|
+
* waits for a task only.
|
|
6
|
+
*/
|
|
7
|
+
export function afterNextPaint(host, now, cb) {
|
|
8
|
+
const task = () => {
|
|
9
|
+
host.setTimeout(() => cb(now()), 0);
|
|
10
|
+
};
|
|
11
|
+
if (host.hidden() || host.requestAnimationFrame === undefined)
|
|
12
|
+
task();
|
|
13
|
+
else
|
|
14
|
+
host.requestAnimationFrame(task);
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view tracker: the initial (or prerender) view at begin, a bfcache_restore view on a
|
|
3
|
+
* persisted pageshow, a soft view of the same name when the session changes (decision P14), and
|
|
4
|
+
* the soft views views() and the router adapters start. Every view opens in the summary
|
|
5
|
+
* accumulator and closes when it ends. views() records the view spans and attaches their contexts.
|
|
6
|
+
*/
|
|
7
|
+
import type { SessionManager } from "../contracts/session.ts";
|
|
8
|
+
import type { SummaryAccumulator } from "../contracts/summary.ts";
|
|
9
|
+
import type { ViewTracker } from "../contracts/views.ts";
|
|
10
|
+
export interface ViewTrackerOptions {
|
|
11
|
+
readonly session: Pick<SessionManager, "current" | "subscribe">;
|
|
12
|
+
readonly summary: SummaryAccumulator;
|
|
13
|
+
readonly viewName: (url: URL) => string;
|
|
14
|
+
/** The page URL now. */
|
|
15
|
+
readonly location: () => string;
|
|
16
|
+
/** performance.now(). */
|
|
17
|
+
readonly now: () => number;
|
|
18
|
+
/** document.prerendering, or a navigation entry with activationStart > 0. */
|
|
19
|
+
readonly prerendered: () => boolean;
|
|
20
|
+
/** Where pageshow fires, for back/forward-cache restores. */
|
|
21
|
+
readonly window?: EventTarget;
|
|
22
|
+
/** When the initial view starts, performance.now(). Default 0, navigation start; a content script's view starts when RUM begins. */
|
|
23
|
+
readonly initialStart?: () => number;
|
|
24
|
+
readonly randomId?: () => string;
|
|
25
|
+
}
|
|
26
|
+
export declare function createViewTracker(opts: ViewTrackerOptions): ViewTracker & {
|
|
27
|
+
close(): void;
|
|
28
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view tracker: the initial (or prerender) view at begin, a bfcache_restore view on a
|
|
3
|
+
* persisted pageshow, a soft view of the same name when the session changes (decision P14), and
|
|
4
|
+
* the soft views views() and the router adapters start. Every view opens in the summary
|
|
5
|
+
* accumulator and closes when it ends. views() records the view spans and attaches their contexts.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUuid } from "../ids.js";
|
|
8
|
+
const LOAD_KINDS = new Set(["initial", "prerender", "bfcache_restore"]);
|
|
9
|
+
export function createViewTracker(opts) {
|
|
10
|
+
const randomId = opts.randomId ?? randomUuid;
|
|
11
|
+
const listeners = new Set();
|
|
12
|
+
let current;
|
|
13
|
+
let load;
|
|
14
|
+
let appLoad;
|
|
15
|
+
const emit = (e) => {
|
|
16
|
+
for (const l of listeners)
|
|
17
|
+
l(e);
|
|
18
|
+
};
|
|
19
|
+
const endCurrent = () => {
|
|
20
|
+
const ended = current;
|
|
21
|
+
if (ended === undefined)
|
|
22
|
+
return undefined;
|
|
23
|
+
current = undefined;
|
|
24
|
+
opts.summary.close(ended.id);
|
|
25
|
+
emit({ type: "end", view: ended });
|
|
26
|
+
return ended;
|
|
27
|
+
};
|
|
28
|
+
const start = (o) => {
|
|
29
|
+
const previous = endCurrent();
|
|
30
|
+
const session = opts.session.current();
|
|
31
|
+
const view = {
|
|
32
|
+
id: randomId(),
|
|
33
|
+
name: o.name ?? opts.viewName(new URL(opts.location())),
|
|
34
|
+
kind: o.kind,
|
|
35
|
+
startTime: o.startTime ?? opts.now(),
|
|
36
|
+
sessionId: session?.id ?? "",
|
|
37
|
+
};
|
|
38
|
+
if (session === undefined)
|
|
39
|
+
return view; // no consent: nothing is tracked
|
|
40
|
+
current = view;
|
|
41
|
+
if (LOAD_KINDS.has(view.kind))
|
|
42
|
+
load = view;
|
|
43
|
+
opts.summary.open(view);
|
|
44
|
+
emit({
|
|
45
|
+
type: "start",
|
|
46
|
+
view,
|
|
47
|
+
...(previous === undefined ? {} : { previous }),
|
|
48
|
+
...(o.cause === undefined ? {} : { cause: o.cause }),
|
|
49
|
+
...(o.rendered === true ? { rendered: true } : {}),
|
|
50
|
+
});
|
|
51
|
+
return view;
|
|
52
|
+
};
|
|
53
|
+
const onPageShow = (e) => {
|
|
54
|
+
if (e.persisted === true && current !== undefined) {
|
|
55
|
+
start({ kind: "bfcache_restore", name: current.name });
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
opts.window?.addEventListener("pageshow", onPageShow);
|
|
59
|
+
const unsubscribe = opts.session.subscribe((e) => {
|
|
60
|
+
if (e.type !== "start" || e.previous === undefined || current === undefined)
|
|
61
|
+
return;
|
|
62
|
+
if (current.sessionId !== e.session.id)
|
|
63
|
+
start({ kind: "soft", name: current.name, rendered: true });
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
current: () => current,
|
|
67
|
+
loadView: () => load,
|
|
68
|
+
start,
|
|
69
|
+
rename(name) {
|
|
70
|
+
if (current === undefined)
|
|
71
|
+
return;
|
|
72
|
+
current = { ...current, name };
|
|
73
|
+
if (load?.id === current.id)
|
|
74
|
+
load = current;
|
|
75
|
+
opts.summary.rename(current.id, name);
|
|
76
|
+
emit({ type: "rename", view: current });
|
|
77
|
+
},
|
|
78
|
+
rendered(at) {
|
|
79
|
+
if (current !== undefined)
|
|
80
|
+
emit({ type: "rendered", view: current, ...(at === undefined ? {} : { at }) });
|
|
81
|
+
},
|
|
82
|
+
appLoadStarted(ctx) {
|
|
83
|
+
appLoad = ctx;
|
|
84
|
+
},
|
|
85
|
+
appLoad: () => appLoad,
|
|
86
|
+
attachSpan(viewId, ctx) {
|
|
87
|
+
if (current?.id === viewId)
|
|
88
|
+
current = { ...current, spanContext: ctx };
|
|
89
|
+
if (load?.id === viewId)
|
|
90
|
+
load = { ...load, spanContext: ctx };
|
|
91
|
+
},
|
|
92
|
+
begin() {
|
|
93
|
+
if (current !== undefined)
|
|
94
|
+
return;
|
|
95
|
+
// The initial view starts at time origin: a view covers getting into it from navigation start.
|
|
96
|
+
// A later begin (consent granted again) is a new session on a page already loaded, so its
|
|
97
|
+
// view is soft and starts now (decision P14).
|
|
98
|
+
if (load === undefined) {
|
|
99
|
+
start({ kind: opts.prerendered() ? "prerender" : "initial", startTime: opts.initialStart?.() ?? 0 });
|
|
100
|
+
}
|
|
101
|
+
else
|
|
102
|
+
start({ kind: "soft", name: load.name });
|
|
103
|
+
},
|
|
104
|
+
end() {
|
|
105
|
+
endCurrent();
|
|
106
|
+
},
|
|
107
|
+
subscribe(listener) {
|
|
108
|
+
listeners.add(listener);
|
|
109
|
+
return () => listeners.delete(listener);
|
|
110
|
+
},
|
|
111
|
+
close() {
|
|
112
|
+
opts.window?.removeEventListener("pageshow", onPageShow);
|
|
113
|
+
unsubscribe();
|
|
114
|
+
listeners.clear();
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -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;
|