@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
package/dist/internal.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @libo11y/rum/internal: what the RUM adapter packages and the browser fixtures compose. For
|
|
3
|
+
* libo11y packages only. It follows the package version and carries no compatibility promise.
|
|
4
|
+
*/
|
|
5
|
+
export { actionName, actionTarget, elementSelector } from "./actions/naming.js";
|
|
6
|
+
export { ATTR_SESSION_ID, ATTR_USER_ID, summaryCountKey, summaryVitalKey } from "./attrs.js";
|
|
7
|
+
export { afterLoadOn, BOOTSTRAP_KEY, bootstrapBuffer, installRumBootstrapOn, takeBootstrap, } from "./bootstrap/buffer.js";
|
|
8
|
+
export { replayBootstrap } from "./bootstrap/replay.js";
|
|
9
|
+
export { createBus } from "./bus.js";
|
|
10
|
+
export { clampConfig, parseRemoteConfig, resolveLimits, } from "./config/clamp.js";
|
|
11
|
+
export { CONFIG_PINNED_STORAGE_KEY, CONFIG_STORAGE_KEY, createRemoteConfig, } from "./config/remote.js";
|
|
12
|
+
export { randomUuid } from "./ids.js";
|
|
13
|
+
export { stripQuery } from "./query.js";
|
|
14
|
+
export { createRetentionBuffer, RETENTION_QUEUE_DEFAULTS, } from "./retention/buffer.js";
|
|
15
|
+
export { retainingLogExporter, retainingSpanExporter, withRetention } from "./retention/exporters.js";
|
|
16
|
+
export { localStorageSessionStore, parseSessionState, SESSION_CHANNEL, SESSION_STORAGE_KEY, } from "./session/local-store.js";
|
|
17
|
+
export { createSessionManager } from "./session/manager.js";
|
|
18
|
+
export { fnv1a32, sessionSampled } from "./session/sampling.js";
|
|
19
|
+
export { resolveSettings } from "./settings.js";
|
|
20
|
+
export { defaultRumDeps, runtimeOf, setupRumWith } from "./setup.js";
|
|
21
|
+
export { APP_LOAD_SPAN, rumSpanProcessor } from "./span-processor.js";
|
|
22
|
+
export { createSummaryAccumulator } from "./summary/accumulator.js";
|
|
23
|
+
export { defaultViewName } from "./views/naming.js";
|
|
24
|
+
export { createViewTracker } from "./views/tracker.js";
|
|
25
|
+
export { gatewayTransportOptions, withWireParams } from "./wire.js";
|
package/dist/loaf.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* longAnimationFrames(): counts long-animation-frame entries into the current view's
|
|
3
|
+
* o11y.rum.summary.long_animation_frames. The first start observes buffered entries, so frames from
|
|
4
|
+
* before it count toward the load view; a restart (consent granted again) counts only new ones.
|
|
5
|
+
* Engines without the entry type (Firefox and WebKit today) record nothing. There is no long-task
|
|
6
|
+
* fallback, because the spec counts long animation frames and a long task is a different measure,
|
|
7
|
+
* so the gateway's o11y.rum.browser.long_animation_frames reflects Chromium-family browsers only.
|
|
8
|
+
*/
|
|
9
|
+
import type { RumCollector } from "./contracts/runtime.ts";
|
|
10
|
+
/** Observes long animation frames, calling cb with the number in each batch. Returns the function that stops. */
|
|
11
|
+
export type LongAnimationFrameSource = (cb: (count: number) => void, buffered: boolean) => () => void;
|
|
12
|
+
/** For tests: the collector over an injected source. */
|
|
13
|
+
export declare function longAnimationFramesWith(observe: LongAnimationFrameSource): RumCollector;
|
|
14
|
+
/** Counts long animation frames per view, on engines that report them. */
|
|
15
|
+
export declare function longAnimationFrames(): RumCollector;
|
package/dist/loaf.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** For tests: the collector over an injected source. */
|
|
2
|
+
export function longAnimationFramesWith(observe) {
|
|
3
|
+
let starts = 0;
|
|
4
|
+
return {
|
|
5
|
+
name: "long_animation_frames",
|
|
6
|
+
start(rt) {
|
|
7
|
+
starts++;
|
|
8
|
+
return observe((n) => {
|
|
9
|
+
if (n > 0 && rt.gate.recording())
|
|
10
|
+
rt.summary.count("long_animation_frames", n);
|
|
11
|
+
}, starts === 1);
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const browserSource = (cb, buffered) => {
|
|
16
|
+
if (typeof PerformanceObserver === "undefined" ||
|
|
17
|
+
!(PerformanceObserver.supportedEntryTypes ?? []).includes("long-animation-frame")) {
|
|
18
|
+
return () => { };
|
|
19
|
+
}
|
|
20
|
+
const observer = new PerformanceObserver((list) => cb(list.getEntries().length));
|
|
21
|
+
observer.observe({ type: "long-animation-frame", buffered });
|
|
22
|
+
return () => observer.disconnect();
|
|
23
|
+
};
|
|
24
|
+
/** Counts long animation frames per view, on engines that report them. */
|
|
25
|
+
export function longAnimationFrames() {
|
|
26
|
+
return longAnimationFramesWith(browserSource);
|
|
27
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** The public options and handle of setupRum. */
|
|
2
|
+
import type { HttpRequestInfo, WebSetupOptions, WebTelemetry } from "@libo11y/web";
|
|
3
|
+
import type { Span } from "@opentelemetry/api";
|
|
4
|
+
import type { RemoteConfig } from "./config/remote.ts";
|
|
5
|
+
import type { CollectorName, RequestStartHook, RumCollector } from "./contracts/runtime.ts";
|
|
6
|
+
/** RUM's own options, under rum in setupRum's options. Code sets every default and limit. */
|
|
7
|
+
export interface RumOptions {
|
|
8
|
+
/** The share of sessions whose detail is sent from the start, 0 to 1. Default 1. */
|
|
9
|
+
readonly sessionSampleRate?: number;
|
|
10
|
+
/** A session ends after this long without activity. Default 900000 (15 minutes). */
|
|
11
|
+
readonly sessionIdleMillis?: number;
|
|
12
|
+
/** A session ends this long after it started. Default 14400000 (4 hours). */
|
|
13
|
+
readonly sessionMaxMillis?: number;
|
|
14
|
+
/** Query parameters kept in recorded URLs. Every other parameter is removed. Default []. */
|
|
15
|
+
readonly queryAllowList?: readonly string[];
|
|
16
|
+
/** The collectors to run, such as [views(), actions(), errors(), vitals(), resources()]. Default []. */
|
|
17
|
+
readonly collectors?: readonly RumCollector[];
|
|
18
|
+
/** Names a view from its URL when no router adapter names it. Default: the path with ID-like segments as $id. */
|
|
19
|
+
readonly viewName?: (url: URL) => string;
|
|
20
|
+
/** Bounds of the retention buffer for sessions not yet kept. */
|
|
21
|
+
readonly retention?: {
|
|
22
|
+
readonly maxBytes?: number;
|
|
23
|
+
readonly maxAgeMillis?: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The app's public client token for the RUM gateway. It is not a secret: it identifies the app.
|
|
27
|
+
* Every OTLP request carries it as the client_token query parameter, next to sent_at, because
|
|
28
|
+
* sendBeacon cannot set headers.
|
|
29
|
+
*/
|
|
30
|
+
readonly clientToken?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Remote configuration from the RUM gateway, from remoteConfig(). Off unless given. It is
|
|
33
|
+
* fetched only once consent is granted.
|
|
34
|
+
*/
|
|
35
|
+
readonly remoteConfig?: RemoteConfig;
|
|
36
|
+
}
|
|
37
|
+
/** Options for setupRum: the setup options of `@libo11y/web` and RUM's own. */
|
|
38
|
+
export interface RumSetupOptions extends WebSetupOptions {
|
|
39
|
+
readonly rum?: RumOptions;
|
|
40
|
+
}
|
|
41
|
+
/** The application's handle on RUM. */
|
|
42
|
+
export interface Rum {
|
|
43
|
+
/** The current session's ID, or undefined while consent is not granted. */
|
|
44
|
+
sessionId(): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Sets user.id on every later span and collector event, or clears it with undefined. View
|
|
47
|
+
* summaries and session events do not carry it. Pass a pseudonymous ID that your application
|
|
48
|
+
* registry classifies internal. Throws O11yError INVALID_OPTION for a value that contains an at
|
|
49
|
+
* sign, because an email address is never an ID.
|
|
50
|
+
*/
|
|
51
|
+
identify(userId: string | undefined): void;
|
|
52
|
+
/** Adds a hook run on every fetch and XHR client span right after it starts. Returns its removal. */
|
|
53
|
+
onRequestStart(hook: RequestStartHook): () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Ends the current view and starts a soft view now, for router adapters. The name defaults to
|
|
56
|
+
* the view name of the current URL. An open action becomes the view's cause.
|
|
57
|
+
*/
|
|
58
|
+
startView(opts?: {
|
|
59
|
+
readonly name?: string;
|
|
60
|
+
}): void;
|
|
61
|
+
/** Names the current view, such as with its route template. Never pass a raw URL. */
|
|
62
|
+
setViewName(name: string): void;
|
|
63
|
+
/** The current view's routed content rendered: its view span ends at the next paint. */
|
|
64
|
+
viewRendered(): void;
|
|
65
|
+
/** Records a custom action, such as a programmatic save. name is a label, never user data. */
|
|
66
|
+
addAction(name: string): void;
|
|
67
|
+
/**
|
|
68
|
+
* Records an error the application caught. Use source boundary, with the component stack, for
|
|
69
|
+
* a UI framework's error boundary. Default source custom.
|
|
70
|
+
*/
|
|
71
|
+
addError(error: unknown, opts?: AddErrorOptions): void;
|
|
72
|
+
}
|
|
73
|
+
/** Options for Rum.addError. */
|
|
74
|
+
export interface AddErrorOptions {
|
|
75
|
+
/** Default custom. */
|
|
76
|
+
readonly source?: "custom" | "boundary";
|
|
77
|
+
/** The UI framework's component stack, appended to the stack trace. */
|
|
78
|
+
readonly componentStack?: string;
|
|
79
|
+
}
|
|
80
|
+
/** Browser telemetry with RUM. */
|
|
81
|
+
export interface RumTelemetry extends WebTelemetry {
|
|
82
|
+
readonly rum: Rum;
|
|
83
|
+
}
|
|
84
|
+
export type { CollectorName, HttpRequestInfo, RequestStartHook, RumCollector, Span };
|
package/dist/options.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spans that start between a grant and the session loading. A session store may answer in a
|
|
3
|
+
* later task, as chrome.storage.session does, while web's documentLoad span starts and ends as soon
|
|
4
|
+
* as consent is granted. The span processor holds the spans that start in that window here. Once
|
|
5
|
+
* the session is known, each held span gets session.id and its view's o11y.rum.view.id: at once
|
|
6
|
+
* while it is still recording, and on export once it has ended. An export whose batch holds a span
|
|
7
|
+
* still waiting waits for the session. A revoke closes the window, and the spans that waited go out
|
|
8
|
+
* with no session, so the retention router drops them.
|
|
9
|
+
*
|
|
10
|
+
* Ended spans get their attributes in RUM's forwarding span exporter, so an app that injects only
|
|
11
|
+
* its own span processor (and so exports spans itself) sees ended held spans without them.
|
|
12
|
+
*/
|
|
13
|
+
import type { Attributes } from "@opentelemetry/api";
|
|
14
|
+
import type { ReadableSpan, Span } from "@opentelemetry/sdk-trace";
|
|
15
|
+
export interface PendingSpans {
|
|
16
|
+
/** Opens the window: spans that start without a session are held until resolve or discard. */
|
|
17
|
+
open(): void;
|
|
18
|
+
/** The window is open. */
|
|
19
|
+
isOpen(): boolean;
|
|
20
|
+
/** Holds span. load is true for a span of the load view (app_load and its document request). */
|
|
21
|
+
hold(span: Span, load: boolean): void;
|
|
22
|
+
/** The session is known: gives every held span stamp(load), keeping attributes it already has, and closes the window. */
|
|
23
|
+
resolve(stamp: (load: boolean) => Attributes): void;
|
|
24
|
+
/** Closes the window without a session (revoke, shutdown). */
|
|
25
|
+
discard(): void;
|
|
26
|
+
/** spans with each held one stamped. Waits for resolve or discard while one of them is still held. */
|
|
27
|
+
prepare(spans: ReadableSpan[]): ReadableSpan[] | Promise<ReadableSpan[]>;
|
|
28
|
+
}
|
|
29
|
+
export declare function createPendingSpans(): PendingSpans;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** At most this many ended spans keep the attributes they are to get on export. */
|
|
2
|
+
const STAMPED_LIMIT = 512;
|
|
3
|
+
/** At most this many spans wait for one session; later ones go out without it. */
|
|
4
|
+
const HELD_LIMIT = 1024;
|
|
5
|
+
function stamped(span, extra) {
|
|
6
|
+
return {
|
|
7
|
+
name: span.name,
|
|
8
|
+
kind: span.kind,
|
|
9
|
+
spanContext: () => span.spanContext(),
|
|
10
|
+
startTime: span.startTime,
|
|
11
|
+
endTime: span.endTime,
|
|
12
|
+
status: span.status,
|
|
13
|
+
attributes: { ...extra, ...span.attributes },
|
|
14
|
+
links: span.links,
|
|
15
|
+
events: span.events,
|
|
16
|
+
duration: span.duration,
|
|
17
|
+
ended: span.ended,
|
|
18
|
+
resource: span.resource,
|
|
19
|
+
instrumentationScope: span.instrumentationScope,
|
|
20
|
+
droppedAttributesCount: span.droppedAttributesCount,
|
|
21
|
+
droppedEventsCount: span.droppedEventsCount,
|
|
22
|
+
droppedLinksCount: span.droppedLinksCount,
|
|
23
|
+
...(span.parentSpanContext === undefined ? {} : { parentSpanContext: span.parentSpanContext }),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function createPendingSpans() {
|
|
27
|
+
let opened = false;
|
|
28
|
+
const held = new Map();
|
|
29
|
+
/** Ended spans and the attributes they get on export, oldest first. */
|
|
30
|
+
const late = new Map();
|
|
31
|
+
let settle;
|
|
32
|
+
let settled;
|
|
33
|
+
const close = () => {
|
|
34
|
+
opened = false;
|
|
35
|
+
held.clear();
|
|
36
|
+
settle?.();
|
|
37
|
+
settle = undefined;
|
|
38
|
+
settled = undefined;
|
|
39
|
+
};
|
|
40
|
+
const patch = (spans) => {
|
|
41
|
+
if (late.size === 0)
|
|
42
|
+
return spans;
|
|
43
|
+
return spans.map((s) => {
|
|
44
|
+
const id = s.spanContext().spanId;
|
|
45
|
+
const extra = late.get(id);
|
|
46
|
+
if (extra === undefined)
|
|
47
|
+
return s;
|
|
48
|
+
late.delete(id);
|
|
49
|
+
return stamped(s, extra);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const prepare = (spans) => {
|
|
53
|
+
if (settled !== undefined && spans.some((s) => held.has(s.spanContext().spanId))) {
|
|
54
|
+
return settled.then(() => prepare(spans));
|
|
55
|
+
}
|
|
56
|
+
return patch(spans);
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
open() {
|
|
60
|
+
if (opened)
|
|
61
|
+
return;
|
|
62
|
+
opened = true;
|
|
63
|
+
settled = new Promise((r) => {
|
|
64
|
+
settle = r;
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
isOpen: () => opened,
|
|
68
|
+
hold(span, load) {
|
|
69
|
+
if (opened && held.size < HELD_LIMIT)
|
|
70
|
+
held.set(span.spanContext().spanId, { span, load });
|
|
71
|
+
},
|
|
72
|
+
resolve(stamp) {
|
|
73
|
+
for (const [id, { span, load }] of held) {
|
|
74
|
+
const attrs = stamp(load);
|
|
75
|
+
if (span.isRecording()) {
|
|
76
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
77
|
+
if (v !== undefined && span.attributes[k] === undefined)
|
|
78
|
+
span.setAttribute(k, v);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
late.set(id, attrs);
|
|
83
|
+
while (late.size > STAMPED_LIMIT)
|
|
84
|
+
late.delete(late.keys().next().value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
close();
|
|
88
|
+
},
|
|
89
|
+
discard: close,
|
|
90
|
+
prepare,
|
|
91
|
+
};
|
|
92
|
+
}
|
package/dist/query.d.ts
ADDED
package/dist/query.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** URL query policy: a query parameter is kept only when the app allows it. */
|
|
2
|
+
export function stripQuery(href, allow) {
|
|
3
|
+
let url;
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(href);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return href;
|
|
9
|
+
}
|
|
10
|
+
url.hash = "";
|
|
11
|
+
if (url.search !== "") {
|
|
12
|
+
const kept = new URLSearchParams();
|
|
13
|
+
for (const [k, v] of url.searchParams)
|
|
14
|
+
if (allow.has(k))
|
|
15
|
+
kept.append(k, v);
|
|
16
|
+
url.search = kept.toString();
|
|
17
|
+
}
|
|
18
|
+
return url.href;
|
|
19
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* resources(): one span per resource performance entry the page loaded.
|
|
3
|
+
*
|
|
4
|
+
* Rules. Entries are observed buffered, so the first start also records the resources loaded before
|
|
5
|
+
* it; a later start (consent granted again) records only resources that started after it. Skipped:
|
|
6
|
+
* fetch, xmlhttprequest and beacon initiators (their client spans exist), non-HTTP URLs, and URLs
|
|
7
|
+
* the runtime or env ignores (the OTLP endpoints and instrumentations.ignoreUrls). A span is named
|
|
8
|
+
* "resource <o11y.rum.resource.type>" (resourceType), is a client span, starts at timeOrigin +
|
|
9
|
+
* startTime and ends at timeOrigin + responseEnd. Its attributes: o11y.rum.resource.type, url.full
|
|
10
|
+
* with the query stripped by the allow-list, server.address, http.response.status_code when
|
|
11
|
+
* responseStatus > 0, http.response.body.size when encodedBodySize > 0, and the session.id and
|
|
12
|
+
* o11y.rum.view.id of its view. It carries no http.request.method, so it is not a request of the
|
|
13
|
+
* view and no action waits for it.
|
|
14
|
+
*
|
|
15
|
+
* A resource that started before loadEventEnd, or while the load is under way, belongs to the load
|
|
16
|
+
* view and is a child of app_load (web's documentLoad span). Such resources wait until app_load is
|
|
17
|
+
* known, polling every RESOURCE_POLL_MILLIS. They are recorded as roots once the load ended
|
|
18
|
+
* RESOURCE_APP_LOAD_WAIT_MILLIS ago, once RESOURCE_MAX_WAIT_MILLIS passed since the first of them
|
|
19
|
+
* waited, or when the page is hidden (runtime.onHide), so a page left before app_load still records
|
|
20
|
+
* its load resources. Any other resource belongs to
|
|
21
|
+
* the current view and is a root linked to that view's span when it is known. A Server-Timing
|
|
22
|
+
* traceparent adds a link to the server span; every other Server-Timing entry becomes a
|
|
23
|
+
* server_timing span event at the span's start with o11y.rum.server_timing.name and
|
|
24
|
+
* o11y.rum.server_timing.duration (seconds). At most maxPerView spans per view, waiting ones
|
|
25
|
+
* included. Stop drops resources still waiting and stops observing.
|
|
26
|
+
*/
|
|
27
|
+
import { type RUMResourceType } from "@libo11y/spec";
|
|
28
|
+
import type { RumCollector } from "../contracts/runtime.ts";
|
|
29
|
+
import { type ServerTimingEntryLike } from "./server-timing.ts";
|
|
30
|
+
export type { ServerTimingEntryLike } from "./server-timing.ts";
|
|
31
|
+
/** Options of resources(). */
|
|
32
|
+
export interface ResourcesOptions {
|
|
33
|
+
/** Most resource spans per view. Default 150. */
|
|
34
|
+
readonly maxPerView?: number;
|
|
35
|
+
}
|
|
36
|
+
/** The fields of a PerformanceResourceTiming entry RUM reads. Times are performance.now() milliseconds. */
|
|
37
|
+
export interface ResourceEntryLike {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly initiatorType: string;
|
|
40
|
+
readonly startTime: number;
|
|
41
|
+
readonly responseEnd: number;
|
|
42
|
+
readonly encodedBodySize?: number;
|
|
43
|
+
readonly responseStatus?: number;
|
|
44
|
+
readonly serverTiming?: readonly ServerTimingEntryLike[];
|
|
45
|
+
}
|
|
46
|
+
/** What resources() needs from the page. Internal: tests inject a fake. */
|
|
47
|
+
export interface ResourcesEnv {
|
|
48
|
+
/** performance.timeOrigin. */
|
|
49
|
+
readonly timeOrigin: number;
|
|
50
|
+
/** Observes resource entries, buffered ones first. Returns the function that stops observing. */
|
|
51
|
+
observe(cb: (entries: readonly ResourceEntryLike[]) => void): () => void;
|
|
52
|
+
/** The navigation entry's loadEventEnd, 0 until the load event ended. */
|
|
53
|
+
loadEventEnd(): number;
|
|
54
|
+
/** URLs to skip beyond the runtime's own. */
|
|
55
|
+
ignore?(url: string): boolean;
|
|
56
|
+
setTimeout(cb: () => void, ms: number): unknown;
|
|
57
|
+
clearTimeout(handle: unknown): void;
|
|
58
|
+
}
|
|
59
|
+
/** How often resources of the load poll for app_load. */
|
|
60
|
+
export declare const RESOURCE_POLL_MILLIS = 100;
|
|
61
|
+
/** How long after the load ended resources of the load wait for app_load before they become roots. */
|
|
62
|
+
export declare const RESOURCE_APP_LOAD_WAIT_MILLIS = 1000;
|
|
63
|
+
/** The longest a resource of the load waits for app_load, even when the load never ends. */
|
|
64
|
+
export declare const RESOURCE_MAX_WAIT_MILLIS = 10000;
|
|
65
|
+
/** The resource type of each initiator type; any other initiator is other. */
|
|
66
|
+
export declare const RESOURCE_TYPES: Readonly<Record<string, RUMResourceType>>;
|
|
67
|
+
/**
|
|
68
|
+
* The resource type of an entry: by initiator type, except that a css, link or other initiator with
|
|
69
|
+
* a font, image, script or stylesheet extension takes the extension's type (fonts and background
|
|
70
|
+
* images load with the css initiator).
|
|
71
|
+
*/
|
|
72
|
+
export declare function resourceType(entry: Pick<ResourceEntryLike, "name" | "initiatorType">): RUMResourceType;
|
|
73
|
+
/** For tests: the resources collector over an injected page. */
|
|
74
|
+
export declare function resourcesWith(opts: ResourcesOptions, env: ResourcesEnv): RumCollector;
|
|
75
|
+
/** The resources collector: a span for each script, stylesheet, image, font, media or frame the page loads. */
|
|
76
|
+
export declare function resources(opts?: ResourcesOptions): RumCollector;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* resources(): one span per resource performance entry the page loaded.
|
|
3
|
+
*
|
|
4
|
+
* Rules. Entries are observed buffered, so the first start also records the resources loaded before
|
|
5
|
+
* it; a later start (consent granted again) records only resources that started after it. Skipped:
|
|
6
|
+
* fetch, xmlhttprequest and beacon initiators (their client spans exist), non-HTTP URLs, and URLs
|
|
7
|
+
* the runtime or env ignores (the OTLP endpoints and instrumentations.ignoreUrls). A span is named
|
|
8
|
+
* "resource <o11y.rum.resource.type>" (resourceType), is a client span, starts at timeOrigin +
|
|
9
|
+
* startTime and ends at timeOrigin + responseEnd. Its attributes: o11y.rum.resource.type, url.full
|
|
10
|
+
* with the query stripped by the allow-list, server.address, http.response.status_code when
|
|
11
|
+
* responseStatus > 0, http.response.body.size when encodedBodySize > 0, and the session.id and
|
|
12
|
+
* o11y.rum.view.id of its view. It carries no http.request.method, so it is not a request of the
|
|
13
|
+
* view and no action waits for it.
|
|
14
|
+
*
|
|
15
|
+
* A resource that started before loadEventEnd, or while the load is under way, belongs to the load
|
|
16
|
+
* view and is a child of app_load (web's documentLoad span). Such resources wait until app_load is
|
|
17
|
+
* known, polling every RESOURCE_POLL_MILLIS. They are recorded as roots once the load ended
|
|
18
|
+
* RESOURCE_APP_LOAD_WAIT_MILLIS ago, once RESOURCE_MAX_WAIT_MILLIS passed since the first of them
|
|
19
|
+
* waited, or when the page is hidden (runtime.onHide), so a page left before app_load still records
|
|
20
|
+
* its load resources. Any other resource belongs to
|
|
21
|
+
* the current view and is a root linked to that view's span when it is known. A Server-Timing
|
|
22
|
+
* traceparent adds a link to the server span; every other Server-Timing entry becomes a
|
|
23
|
+
* server_timing span event at the span's start with o11y.rum.server_timing.name and
|
|
24
|
+
* o11y.rum.server_timing.duration (seconds). At most maxPerView spans per view, waiting ones
|
|
25
|
+
* included. Stop drops resources still waiting and stops observing.
|
|
26
|
+
*/
|
|
27
|
+
import { ATTR_RUM_RESOURCE_TYPE, ATTR_RUM_SERVER_TIMING_DURATION, ATTR_RUM_SERVER_TIMING_NAME, ATTR_RUM_VIEW_ID, } from "@libo11y/spec";
|
|
28
|
+
import { ROOT_CONTEXT, SpanKind, trace } from "@opentelemetry/api";
|
|
29
|
+
import { ATTR_SESSION_ID } from "../attrs.js";
|
|
30
|
+
import { stripQuery } from "../query.js";
|
|
31
|
+
import { readServerTiming } from "./server-timing.js";
|
|
32
|
+
const DEFAULT_MAX_PER_VIEW = 150;
|
|
33
|
+
/** How often resources of the load poll for app_load. */
|
|
34
|
+
export const RESOURCE_POLL_MILLIS = 100;
|
|
35
|
+
/** How long after the load ended resources of the load wait for app_load before they become roots. */
|
|
36
|
+
export const RESOURCE_APP_LOAD_WAIT_MILLIS = 1000;
|
|
37
|
+
/** The longest a resource of the load waits for app_load, even when the load never ends. */
|
|
38
|
+
export const RESOURCE_MAX_WAIT_MILLIS = 10_000;
|
|
39
|
+
/** How many views' resource counts a start remembers. */
|
|
40
|
+
const MAX_COUNTED_VIEWS = 8;
|
|
41
|
+
const SKIPPED_INITIATORS = new Set(["fetch", "xmlhttprequest", "beacon"]);
|
|
42
|
+
/** The resource type of each initiator type; any other initiator is other. */
|
|
43
|
+
export const RESOURCE_TYPES = {
|
|
44
|
+
script: "script",
|
|
45
|
+
css: "stylesheet",
|
|
46
|
+
link: "stylesheet",
|
|
47
|
+
img: "image",
|
|
48
|
+
image: "image",
|
|
49
|
+
input: "image",
|
|
50
|
+
video: "media",
|
|
51
|
+
audio: "media",
|
|
52
|
+
track: "media",
|
|
53
|
+
iframe: "document",
|
|
54
|
+
frame: "document",
|
|
55
|
+
};
|
|
56
|
+
/** Initiators that load several kinds of resource, so the URL's extension decides. */
|
|
57
|
+
const BY_EXTENSION_INITIATORS = new Set(["css", "link", "other"]);
|
|
58
|
+
const EXTENSION_TYPES = {
|
|
59
|
+
woff: "font",
|
|
60
|
+
woff2: "font",
|
|
61
|
+
ttf: "font",
|
|
62
|
+
otf: "font",
|
|
63
|
+
eot: "font",
|
|
64
|
+
png: "image",
|
|
65
|
+
jpg: "image",
|
|
66
|
+
jpeg: "image",
|
|
67
|
+
gif: "image",
|
|
68
|
+
webp: "image",
|
|
69
|
+
avif: "image",
|
|
70
|
+
svg: "image",
|
|
71
|
+
ico: "image",
|
|
72
|
+
js: "script",
|
|
73
|
+
mjs: "script",
|
|
74
|
+
css: "stylesheet",
|
|
75
|
+
};
|
|
76
|
+
function extensionOf(url) {
|
|
77
|
+
try {
|
|
78
|
+
const path = new URL(url).pathname;
|
|
79
|
+
const dot = path.lastIndexOf(".");
|
|
80
|
+
return dot < 0 || dot < path.lastIndexOf("/") ? "" : path.slice(dot + 1).toLowerCase();
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The resource type of an entry: by initiator type, except that a css, link or other initiator with
|
|
88
|
+
* a font, image, script or stylesheet extension takes the extension's type (fonts and background
|
|
89
|
+
* images load with the css initiator).
|
|
90
|
+
*/
|
|
91
|
+
export function resourceType(entry) {
|
|
92
|
+
if (BY_EXTENSION_INITIATORS.has(entry.initiatorType)) {
|
|
93
|
+
const byExtension = EXTENSION_TYPES[extensionOf(entry.name)];
|
|
94
|
+
if (byExtension !== undefined)
|
|
95
|
+
return byExtension;
|
|
96
|
+
}
|
|
97
|
+
return RESOURCE_TYPES[entry.initiatorType] ?? "other";
|
|
98
|
+
}
|
|
99
|
+
function httpUrl(href) {
|
|
100
|
+
try {
|
|
101
|
+
const u = new URL(href);
|
|
102
|
+
return u.protocol === "http:" || u.protocol === "https:" ? u : undefined;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** For tests: the resources collector over an injected page. */
|
|
109
|
+
export function resourcesWith(opts, env) {
|
|
110
|
+
const maxPerView = opts.maxPerView ?? DEFAULT_MAX_PER_VIEW;
|
|
111
|
+
let starts = 0;
|
|
112
|
+
return {
|
|
113
|
+
name: "resources",
|
|
114
|
+
start(rt) {
|
|
115
|
+
starts++;
|
|
116
|
+
/** A restart records only resources that started after it. */
|
|
117
|
+
const since = starts === 1 ? Number.NEGATIVE_INFINITY : rt.now();
|
|
118
|
+
const perView = new Map();
|
|
119
|
+
const waiting = [];
|
|
120
|
+
let poll;
|
|
121
|
+
let giveUpAt;
|
|
122
|
+
let waitUntil;
|
|
123
|
+
let stopped = false;
|
|
124
|
+
/** Takes one of the view's maxPerView slots, false when none is left. */
|
|
125
|
+
const take = (view) => {
|
|
126
|
+
const n = perView.get(view.id) ?? 0;
|
|
127
|
+
if (n >= maxPerView)
|
|
128
|
+
return false;
|
|
129
|
+
perView.delete(view.id);
|
|
130
|
+
perView.set(view.id, n + 1);
|
|
131
|
+
while (perView.size > MAX_COUNTED_VIEWS)
|
|
132
|
+
perView.delete(perView.keys().next().value);
|
|
133
|
+
return true;
|
|
134
|
+
};
|
|
135
|
+
const record = (entry, view, inLoad) => {
|
|
136
|
+
const url = httpUrl(entry.name);
|
|
137
|
+
if (url === undefined)
|
|
138
|
+
return;
|
|
139
|
+
const type = resourceType(entry);
|
|
140
|
+
const st = readServerTiming(entry.serverTiming ?? []);
|
|
141
|
+
const attributes = {
|
|
142
|
+
[ATTR_RUM_RESOURCE_TYPE]: type,
|
|
143
|
+
"url.full": stripQuery(url.href, rt.settings().queryAllowList),
|
|
144
|
+
"server.address": url.hostname,
|
|
145
|
+
[ATTR_SESSION_ID]: view.sessionId,
|
|
146
|
+
[ATTR_RUM_VIEW_ID]: view.id,
|
|
147
|
+
...(entry.responseStatus !== undefined && entry.responseStatus > 0
|
|
148
|
+
? { "http.response.status_code": entry.responseStatus }
|
|
149
|
+
: {}),
|
|
150
|
+
...(entry.encodedBodySize !== undefined && entry.encodedBodySize > 0
|
|
151
|
+
? { "http.response.body.size": entry.encodedBodySize }
|
|
152
|
+
: {}),
|
|
153
|
+
};
|
|
154
|
+
const appLoad = inLoad ? rt.views.appLoad() : undefined;
|
|
155
|
+
const links = [];
|
|
156
|
+
if (!inLoad && view.spanContext !== undefined)
|
|
157
|
+
links.push({ context: view.spanContext });
|
|
158
|
+
if (st.remote !== undefined)
|
|
159
|
+
links.push({ context: st.remote });
|
|
160
|
+
const start = env.timeOrigin + entry.startTime;
|
|
161
|
+
const span = rt.tracer.startSpan(`resource ${type}`, { kind: SpanKind.CLIENT, startTime: start, attributes, ...(links.length === 0 ? {} : { links }) }, appLoad === undefined ? ROOT_CONTEXT : trace.setSpanContext(ROOT_CONTEXT, appLoad));
|
|
162
|
+
for (const e of st.entries) {
|
|
163
|
+
span.addEvent("server_timing", { [ATTR_RUM_SERVER_TIMING_NAME]: e.name, [ATTR_RUM_SERVER_TIMING_DURATION]: e.seconds }, start);
|
|
164
|
+
}
|
|
165
|
+
span.end(env.timeOrigin + Math.max(entry.responseEnd, entry.startTime));
|
|
166
|
+
};
|
|
167
|
+
const release = () => {
|
|
168
|
+
if (poll !== undefined)
|
|
169
|
+
env.clearTimeout(poll);
|
|
170
|
+
poll = undefined;
|
|
171
|
+
giveUpAt = undefined;
|
|
172
|
+
waitUntil = undefined;
|
|
173
|
+
for (const w of waiting.splice(0))
|
|
174
|
+
record(w.entry, w.view, true);
|
|
175
|
+
};
|
|
176
|
+
const drain = () => {
|
|
177
|
+
poll = undefined;
|
|
178
|
+
if (stopped)
|
|
179
|
+
return;
|
|
180
|
+
const now = rt.now();
|
|
181
|
+
if (env.loadEventEnd() > 0 && giveUpAt === undefined)
|
|
182
|
+
giveUpAt = now + RESOURCE_APP_LOAD_WAIT_MILLIS;
|
|
183
|
+
if (rt.views.appLoad() !== undefined ||
|
|
184
|
+
(giveUpAt !== undefined && now >= giveUpAt) ||
|
|
185
|
+
(waitUntil !== undefined && now >= waitUntil)) {
|
|
186
|
+
release();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
poll = env.setTimeout(drain, RESOURCE_POLL_MILLIS);
|
|
190
|
+
};
|
|
191
|
+
const onEntries = (entries) => {
|
|
192
|
+
if (stopped || !rt.gate.recording())
|
|
193
|
+
return;
|
|
194
|
+
for (const entry of entries) {
|
|
195
|
+
if (entry.startTime < since || SKIPPED_INITIATORS.has(entry.initiatorType))
|
|
196
|
+
continue;
|
|
197
|
+
if (httpUrl(entry.name) === undefined ||
|
|
198
|
+
rt.ignored(entry.name) ||
|
|
199
|
+
env.ignore?.(entry.name) === true) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const loadEnd = env.loadEventEnd();
|
|
203
|
+
const inLoad = loadEnd === 0 || entry.startTime < loadEnd;
|
|
204
|
+
const view = inLoad ? rt.views.loadView() : rt.views.current();
|
|
205
|
+
if (view === undefined || !take(view))
|
|
206
|
+
continue;
|
|
207
|
+
if (inLoad && rt.views.appLoad() === undefined) {
|
|
208
|
+
waitUntil ??= rt.now() + RESOURCE_MAX_WAIT_MILLIS;
|
|
209
|
+
waiting.push({ entry, view });
|
|
210
|
+
}
|
|
211
|
+
else
|
|
212
|
+
record(entry, view, inLoad);
|
|
213
|
+
}
|
|
214
|
+
if (waiting.length > 0 && poll === undefined)
|
|
215
|
+
drain();
|
|
216
|
+
};
|
|
217
|
+
const unobserve = env.observe(onEntries);
|
|
218
|
+
// A page hidden before app_load is known may never come back: record what waits now.
|
|
219
|
+
const offHide = rt.onHide(() => {
|
|
220
|
+
if (!stopped && rt.gate.recording())
|
|
221
|
+
release();
|
|
222
|
+
});
|
|
223
|
+
return () => {
|
|
224
|
+
stopped = true;
|
|
225
|
+
waiting.length = 0;
|
|
226
|
+
if (poll !== undefined)
|
|
227
|
+
env.clearTimeout(poll);
|
|
228
|
+
poll = undefined;
|
|
229
|
+
offHide();
|
|
230
|
+
unobserve();
|
|
231
|
+
};
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function browserResourcesEnv() {
|
|
236
|
+
return {
|
|
237
|
+
timeOrigin: performance.timeOrigin,
|
|
238
|
+
observe(cb) {
|
|
239
|
+
if (typeof PerformanceObserver === "undefined")
|
|
240
|
+
return () => { };
|
|
241
|
+
const observer = new PerformanceObserver((list) => {
|
|
242
|
+
cb(list.getEntries());
|
|
243
|
+
});
|
|
244
|
+
try {
|
|
245
|
+
observer.observe({ type: "resource", buffered: true });
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return () => { };
|
|
249
|
+
}
|
|
250
|
+
return () => observer.disconnect();
|
|
251
|
+
},
|
|
252
|
+
loadEventEnd() {
|
|
253
|
+
const nav = performance.getEntriesByType?.("navigation")[0];
|
|
254
|
+
return nav?.loadEventEnd ?? 0;
|
|
255
|
+
},
|
|
256
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
257
|
+
clearTimeout: (h) => clearTimeout(h),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** The resources collector: a span for each script, stylesheet, image, font, media or frame the page loads. */
|
|
261
|
+
export function resources(opts = {}) {
|
|
262
|
+
let inner;
|
|
263
|
+
return {
|
|
264
|
+
name: "resources",
|
|
265
|
+
start(rt) {
|
|
266
|
+
inner ??= resourcesWith(opts, browserResourcesEnv());
|
|
267
|
+
return inner.start(rt);
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SpanContext } from "@opentelemetry/api";
|
|
2
|
+
/** The fields of a PerformanceServerTiming entry. Durations are milliseconds. */
|
|
3
|
+
export interface ServerTimingEntryLike {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly duration: number;
|
|
6
|
+
readonly description: string;
|
|
7
|
+
}
|
|
8
|
+
/** The most Server-Timing entries one resource span reports. */
|
|
9
|
+
export declare const MAX_SERVER_TIMING_ENTRIES = 10;
|
|
10
|
+
export interface ServerTiming {
|
|
11
|
+
/** The server span a traceparent entry names. */
|
|
12
|
+
readonly remote?: SpanContext;
|
|
13
|
+
readonly entries: readonly {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly seconds: number;
|
|
16
|
+
}[];
|
|
17
|
+
}
|
|
18
|
+
export declare function readServerTiming(entries: readonly ServerTimingEntryLike[]): ServerTiming;
|