@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,29 @@
|
|
|
1
|
+
import type { RumCollector } from "../contracts/runtime.ts";
|
|
2
|
+
import { type MutationObserverCtor } from "../views/collector.ts";
|
|
3
|
+
import { type CssEscape } from "./naming.ts";
|
|
4
|
+
export interface ActionsOptions {
|
|
5
|
+
/** How long the page must be idle before an action settles. Default 100. */
|
|
6
|
+
readonly idleMillis?: number;
|
|
7
|
+
/** An action ends this long after it started at the latest. Default 10000. */
|
|
8
|
+
readonly capMillis?: number;
|
|
9
|
+
/** Detect rage, dead and error clicks. Default true. */
|
|
10
|
+
readonly frustration?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** What actions() needs from the page. Internal: tests inject a fake. */
|
|
13
|
+
export interface ActionsEnv {
|
|
14
|
+
/** performance.timeOrigin. */
|
|
15
|
+
readonly timeOrigin: number;
|
|
16
|
+
readonly window: EventTarget;
|
|
17
|
+
setTimeout(cb: () => void, ms: number): unknown;
|
|
18
|
+
clearTimeout(handle: unknown): void;
|
|
19
|
+
readonly MutationObserver?: MutationObserverCtor;
|
|
20
|
+
readonly root?: object;
|
|
21
|
+
readonly cssEscape?: CssEscape;
|
|
22
|
+
/** True while the page has a text selection that is not collapsed. */
|
|
23
|
+
selecting(): boolean;
|
|
24
|
+
}
|
|
25
|
+
/** Events of one gesture (a click and the submit it causes) come this close together. */
|
|
26
|
+
export declare const GESTURE_MILLIS = 100;
|
|
27
|
+
export declare function actionsWith(opts: ActionsOptions, env: ActionsEnv): RumCollector;
|
|
28
|
+
/** The actions collector: clicks, key presses and submits as action spans, and frustration signals. */
|
|
29
|
+
export declare function actions(opts?: ActionsOptions): RumCollector;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* actions(): user actions and frustration signals.
|
|
3
|
+
*
|
|
4
|
+
* Listens on window in the capture phase. A click is a click action on its actionTarget. Enter or
|
|
5
|
+
* Space on a control that is not a text field (an input that takes text, a textarea or
|
|
6
|
+
* contenteditable) is a key_press action; the key itself is never recorded. A submit is a submit
|
|
7
|
+
* action on its form, except when it belongs to the gesture of the click or key press just before
|
|
8
|
+
* it (within GESTURE_MILLIS), and a keyboard click (detail 0) that follows a key press is that
|
|
9
|
+
* key press's gesture too.
|
|
10
|
+
*
|
|
11
|
+
* Each new action ends the open one at its last activity, then starts the span "action <type>" as
|
|
12
|
+
* a root at the event's time, with o11y.rum.span.shape action, the type, the name (actionName,
|
|
13
|
+
* when there is one), o11y.rum.element.selector and the view ID, linked to the view span. It is
|
|
14
|
+
* the open action until it settles: the RUM context manager makes it the parent of
|
|
15
|
+
* requests started meanwhile, and the requests whose parent it is, DOM mutations and view starts
|
|
16
|
+
* are its activity. Its breadcrumb (type and name, else the tag) is published when it starts.
|
|
17
|
+
*
|
|
18
|
+
* Frustration (when frustration is not false): clicks feed rage and dead detection, and
|
|
19
|
+
* errors feed error clicks. A click is not dead-eligible on a form field, label, contenteditable,
|
|
20
|
+
* a link with an href, or inside a text selection. Requests, mutations and view starts within the
|
|
21
|
+
* dead window count as activity, so the mutation observer stays connected while an action is open
|
|
22
|
+
* or a dead window is. Each signal counts in the summary, is published, retains the session and is
|
|
23
|
+
* emitted as o11y.rum.frustration in the open action's context. A runtime scope's action filter
|
|
24
|
+
* (a content script's UI root) drops every other event before any of this. Stop removes every listener, ends
|
|
25
|
+
* the open action and cancels every timer.
|
|
26
|
+
*/
|
|
27
|
+
import { ATTR_RUM_ELEMENT_SELECTOR, ATTR_RUM_FRUSTRATION_TYPE, EVENT_RUM_FRUSTRATION, } from "@libo11y/spec";
|
|
28
|
+
import { context, ROOT_CONTEXT, trace } from "@opentelemetry/api";
|
|
29
|
+
import { createFrustrationDetector } from "../frustration/detect.js";
|
|
30
|
+
import { MUTATIONS } from "../views/collector.js";
|
|
31
|
+
import { actionName, actionTarget, elementSelector } from "./naming.js";
|
|
32
|
+
import { actionSpanName, actionSpanStart } from "./record.js";
|
|
33
|
+
import { createSettleTracker } from "./settle.js";
|
|
34
|
+
/** Events of one gesture (a click and the submit it causes) come this close together. */
|
|
35
|
+
export const GESTURE_MILLIS = 100;
|
|
36
|
+
const FRUSTRATION_WINDOW_MILLIS = 1000;
|
|
37
|
+
const TEXT_INPUT_TYPES = new Set([
|
|
38
|
+
"",
|
|
39
|
+
"text",
|
|
40
|
+
"search",
|
|
41
|
+
"email",
|
|
42
|
+
"url",
|
|
43
|
+
"tel",
|
|
44
|
+
"password",
|
|
45
|
+
"number",
|
|
46
|
+
"date",
|
|
47
|
+
"datetime-local",
|
|
48
|
+
"month",
|
|
49
|
+
"time",
|
|
50
|
+
"week",
|
|
51
|
+
]);
|
|
52
|
+
const NOT_DEAD = "input,select,textarea,label,[contenteditable]";
|
|
53
|
+
function isElement(v) {
|
|
54
|
+
return (typeof v === "object" &&
|
|
55
|
+
v !== null &&
|
|
56
|
+
typeof v.closest === "function" &&
|
|
57
|
+
typeof v.tagName === "string");
|
|
58
|
+
}
|
|
59
|
+
function isTextField(el) {
|
|
60
|
+
const tag = el.tagName;
|
|
61
|
+
if (tag === "TEXTAREA" || el.isContentEditable === true)
|
|
62
|
+
return true;
|
|
63
|
+
if (tag !== "INPUT")
|
|
64
|
+
return false;
|
|
65
|
+
return TEXT_INPUT_TYPES.has((el.getAttribute("type") ?? "").toLowerCase());
|
|
66
|
+
}
|
|
67
|
+
export function actionsWith(opts, env) {
|
|
68
|
+
const idleMillis = opts.idleMillis ?? 100;
|
|
69
|
+
const capMillis = opts.capMillis ?? 10_000;
|
|
70
|
+
return {
|
|
71
|
+
name: "actions",
|
|
72
|
+
start(rt) {
|
|
73
|
+
let current;
|
|
74
|
+
/** The span of the last click, so an error counts against it only while its own action is open. */
|
|
75
|
+
let lastClickSpan;
|
|
76
|
+
let observer;
|
|
77
|
+
/** The observer stays connected until then, or while an action is open. */
|
|
78
|
+
let watchUntil = 0;
|
|
79
|
+
let watchTimer;
|
|
80
|
+
const unwatch = () => {
|
|
81
|
+
if (current !== undefined || rt.now() < watchUntil)
|
|
82
|
+
return;
|
|
83
|
+
observer?.disconnect();
|
|
84
|
+
observer = undefined;
|
|
85
|
+
};
|
|
86
|
+
const watch = (until) => {
|
|
87
|
+
watchUntil = Math.max(watchUntil, until);
|
|
88
|
+
if (watchTimer !== undefined)
|
|
89
|
+
env.clearTimeout(watchTimer);
|
|
90
|
+
watchTimer = env.setTimeout(() => {
|
|
91
|
+
watchTimer = undefined;
|
|
92
|
+
unwatch();
|
|
93
|
+
}, Math.max(0, watchUntil - rt.now()));
|
|
94
|
+
if (observer !== undefined || env.MutationObserver === undefined || env.root === undefined)
|
|
95
|
+
return;
|
|
96
|
+
observer = new env.MutationObserver(() => rt.bus.publish({ type: "mutation", at: rt.now() }));
|
|
97
|
+
observer.observe(env.root, MUTATIONS);
|
|
98
|
+
};
|
|
99
|
+
const signal = (type, selector, at) => {
|
|
100
|
+
rt.summary.count(`frustrations.${type}`);
|
|
101
|
+
rt.bus.publish({ type: "frustration", frustration: type, selector, at });
|
|
102
|
+
rt.session.retain("frustration");
|
|
103
|
+
const action = current?.span;
|
|
104
|
+
rt.emit(EVENT_RUM_FRUSTRATION, { [ATTR_RUM_FRUSTRATION_TYPE]: type, [ATTR_RUM_ELEMENT_SELECTOR]: selector }, action === undefined ? undefined : trace.setSpan(context.active(), action));
|
|
105
|
+
};
|
|
106
|
+
const detector = opts.frustration === false ? undefined : createFrustrationDetector({ timers: env, onSignal: signal });
|
|
107
|
+
const begin = (type, target, timeStamp) => {
|
|
108
|
+
current?.settle.end();
|
|
109
|
+
const now = rt.now();
|
|
110
|
+
// Some engines stamp events in epoch milliseconds; anything not on the performance clock is now.
|
|
111
|
+
const start = Number.isFinite(timeStamp) && timeStamp > 0 && timeStamp <= now ? timeStamp : now;
|
|
112
|
+
const el = type === "submit" ? target : actionTarget(target);
|
|
113
|
+
const name = actionName(el);
|
|
114
|
+
const selector = elementSelector(el, env.cssEscape);
|
|
115
|
+
const span = rt.tracer.startSpan(actionSpanName(type), {
|
|
116
|
+
startTime: env.timeOrigin + start,
|
|
117
|
+
...actionSpanStart(rt, type, name, { [ATTR_RUM_ELEMENT_SELECTOR]: selector }),
|
|
118
|
+
}, ROOT_CONTEXT);
|
|
119
|
+
const settle = createSettleTracker({
|
|
120
|
+
start,
|
|
121
|
+
idleMillis,
|
|
122
|
+
capMillis,
|
|
123
|
+
now: rt.now,
|
|
124
|
+
timers: env,
|
|
125
|
+
onSettle: (end) => {
|
|
126
|
+
span.end(env.timeOrigin + end);
|
|
127
|
+
if (current?.span === span)
|
|
128
|
+
current = undefined;
|
|
129
|
+
if (rt.activeAction() === span)
|
|
130
|
+
rt.setActiveAction(undefined);
|
|
131
|
+
unwatch();
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
current = { span, type, start, settle };
|
|
135
|
+
rt.setActiveAction(span);
|
|
136
|
+
rt.session.touch();
|
|
137
|
+
rt.bus.publish({
|
|
138
|
+
type: "breadcrumb",
|
|
139
|
+
kind: type,
|
|
140
|
+
label: name ?? el.tagName.toLowerCase(),
|
|
141
|
+
at: start,
|
|
142
|
+
});
|
|
143
|
+
watch(start + (detector === undefined ? 0 : FRUSTRATION_WINDOW_MILLIS));
|
|
144
|
+
if (type === "click" && detector !== undefined) {
|
|
145
|
+
const eligible = target.closest(NOT_DEAD) === null &&
|
|
146
|
+
target.isContentEditable !== true &&
|
|
147
|
+
target.closest("a")?.getAttribute("href") == null &&
|
|
148
|
+
!env.selecting();
|
|
149
|
+
lastClickSpan = span;
|
|
150
|
+
detector.click(selector, start, eligible);
|
|
151
|
+
}
|
|
152
|
+
return el;
|
|
153
|
+
};
|
|
154
|
+
// A click the bootstrap buffered before RUM loaded: an instant action at its time. Its
|
|
155
|
+
// activity is past, so it feeds rage detection only.
|
|
156
|
+
const replayClick = (target, at) => {
|
|
157
|
+
const el = actionTarget(target);
|
|
158
|
+
const name = actionName(el);
|
|
159
|
+
const selector = elementSelector(el, env.cssEscape);
|
|
160
|
+
const when = env.timeOrigin + at;
|
|
161
|
+
rt.tracer
|
|
162
|
+
.startSpan(actionSpanName("click"), {
|
|
163
|
+
startTime: when,
|
|
164
|
+
...actionSpanStart(rt, "click", name, { [ATTR_RUM_ELEMENT_SELECTOR]: selector }),
|
|
165
|
+
}, ROOT_CONTEXT)
|
|
166
|
+
.end(when);
|
|
167
|
+
rt.bus.publish({ type: "breadcrumb", kind: "click", label: name ?? el.tagName.toLowerCase(), at });
|
|
168
|
+
detector?.click(selector, at, false);
|
|
169
|
+
};
|
|
170
|
+
const sameGesture = (timeStamp) => current !== undefined && timeStamp - current.start < GESTURE_MILLIS;
|
|
171
|
+
// A scope (a content script) limits actions to the events it accepts, such as its own UI's.
|
|
172
|
+
const refused = (e) => rt.scope?.action?.(e) === false;
|
|
173
|
+
const onClick = (e) => {
|
|
174
|
+
if (!isElement(e.target) || refused(e))
|
|
175
|
+
return;
|
|
176
|
+
if (e.detail === 0 && current?.type === "key_press")
|
|
177
|
+
return;
|
|
178
|
+
begin("click", e.target, e.timeStamp);
|
|
179
|
+
};
|
|
180
|
+
const onKey = (e) => {
|
|
181
|
+
const k = e;
|
|
182
|
+
if (k.repeat === true || (k.key !== "Enter" && k.key !== " "))
|
|
183
|
+
return;
|
|
184
|
+
if (!isElement(e.target) || isTextField(e.target) || refused(e))
|
|
185
|
+
return;
|
|
186
|
+
begin("key_press", e.target, e.timeStamp);
|
|
187
|
+
};
|
|
188
|
+
const onSubmit = (e) => {
|
|
189
|
+
if (!isElement(e.target) || refused(e))
|
|
190
|
+
return;
|
|
191
|
+
if ((current?.type === "click" || current?.type === "key_press") && sameGesture(e.timeStamp))
|
|
192
|
+
return;
|
|
193
|
+
begin("submit", e.target, e.timeStamp);
|
|
194
|
+
};
|
|
195
|
+
env.window.addEventListener("click", onClick, { capture: true });
|
|
196
|
+
env.window.addEventListener("keydown", onKey, { capture: true });
|
|
197
|
+
env.window.addEventListener("submit", onSubmit, { capture: true });
|
|
198
|
+
const offBus = rt.bus.subscribe((s) => {
|
|
199
|
+
const a = current;
|
|
200
|
+
if (s.type === "request-start") {
|
|
201
|
+
if (a !== undefined && s.parentSpanId === a.span.spanContext().spanId) {
|
|
202
|
+
a.settle.requestStarted(s.spanId, s.at);
|
|
203
|
+
}
|
|
204
|
+
detector?.activity(s.at);
|
|
205
|
+
}
|
|
206
|
+
else if (s.type === "request-end")
|
|
207
|
+
a?.settle.requestEnded(s.spanId, s.at);
|
|
208
|
+
else if (s.type === "mutation") {
|
|
209
|
+
a?.settle.activity(s.at);
|
|
210
|
+
detector?.activity(s.at);
|
|
211
|
+
}
|
|
212
|
+
else if (s.type === "error")
|
|
213
|
+
detector?.error(s.at, a?.type === "click" && a.span === lastClickSpan);
|
|
214
|
+
else if (s.type === "bootstrap" && s.entry.kind === "click")
|
|
215
|
+
replayClick(s.entry.target, s.entry.at);
|
|
216
|
+
});
|
|
217
|
+
const offViews = rt.views.subscribe((e) => {
|
|
218
|
+
if (e.type !== "start")
|
|
219
|
+
return;
|
|
220
|
+
current?.settle.activity(rt.now());
|
|
221
|
+
detector?.activity(rt.now());
|
|
222
|
+
});
|
|
223
|
+
return () => {
|
|
224
|
+
env.window.removeEventListener("click", onClick, { capture: true });
|
|
225
|
+
env.window.removeEventListener("keydown", onKey, { capture: true });
|
|
226
|
+
env.window.removeEventListener("submit", onSubmit, { capture: true });
|
|
227
|
+
offBus();
|
|
228
|
+
offViews();
|
|
229
|
+
current?.settle.end();
|
|
230
|
+
current = undefined;
|
|
231
|
+
detector?.stop();
|
|
232
|
+
if (watchTimer !== undefined)
|
|
233
|
+
env.clearTimeout(watchTimer);
|
|
234
|
+
watchUntil = 0;
|
|
235
|
+
observer?.disconnect();
|
|
236
|
+
observer = undefined;
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function browserActionsEnv() {
|
|
242
|
+
const g = globalThis;
|
|
243
|
+
const doc = g.document;
|
|
244
|
+
return {
|
|
245
|
+
timeOrigin: performance.timeOrigin,
|
|
246
|
+
window: g,
|
|
247
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
248
|
+
clearTimeout: (h) => clearTimeout(h),
|
|
249
|
+
...(typeof g.MutationObserver === "function" ? { MutationObserver: g.MutationObserver } : {}),
|
|
250
|
+
...(doc?.documentElement == null ? {} : { root: doc.documentElement }),
|
|
251
|
+
selecting: () => {
|
|
252
|
+
const sel = doc?.getSelection?.();
|
|
253
|
+
return sel != null && !sel.isCollapsed;
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/** The actions collector: clicks, key presses and submits as action spans, and frustration signals. */
|
|
258
|
+
export function actions(opts = {}) {
|
|
259
|
+
return { name: "actions", start: (rt) => actionsWith(opts, browserActionsEnv()).start(rt) };
|
|
260
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action names and element selectors. A name is the data-o11y label on the
|
|
3
|
+
* element or its closest ancestor, else the aria-label of an interactive element, never visible
|
|
4
|
+
* text, and nothing inside [data-o11y-mask]. A selector identifies the element by structure: an ID
|
|
5
|
+
* that is not ID-like, a data-o11y label, or a short path of tags, stable classes and positions.
|
|
6
|
+
*/
|
|
7
|
+
/** What an action acts on: the closest of these to the event target. */
|
|
8
|
+
export declare const INTERACTIVE = "button,a,input,select,textarea,label,summary,[role=button],[role=link],[role=menuitem],[role=tab],[role=checkbox],[role=switch],[data-o11y]";
|
|
9
|
+
export type CssEscape = (s: string) => string;
|
|
10
|
+
/** The element a click acts on: the closest interactive element, else the target itself. */
|
|
11
|
+
export declare function actionTarget(el: Element): Element;
|
|
12
|
+
/** The action's name, at most 64 characters, or undefined when there is no privacy-safe one. */
|
|
13
|
+
export declare function actionName(el: Element): string | undefined;
|
|
14
|
+
/** A short CSS selector for el, at most 200 characters. */
|
|
15
|
+
export declare function elementSelector(el: Element, esc?: CssEscape): string;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action names and element selectors. A name is the data-o11y label on the
|
|
3
|
+
* element or its closest ancestor, else the aria-label of an interactive element, never visible
|
|
4
|
+
* text, and nothing inside [data-o11y-mask]. A selector identifies the element by structure: an ID
|
|
5
|
+
* that is not ID-like, a data-o11y label, or a short path of tags, stable classes and positions.
|
|
6
|
+
*/
|
|
7
|
+
/** What an action acts on: the closest of these to the event target. */
|
|
8
|
+
export const INTERACTIVE = "button,a,input,select,textarea,label,summary,[role=button],[role=link],[role=menuitem],[role=tab],[role=checkbox],[role=switch],[data-o11y]";
|
|
9
|
+
const MAX_NAME = 64;
|
|
10
|
+
const MAX_SELECTOR = 200;
|
|
11
|
+
const MAX_STEPS = 5;
|
|
12
|
+
/** An ID or class with a run of three digits, or any digit for classes, looks generated or user-specific. */
|
|
13
|
+
const ID_LIKE = /\d{3}/;
|
|
14
|
+
function defaultEscape() {
|
|
15
|
+
const css = globalThis.CSS;
|
|
16
|
+
return css?.escape === undefined ? (s) => s.replace(/["\\]/g, "\\$&") : (s) => css.escape?.(s) ?? s;
|
|
17
|
+
}
|
|
18
|
+
/** The element a click acts on: the closest interactive element, else the target itself. */
|
|
19
|
+
export function actionTarget(el) {
|
|
20
|
+
return el.closest(INTERACTIVE) ?? el;
|
|
21
|
+
}
|
|
22
|
+
/** The action's name, at most 64 characters, or undefined when there is no privacy-safe one. */
|
|
23
|
+
export function actionName(el) {
|
|
24
|
+
if (el.closest("[data-o11y-mask]") !== null)
|
|
25
|
+
return undefined;
|
|
26
|
+
const labelled = el.closest("[data-o11y]")?.getAttribute("data-o11y");
|
|
27
|
+
const name = labelled ?? (el.matches(INTERACTIVE) ? el.getAttribute("aria-label") : null);
|
|
28
|
+
const trimmed = name?.replace(/\s+/g, " ").trim();
|
|
29
|
+
return trimmed === undefined || trimmed === "" ? undefined : trimmed.slice(0, MAX_NAME);
|
|
30
|
+
}
|
|
31
|
+
function stableId(el) {
|
|
32
|
+
return el.id !== "" && !ID_LIKE.test(el.id);
|
|
33
|
+
}
|
|
34
|
+
function step(el, esc) {
|
|
35
|
+
const tag = el.tagName.toLowerCase();
|
|
36
|
+
const cls = [...el.classList]
|
|
37
|
+
.filter((c) => !/\d/.test(c))
|
|
38
|
+
.slice(0, 2)
|
|
39
|
+
.map((c) => `.${esc(c)}`)
|
|
40
|
+
.join("");
|
|
41
|
+
const parent = el.parentElement;
|
|
42
|
+
if (parent === null)
|
|
43
|
+
return tag + cls;
|
|
44
|
+
const same = [...parent.children].filter((c) => c.tagName === el.tagName);
|
|
45
|
+
return same.length > 1 ? `${tag}${cls}:nth-of-type(${same.indexOf(el) + 1})` : tag + cls;
|
|
46
|
+
}
|
|
47
|
+
/** A short CSS selector for el, at most 200 characters. */
|
|
48
|
+
export function elementSelector(el, esc = defaultEscape()) {
|
|
49
|
+
if (stableId(el))
|
|
50
|
+
return `#${esc(el.id)}`;
|
|
51
|
+
const label = el.getAttribute("data-o11y");
|
|
52
|
+
if (label !== null && el.closest("[data-o11y-mask]") === null)
|
|
53
|
+
return `[data-o11y="${esc(label)}"]`;
|
|
54
|
+
const steps = [];
|
|
55
|
+
for (let cur = el; cur !== null && steps.length < MAX_STEPS; cur = cur.parentElement) {
|
|
56
|
+
if (stableId(cur)) {
|
|
57
|
+
steps.unshift(`#${esc(cur.id)}`);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
steps.unshift(step(cur, esc));
|
|
61
|
+
if (cur.tagName === "BODY")
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return steps.join(" > ").slice(0, MAX_SELECTOR);
|
|
65
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Instant actions: custom actions (rum.addAction) and clicks replayed from the bootstrap. */
|
|
2
|
+
import { type RUMActionType } from "@libo11y/spec";
|
|
3
|
+
import { type Attributes, type Link } from "@opentelemetry/api";
|
|
4
|
+
import type { RumRuntime } from "../contracts/runtime.ts";
|
|
5
|
+
/** The span name of an action: the type only, because span names bypass the sensitivity policy. */
|
|
6
|
+
export declare function actionSpanName(type: RUMActionType): string;
|
|
7
|
+
/** The attributes and links every action span starts with. */
|
|
8
|
+
export declare function actionSpanStart(rt: RumRuntime, type: RUMActionType, name: string | undefined, extra?: Attributes): {
|
|
9
|
+
attributes: Attributes;
|
|
10
|
+
links: Link[];
|
|
11
|
+
};
|
|
12
|
+
/** Records a zero-length action span at at (performance.now(), default now) and its breadcrumb. */
|
|
13
|
+
export declare function recordInstantAction(rt: RumRuntime, type: RUMActionType, name: string | undefined, at?: number): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Instant actions: custom actions (rum.addAction) and clicks replayed from the bootstrap. */
|
|
2
|
+
import { ATTR_RUM_ACTION_NAME, ATTR_RUM_ACTION_TYPE, ATTR_RUM_SPAN_SHAPE, ATTR_RUM_VIEW_ID, RUM_SPAN_SHAPE_VALUE_ACTION, } from "@libo11y/spec";
|
|
3
|
+
import { ROOT_CONTEXT } from "@opentelemetry/api";
|
|
4
|
+
/** The span name of an action: the type only, because span names bypass the sensitivity policy. */
|
|
5
|
+
export function actionSpanName(type) {
|
|
6
|
+
return `action ${type}`;
|
|
7
|
+
}
|
|
8
|
+
/** The attributes and links every action span starts with. */
|
|
9
|
+
export function actionSpanStart(rt, type, name, extra = {}) {
|
|
10
|
+
const view = rt.views.current();
|
|
11
|
+
return {
|
|
12
|
+
attributes: {
|
|
13
|
+
[ATTR_RUM_SPAN_SHAPE]: RUM_SPAN_SHAPE_VALUE_ACTION,
|
|
14
|
+
[ATTR_RUM_ACTION_TYPE]: type,
|
|
15
|
+
...(name === undefined ? {} : { [ATTR_RUM_ACTION_NAME]: name }),
|
|
16
|
+
...(view === undefined ? {} : { [ATTR_RUM_VIEW_ID]: view.id }),
|
|
17
|
+
...extra,
|
|
18
|
+
},
|
|
19
|
+
links: view?.spanContext === undefined ? [] : [{ context: view.spanContext }],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Records a zero-length action span at at (performance.now(), default now) and its breadcrumb. */
|
|
23
|
+
export function recordInstantAction(rt, type, name, at = rt.now()) {
|
|
24
|
+
const when = rt.timeOrigin + at;
|
|
25
|
+
const span = rt.tracer.startSpan(actionSpanName(type), { startTime: when, ...actionSpanStart(rt, type, name) }, ROOT_CONTEXT);
|
|
26
|
+
span.end(when);
|
|
27
|
+
rt.bus.publish({ type: "breadcrumb", kind: type, label: name ?? type, at });
|
|
28
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The action settle rule. An action ends at the time of its last activity (a
|
|
3
|
+
* request end or a DOM mutation it caused, or its start), detected once no request is pending and
|
|
4
|
+
* idleMillis passed without activity, capped capMillis after it started. Its duration therefore
|
|
5
|
+
* excludes the idle wait. end() settles at once, as when a new action starts.
|
|
6
|
+
*/
|
|
7
|
+
export interface SettleTimers {
|
|
8
|
+
setTimeout(cb: () => void, ms: number): unknown;
|
|
9
|
+
clearTimeout(handle: unknown): void;
|
|
10
|
+
}
|
|
11
|
+
export interface SettleTracker {
|
|
12
|
+
requestStarted(id: string, at: number): void;
|
|
13
|
+
requestEnded(id: string, at: number): void;
|
|
14
|
+
activity(at: number): void;
|
|
15
|
+
/** Ends now, at the last activity: a new action started, or the collector stopped. */
|
|
16
|
+
end(): void;
|
|
17
|
+
}
|
|
18
|
+
export interface SettleOptions {
|
|
19
|
+
/** performance.now() when the action started. */
|
|
20
|
+
readonly start: number;
|
|
21
|
+
readonly idleMillis: number;
|
|
22
|
+
readonly capMillis: number;
|
|
23
|
+
readonly now: () => number;
|
|
24
|
+
readonly timers: SettleTimers;
|
|
25
|
+
/** Called once with the action's end time. */
|
|
26
|
+
readonly onSettle: (endTime: number) => void;
|
|
27
|
+
}
|
|
28
|
+
export declare function createSettleTracker(o: SettleOptions): SettleTracker;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The action settle rule. An action ends at the time of its last activity (a
|
|
3
|
+
* request end or a DOM mutation it caused, or its start), detected once no request is pending and
|
|
4
|
+
* idleMillis passed without activity, capped capMillis after it started. Its duration therefore
|
|
5
|
+
* excludes the idle wait. end() settles at once, as when a new action starts.
|
|
6
|
+
*/
|
|
7
|
+
export function createSettleTracker(o) {
|
|
8
|
+
const pending = new Set();
|
|
9
|
+
let last = o.start;
|
|
10
|
+
let done = false;
|
|
11
|
+
let idle;
|
|
12
|
+
let cap;
|
|
13
|
+
const finish = (end) => {
|
|
14
|
+
if (done)
|
|
15
|
+
return;
|
|
16
|
+
done = true;
|
|
17
|
+
o.timers.clearTimeout(idle);
|
|
18
|
+
o.timers.clearTimeout(cap);
|
|
19
|
+
o.onSettle(end);
|
|
20
|
+
};
|
|
21
|
+
const arm = () => {
|
|
22
|
+
o.timers.clearTimeout(idle);
|
|
23
|
+
idle = undefined;
|
|
24
|
+
if (pending.size > 0 || done)
|
|
25
|
+
return;
|
|
26
|
+
idle = o.timers.setTimeout(() => finish(last), Math.max(0, last + o.idleMillis - o.now()));
|
|
27
|
+
};
|
|
28
|
+
const capAt = o.start + o.capMillis;
|
|
29
|
+
cap = o.timers.setTimeout(() => finish(Math.min(o.now(), capAt)), Math.max(0, capAt - o.now()));
|
|
30
|
+
arm();
|
|
31
|
+
return {
|
|
32
|
+
requestStarted(id, at) {
|
|
33
|
+
if (done)
|
|
34
|
+
return;
|
|
35
|
+
pending.add(id);
|
|
36
|
+
last = Math.max(last, at);
|
|
37
|
+
arm();
|
|
38
|
+
},
|
|
39
|
+
requestEnded(id, at) {
|
|
40
|
+
if (done || !pending.delete(id))
|
|
41
|
+
return;
|
|
42
|
+
last = Math.max(last, at);
|
|
43
|
+
arm();
|
|
44
|
+
},
|
|
45
|
+
activity(at) {
|
|
46
|
+
if (done)
|
|
47
|
+
return;
|
|
48
|
+
last = Math.max(last, at);
|
|
49
|
+
arm();
|
|
50
|
+
},
|
|
51
|
+
end: () => finish(last),
|
|
52
|
+
};
|
|
53
|
+
}
|
package/dist/attrs.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Attribute keys RUM uses that the generator does not mint as constants, and the summary key builders. */
|
|
2
|
+
import type { SummaryCountKey, VitalName } from "./contracts/summary.ts";
|
|
3
|
+
/** semconv session.id. */
|
|
4
|
+
export declare const ATTR_SESSION_ID = "session.id";
|
|
5
|
+
/** semconv user.id, set by identify(). The app registry classifies it. */
|
|
6
|
+
export declare const ATTR_USER_ID = "user.id";
|
|
7
|
+
/** The summary attribute for a count, such as o11y.rum.summary.errors.uncaught. */
|
|
8
|
+
export declare function summaryCountKey(key: SummaryCountKey): string;
|
|
9
|
+
/** The summary attribute for a vital's value, such as o11y.rum.summary.lcp.value. */
|
|
10
|
+
export declare function summaryVitalKey(name: VitalName): string;
|
package/dist/attrs.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** semconv session.id. */
|
|
2
|
+
export const ATTR_SESSION_ID = "session.id";
|
|
3
|
+
/** semconv user.id, set by identify(). The app registry classifies it. */
|
|
4
|
+
export const ATTR_USER_ID = "user.id";
|
|
5
|
+
/** The summary attribute for a count, such as o11y.rum.summary.errors.uncaught. */
|
|
6
|
+
export function summaryCountKey(key) {
|
|
7
|
+
return `o11y.rum.summary.${key}`;
|
|
8
|
+
}
|
|
9
|
+
/** The summary attribute for a vital's value, such as o11y.rum.summary.lcp.value. */
|
|
10
|
+
export function summaryVitalKey(name) {
|
|
11
|
+
return `o11y.rum.summary.${name}.value`;
|
|
12
|
+
}
|
package/dist/bind.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web's platform binding and lifecycle. setupRumCore builds the RUM core, then hands
|
|
3
|
+
* a binding the setup options with RUM's span processor, context manager and forwarding span
|
|
4
|
+
* exporter in place, and the wrappers (contracts/binding.ts). webBinding runs web's setupWithDeps
|
|
5
|
+
* with them; @libo11y/rum/webext's contentBinding runs @libo11y/webext's setupContentScriptWith.
|
|
6
|
+
*/
|
|
7
|
+
import type { WebSetupOptions, WebTelemetry } from "@libo11y/web";
|
|
8
|
+
import { type SetupDeps } from "@libo11y/web/internal";
|
|
9
|
+
import type { RumBind, RumLifecycle } from "./contracts/binding.ts";
|
|
10
|
+
export type { RumWrappers } from "./contracts/binding.ts";
|
|
11
|
+
/** A binding of browser contexts: web's setup options and telemetry. */
|
|
12
|
+
export type WebRumBind = RumBind<WebSetupOptions, WebTelemetry>;
|
|
13
|
+
/** The web binding: setupWithDeps over deps with RUM's wrappers, installing globally when install is true. */
|
|
14
|
+
export declare function webBinding(deps: SetupDeps, install: boolean): WebRumBind;
|
|
15
|
+
/**
|
|
16
|
+
* A page's lifecycle: hide on visibilitychange to hidden and on pagehide (a navigation signals
|
|
17
|
+
* both, pagehide first), show on visibilitychange to visible, and restore on the pageshow of a page
|
|
18
|
+
* restored from the back/forward cache; the pageshow of a load that finished in a background tab
|
|
19
|
+
* is not one. Activity is pointerdown, keydown, scroll and touchstart on window, captured.
|
|
20
|
+
*/
|
|
21
|
+
export declare function webLifecycle(doc: (EventTarget & {
|
|
22
|
+
readonly visibilityState?: string;
|
|
23
|
+
}) | undefined, win: EventTarget | undefined): RumLifecycle;
|
package/dist/bind.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { browserEnv, setupWithDeps } from "@libo11y/web/internal";
|
|
2
|
+
/** The web binding: setupWithDeps over deps with RUM's wrappers, installing globally when install is true. */
|
|
3
|
+
export function webBinding(deps, install) {
|
|
4
|
+
return (options, w) => setupWithDeps(options, {
|
|
5
|
+
...deps,
|
|
6
|
+
createConsentController: (o) => w.consentController(deps.createConsentController(o)),
|
|
7
|
+
createHttpTransport: (o) => deps.createHttpTransport(w.transportOptions(o)),
|
|
8
|
+
onPlatform: (platform, po) => {
|
|
9
|
+
w.platform(po, browserEnv(options, po.endpoints));
|
|
10
|
+
deps.onPlatform?.(platform, po);
|
|
11
|
+
},
|
|
12
|
+
// Traces are injected, so web builds no span exporter here; logs are routed by session. The
|
|
13
|
+
// gateway accepts only delta sums and histograms, so app metrics export with delta temporality.
|
|
14
|
+
createExporterSet: (req, d, endpoints, eo) => w.exporterSet(deps.createExporterSet(req, d, endpoints, { ...eo, metricTemporality: "delta" })),
|
|
15
|
+
installFetchInstrumentation: (o) => deps.installFetchInstrumentation({ ...o, onStart: w.onRequestStart }),
|
|
16
|
+
installXhrInstrumentation: (o) => deps.installXhrInstrumentation({ ...o, onStart: w.onRequestStart }),
|
|
17
|
+
}, install);
|
|
18
|
+
}
|
|
19
|
+
/** User activity that keeps the session alive. */
|
|
20
|
+
const ACTIVITY = ["pointerdown", "keydown", "scroll", "touchstart"];
|
|
21
|
+
/**
|
|
22
|
+
* A page's lifecycle: hide on visibilitychange to hidden and on pagehide (a navigation signals
|
|
23
|
+
* both, pagehide first), show on visibilitychange to visible, and restore on the pageshow of a page
|
|
24
|
+
* restored from the back/forward cache; the pageshow of a load that finished in a background tab
|
|
25
|
+
* is not one. Activity is pointerdown, keydown, scroll and touchstart on window, captured.
|
|
26
|
+
*/
|
|
27
|
+
export function webLifecycle(doc, win) {
|
|
28
|
+
const hidden = () => doc?.visibilityState === "hidden";
|
|
29
|
+
return {
|
|
30
|
+
visible: () => !hidden(),
|
|
31
|
+
subscribe(h) {
|
|
32
|
+
const onVisibility = () => (hidden() ? h.hide() : h.show());
|
|
33
|
+
const onPageShow = (e) => {
|
|
34
|
+
if (e.persisted === true)
|
|
35
|
+
h.restore();
|
|
36
|
+
};
|
|
37
|
+
doc?.addEventListener("visibilitychange", onVisibility);
|
|
38
|
+
win?.addEventListener("pagehide", h.hide);
|
|
39
|
+
win?.addEventListener("pageshow", onPageShow);
|
|
40
|
+
return () => {
|
|
41
|
+
doc?.removeEventListener("visibilitychange", onVisibility);
|
|
42
|
+
win?.removeEventListener("pagehide", h.hide);
|
|
43
|
+
win?.removeEventListener("pageshow", onPageShow);
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
activity(listener) {
|
|
47
|
+
for (const a of ACTIVITY)
|
|
48
|
+
win?.addEventListener(a, listener, { capture: true, passive: true });
|
|
49
|
+
return () => {
|
|
50
|
+
for (const a of ACTIVITY)
|
|
51
|
+
win?.removeEventListener(a, listener, { capture: true });
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|