@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
package/dist/settings.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Resolves RumOptions into RumSettings, refusing invalid values. */
|
|
2
|
+
import { O11yError } from "@libo11y/core";
|
|
3
|
+
import { defaultViewName } from "./views/naming.js";
|
|
4
|
+
function positive(name, v, dflt) {
|
|
5
|
+
if (v === undefined)
|
|
6
|
+
return dflt;
|
|
7
|
+
if (!(Number.isFinite(v) && v > 0))
|
|
8
|
+
throw new O11yError("INVALID_OPTION", `rum.${name} must be a positive number`);
|
|
9
|
+
return v;
|
|
10
|
+
}
|
|
11
|
+
/** The settings for o. Throws O11yError INVALID_OPTION for an invalid value. */
|
|
12
|
+
export function resolveSettings(o) {
|
|
13
|
+
const rate = o?.sessionSampleRate ?? 1;
|
|
14
|
+
if (!(rate >= 0 && rate <= 1))
|
|
15
|
+
throw new O11yError("INVALID_OPTION", "rum.sessionSampleRate must be between 0 and 1");
|
|
16
|
+
if (o?.clientToken !== undefined && (typeof o.clientToken !== "string" || o.clientToken === "")) {
|
|
17
|
+
throw new O11yError("INVALID_OPTION", "rum.clientToken must be a non-empty string");
|
|
18
|
+
}
|
|
19
|
+
const collectors = o?.collectors ?? [];
|
|
20
|
+
const names = new Set();
|
|
21
|
+
for (const c of collectors) {
|
|
22
|
+
if (names.has(c.name))
|
|
23
|
+
throw new O11yError("INVALID_OPTION", `rum.collectors lists ${c.name} twice`);
|
|
24
|
+
names.add(c.name);
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
sessionSampleRate: rate,
|
|
28
|
+
sessionIdleMillis: positive("sessionIdleMillis", o?.sessionIdleMillis, 900_000),
|
|
29
|
+
sessionMaxMillis: positive("sessionMaxMillis", o?.sessionMaxMillis, 14_400_000),
|
|
30
|
+
queryAllowList: new Set(o?.queryAllowList ?? []),
|
|
31
|
+
enabled: names,
|
|
32
|
+
viewName: o?.viewName ?? defaultViewName,
|
|
33
|
+
collectors,
|
|
34
|
+
...(o?.clientToken === undefined ? {} : { clientToken: o.clientToken }),
|
|
35
|
+
};
|
|
36
|
+
}
|
package/dist/setup.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* setupRum: @libo11y/web's setup with RUM wired in (decision P1). setupRumWith binds the RUM core
|
|
3
|
+
* (core-setup.ts) to the web: web's setupWithDeps through webBinding, the page's lifecycle from
|
|
4
|
+
* visibilitychange, pagehide and pageshow, remote configuration cached in localStorage, and the
|
|
5
|
+
* bootstrap buffer replayed. The extension contexts pass their own binding and stores in deps.
|
|
6
|
+
*/
|
|
7
|
+
import type { RUMPlatform } from "@libo11y/spec";
|
|
8
|
+
import type { WebSetupOptions, WebTelemetry } from "@libo11y/web";
|
|
9
|
+
import { type SetupDeps } from "@libo11y/web/internal";
|
|
10
|
+
import type { RumBind, RumLifecycle } from "./contracts/binding.ts";
|
|
11
|
+
import { type RumCoreDeps } from "./core-setup.ts";
|
|
12
|
+
import type { RumSetupOptions, RumTelemetry } from "./options.ts";
|
|
13
|
+
export { runtimeOf, SESSION_END_ELECTION_MILLIS } from "./core-setup.ts";
|
|
14
|
+
/** What setupRumWith builds on. Internal: tests and the extension wiring replace parts of it. */
|
|
15
|
+
export interface RumDeps extends Omit<RumCoreDeps<WebSetupOptions, WebTelemetry>, "platform" | "bind" | "lifecycle" | "window" | "bootstrap" | "storage"> {
|
|
16
|
+
readonly web: SetupDeps;
|
|
17
|
+
readonly document: (EventTarget & {
|
|
18
|
+
readonly visibilityState?: string;
|
|
19
|
+
}) | undefined;
|
|
20
|
+
readonly window: EventTarget | undefined;
|
|
21
|
+
/** Install the telemetry globally, as setup does. Default true; tests that run two tabs in one process pass false. */
|
|
22
|
+
readonly install?: boolean;
|
|
23
|
+
/** The platform summaries, session starts and app_load record. Default "web". */
|
|
24
|
+
readonly platform?: RUMPlatform;
|
|
25
|
+
/** Runs the platform's setup with RUM's wrappers. Default webBinding(web, install). */
|
|
26
|
+
readonly bind?: RumBind<WebSetupOptions, WebTelemetry>;
|
|
27
|
+
/** Default webLifecycle(document, window). */
|
|
28
|
+
readonly lifecycle?: RumLifecycle;
|
|
29
|
+
/**
|
|
30
|
+
* Where remote configuration is cached. Default localStorage. Called only once consent is granted.
|
|
31
|
+
* It may resolve later, as a cache in chrome.storage.local does once it has read the stored value.
|
|
32
|
+
*/
|
|
33
|
+
readonly storage?: RumCoreDeps<WebSetupOptions, WebTelemetry>["storage"];
|
|
34
|
+
}
|
|
35
|
+
/** The real dependencies of a page. */
|
|
36
|
+
export declare function defaultRumDeps(): RumDeps;
|
|
37
|
+
/** Builds browser telemetry with RUM and installs it globally, as the setup of `@libo11y/web` does. */
|
|
38
|
+
export declare function setupRum(options?: RumSetupOptions): Promise<RumTelemetry>;
|
|
39
|
+
/** setupRum over deps. Internal: the seam the unit tests and the extension wiring use. */
|
|
40
|
+
export declare function setupRumWith(options: RumSetupOptions, deps: RumDeps): Promise<RumTelemetry>;
|
package/dist/setup.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { defaultSetupDeps, openQueue } from "@libo11y/web/internal";
|
|
2
|
+
import { webBinding, webLifecycle } from "./bind.js";
|
|
3
|
+
import { takeBootstrap } from "./bootstrap/buffer.js";
|
|
4
|
+
import { replayBootstrap } from "./bootstrap/replay.js";
|
|
5
|
+
import { setupRumCore } from "./core-setup.js";
|
|
6
|
+
import { localStorageSessionStore } from "./session/local-store.js";
|
|
7
|
+
export { runtimeOf, SESSION_END_ELECTION_MILLIS } from "./core-setup.js";
|
|
8
|
+
/** The real dependencies of a page. */
|
|
9
|
+
export function defaultRumDeps() {
|
|
10
|
+
const g = globalThis;
|
|
11
|
+
const web = defaultSetupDeps();
|
|
12
|
+
return {
|
|
13
|
+
web,
|
|
14
|
+
openRetentionQueue: openQueue,
|
|
15
|
+
sessionStore: () => localStorageSessionStore(),
|
|
16
|
+
now: Date.now,
|
|
17
|
+
perfNow: () => performance.now(),
|
|
18
|
+
timeOrigin: performance.timeOrigin,
|
|
19
|
+
location: () => g.location?.href ?? "about:blank",
|
|
20
|
+
prerendered: () => {
|
|
21
|
+
if (g.document?.prerendering === true)
|
|
22
|
+
return true;
|
|
23
|
+
const nav = performance.getEntriesByType?.("navigation")[0];
|
|
24
|
+
return (nav?.activationStart ?? 0) > 0;
|
|
25
|
+
},
|
|
26
|
+
connection: () => g.navigator?.connection?.effectiveType,
|
|
27
|
+
document: g.document,
|
|
28
|
+
window: web.window,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Builds browser telemetry with RUM and installs it globally, as the setup of `@libo11y/web` does. */
|
|
32
|
+
export function setupRum(options = {}) {
|
|
33
|
+
return setupRumWith(options, defaultRumDeps());
|
|
34
|
+
}
|
|
35
|
+
/** setupRum over deps. Internal: the seam the unit tests and the extension wiring use. */
|
|
36
|
+
export function setupRumWith(options, deps) {
|
|
37
|
+
const { rum, ...webOptions } = options;
|
|
38
|
+
return setupRumCore(webOptions, rum, {
|
|
39
|
+
...deps,
|
|
40
|
+
platform: deps.platform ?? "web",
|
|
41
|
+
bind: deps.bind ?? webBinding(deps.web, deps.install !== false),
|
|
42
|
+
lifecycle: deps.lifecycle ?? webLifecycle(deps.document, deps.window),
|
|
43
|
+
storage: deps.storage ?? (() => globalThis.localStorage),
|
|
44
|
+
bootstrap: () => {
|
|
45
|
+
const early = takeBootstrap();
|
|
46
|
+
return (runtime) => replayBootstrap(runtime, early);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RUM span processor. On every span start it adds session.id, o11y.rum.view.id (unless the
|
|
3
|
+
* span already names them) and user.id,
|
|
4
|
+
* removes disallowed query parameters from url.full, and turns web's documentLoad span into the
|
|
5
|
+
* app_load shape of the load view: it is renamed app_load and carries o11y.rum.span.shape app_load,
|
|
6
|
+
* the attribute the gateway keys on. Its documentFetch child also belongs to the load view, and it
|
|
7
|
+
* is not a request of the view. It publishes client request starts and ends on the bus for
|
|
8
|
+
* actions, summaries and errors, and a breadcrumb for each finished request: "fetch <METHOD> <host>
|
|
9
|
+
* <status or failed>", or "rpc <method>" for a span with rpc.method, never a path (decision P18). It runs before the exporting processors, so they see the
|
|
10
|
+
* attributes. A span that starts while the session loads after a grant gets the same treatment,
|
|
11
|
+
* and waits in pending for its session.id and view (R3f).
|
|
12
|
+
*/
|
|
13
|
+
import { type RUMPlatform } from "@libo11y/spec";
|
|
14
|
+
import type { SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
15
|
+
import type { RumBus } from "./contracts/runtime.ts";
|
|
16
|
+
import type { SessionManager } from "./contracts/session.ts";
|
|
17
|
+
import type { ViewTracker } from "./contracts/views.ts";
|
|
18
|
+
import type { PendingSpans } from "./pending-spans.ts";
|
|
19
|
+
/** The name of the app_load span shape. */
|
|
20
|
+
export declare const APP_LOAD_SPAN = "app_load";
|
|
21
|
+
export interface RumSpanProcessorOptions {
|
|
22
|
+
readonly platform: RUMPlatform;
|
|
23
|
+
readonly session: Pick<SessionManager, "current">;
|
|
24
|
+
readonly views: Pick<ViewTracker, "current" | "loadView" | "appLoadStarted">;
|
|
25
|
+
readonly queryAllowList: () => ReadonlySet<string>;
|
|
26
|
+
readonly userId: () => string | undefined;
|
|
27
|
+
readonly bus: RumBus;
|
|
28
|
+
/** performance.now(). */
|
|
29
|
+
readonly now: () => number;
|
|
30
|
+
/**
|
|
31
|
+
* performance.timeOrigin, to read a span's end time on the now() clock. Without it the bus
|
|
32
|
+
* reports now() alone.
|
|
33
|
+
*/
|
|
34
|
+
readonly timeOrigin?: number;
|
|
35
|
+
/** Where spans that start while the session loads wait for it. */
|
|
36
|
+
readonly pending?: Pick<PendingSpans, "isOpen" | "hold">;
|
|
37
|
+
}
|
|
38
|
+
export declare function rumSpanProcessor(opts: RumSpanProcessorOptions): SpanProcessor;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RUM span processor. On every span start it adds session.id, o11y.rum.view.id (unless the
|
|
3
|
+
* span already names them) and user.id,
|
|
4
|
+
* removes disallowed query parameters from url.full, and turns web's documentLoad span into the
|
|
5
|
+
* app_load shape of the load view: it is renamed app_load and carries o11y.rum.span.shape app_load,
|
|
6
|
+
* the attribute the gateway keys on. Its documentFetch child also belongs to the load view, and it
|
|
7
|
+
* is not a request of the view. It publishes client request starts and ends on the bus for
|
|
8
|
+
* actions, summaries and errors, and a breadcrumb for each finished request: "fetch <METHOD> <host>
|
|
9
|
+
* <status or failed>", or "rpc <method>" for a span with rpc.method, never a path (decision P18). It runs before the exporting processors, so they see the
|
|
10
|
+
* attributes. A span that starts while the session loads after a grant gets the same treatment,
|
|
11
|
+
* and waits in pending for its session.id and view (R3f).
|
|
12
|
+
*/
|
|
13
|
+
import { ATTR_RUM_PLATFORM, ATTR_RUM_SPAN_SHAPE, ATTR_RUM_VIEW_ID, RUM_SPAN_SHAPE_VALUE_APP_LOAD, } from "@libo11y/spec";
|
|
14
|
+
import { SpanKind } from "@opentelemetry/api";
|
|
15
|
+
import { hrTimeToMilliseconds } from "@opentelemetry/core";
|
|
16
|
+
import { ATTR_SESSION_ID, ATTR_USER_ID } from "./attrs.js";
|
|
17
|
+
import { stripQuery } from "./query.js";
|
|
18
|
+
/** The name of the app_load span shape. */
|
|
19
|
+
export const APP_LOAD_SPAN = "app_load";
|
|
20
|
+
const DOCUMENT_LOAD = "documentLoad";
|
|
21
|
+
/** web's child of documentLoad: the document request, part of the load and not a request the view made. */
|
|
22
|
+
const DOCUMENT_FETCH = "documentFetch";
|
|
23
|
+
const WEB_SCOPE = "@libo11y/web";
|
|
24
|
+
function fromWeb(span, name) {
|
|
25
|
+
return span.name === name && span.instrumentationScope.name === WEB_SCOPE;
|
|
26
|
+
}
|
|
27
|
+
function hostOf(span) {
|
|
28
|
+
const address = span.attributes["server.address"];
|
|
29
|
+
if (typeof address === "string" && address !== "")
|
|
30
|
+
return address;
|
|
31
|
+
const full = span.attributes["url.full"];
|
|
32
|
+
try {
|
|
33
|
+
return typeof full === "string" ? new URL(full).hostname : "";
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function isRequest(span) {
|
|
40
|
+
return (span.kind === SpanKind.CLIENT &&
|
|
41
|
+
typeof span.attributes["http.request.method"] === "string" &&
|
|
42
|
+
!fromWeb(span, DOCUMENT_FETCH));
|
|
43
|
+
}
|
|
44
|
+
export function rumSpanProcessor(opts) {
|
|
45
|
+
// Request spans this processor announced, so request-end is published only for them.
|
|
46
|
+
const announced = new Set();
|
|
47
|
+
return {
|
|
48
|
+
onStart(span, _parent) {
|
|
49
|
+
const session = opts.session.current();
|
|
50
|
+
if (session === undefined && opts.pending?.isOpen() !== true)
|
|
51
|
+
return;
|
|
52
|
+
const appLoad = fromWeb(span, DOCUMENT_LOAD);
|
|
53
|
+
const ofLoad = appLoad || fromWeb(span, DOCUMENT_FETCH);
|
|
54
|
+
if (session === undefined) {
|
|
55
|
+
// Consent is granted and the session still loading: the span waits for it.
|
|
56
|
+
opts.pending?.hold(span, ofLoad);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// RUM's own view and action spans name their view and session, which may be older ones.
|
|
60
|
+
if (span.attributes[ATTR_SESSION_ID] === undefined)
|
|
61
|
+
span.setAttribute(ATTR_SESSION_ID, session.id);
|
|
62
|
+
const view = ofLoad ? opts.views.loadView() : opts.views.current();
|
|
63
|
+
if (view !== undefined && span.attributes[ATTR_RUM_VIEW_ID] === undefined) {
|
|
64
|
+
span.setAttribute(ATTR_RUM_VIEW_ID, view.id);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const user = opts.userId();
|
|
68
|
+
if (user !== undefined)
|
|
69
|
+
span.setAttribute(ATTR_USER_ID, user);
|
|
70
|
+
const full = span.attributes["url.full"];
|
|
71
|
+
const url = typeof full === "string" ? stripQuery(full, opts.queryAllowList()) : undefined;
|
|
72
|
+
if (url !== undefined && url !== full)
|
|
73
|
+
span.setAttribute("url.full", url);
|
|
74
|
+
if (appLoad) {
|
|
75
|
+
span.updateName(APP_LOAD_SPAN);
|
|
76
|
+
span.setAttribute(ATTR_RUM_SPAN_SHAPE, RUM_SPAN_SHAPE_VALUE_APP_LOAD);
|
|
77
|
+
span.setAttribute(ATTR_RUM_PLATFORM, opts.platform);
|
|
78
|
+
opts.views.appLoadStarted(span.spanContext());
|
|
79
|
+
}
|
|
80
|
+
if (isRequest(span)) {
|
|
81
|
+
const spanId = span.spanContext().spanId;
|
|
82
|
+
const parent = span.parentSpanContext?.spanId;
|
|
83
|
+
announced.add(spanId);
|
|
84
|
+
opts.bus.publish({
|
|
85
|
+
type: "request-start",
|
|
86
|
+
spanId,
|
|
87
|
+
...(parent === undefined ? {} : { parentSpanId: parent }),
|
|
88
|
+
method: String(span.attributes["http.request.method"]),
|
|
89
|
+
url: url ?? "",
|
|
90
|
+
at: opts.now(),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
onEnd(span) {
|
|
95
|
+
const spanId = span.spanContext().spanId;
|
|
96
|
+
if (!announced.delete(spanId))
|
|
97
|
+
return;
|
|
98
|
+
const status = span.attributes["http.response.status_code"];
|
|
99
|
+
const errorType = span.attributes["error.type"];
|
|
100
|
+
// The later of now and the span's own end, so an action that settles at this time never
|
|
101
|
+
// ends before the request it caused (WebKit's span end can trail now() by a millisecond).
|
|
102
|
+
const origin = opts.timeOrigin;
|
|
103
|
+
const spanEnd = origin === undefined ? Number.NaN : hrTimeToMilliseconds(span.endTime) - origin;
|
|
104
|
+
const at = Number.isFinite(spanEnd) ? Math.max(opts.now(), spanEnd) : opts.now();
|
|
105
|
+
opts.bus.publish({
|
|
106
|
+
type: "request-end",
|
|
107
|
+
spanId,
|
|
108
|
+
...(typeof status === "number" ? { status } : {}),
|
|
109
|
+
...(typeof status !== "number" && typeof errorType === "string" ? { errorType } : {}),
|
|
110
|
+
at,
|
|
111
|
+
});
|
|
112
|
+
const rpc = span.attributes["rpc.method"];
|
|
113
|
+
opts.bus.publish(rpc === undefined
|
|
114
|
+
? {
|
|
115
|
+
type: "breadcrumb",
|
|
116
|
+
kind: "fetch",
|
|
117
|
+
label: `${String(span.attributes["http.request.method"])} ${hostOf(span)} ${typeof status === "number" ? status : "failed"}`,
|
|
118
|
+
at,
|
|
119
|
+
}
|
|
120
|
+
: { type: "breadcrumb", kind: "rpc", label: String(rpc), at });
|
|
121
|
+
},
|
|
122
|
+
forceFlush: () => Promise.resolve(),
|
|
123
|
+
shutdown: () => Promise.resolve(),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view summary accumulator (design D2, spec 1.3.0 o11y.rum.view.summary). conformance/rum-summary
|
|
3
|
+
* holds the shared vectors.
|
|
4
|
+
*
|
|
5
|
+
* Rules. A tracked view emits on close, and on flush when it has never emitted (seq 0) or has a
|
|
6
|
+
* nonzero count, unsent visible time or an unsent vital. Counts and time on view are deltas since
|
|
7
|
+
* the view's previous summary; a zero count is omitted. A vital is accepted once per view and name
|
|
8
|
+
* and is sent in the next summary of that view. A closed view stays tracked, for late vitals, until
|
|
9
|
+
* ten newer views closed after it, and while it is pinned (vitals() pins the load view until it has
|
|
10
|
+
* committed its held vitals).
|
|
11
|
+
*/
|
|
12
|
+
import { type RUMPlatform } from "@libo11y/spec";
|
|
13
|
+
import type { SummaryAccumulator } from "../contracts/summary.ts";
|
|
14
|
+
export interface SummaryOptions {
|
|
15
|
+
readonly platform: RUMPlatform;
|
|
16
|
+
/** Emits one o11y.rum.view.summary record with these attributes. */
|
|
17
|
+
readonly emit: (attrs: Record<string, string | number>) => void;
|
|
18
|
+
/** performance.now(). */
|
|
19
|
+
readonly now: () => number;
|
|
20
|
+
/** Whether the page is visible now. */
|
|
21
|
+
readonly visible: () => boolean;
|
|
22
|
+
/** Optional attributes added to every summary: o11y.rum.connection.effective, o11y.rum.config.version. */
|
|
23
|
+
readonly context?: () => Readonly<Record<string, string | number>>;
|
|
24
|
+
}
|
|
25
|
+
export declare function createSummaryAccumulator(opts: SummaryOptions): SummaryAccumulator;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view summary accumulator (design D2, spec 1.3.0 o11y.rum.view.summary). conformance/rum-summary
|
|
3
|
+
* holds the shared vectors.
|
|
4
|
+
*
|
|
5
|
+
* Rules. A tracked view emits on close, and on flush when it has never emitted (seq 0) or has a
|
|
6
|
+
* nonzero count, unsent visible time or an unsent vital. Counts and time on view are deltas since
|
|
7
|
+
* the view's previous summary; a zero count is omitted. A vital is accepted once per view and name
|
|
8
|
+
* and is sent in the next summary of that view. A closed view stays tracked, for late vitals, until
|
|
9
|
+
* ten newer views closed after it, and while it is pinned (vitals() pins the load view until it has
|
|
10
|
+
* committed its held vitals).
|
|
11
|
+
*/
|
|
12
|
+
import { ATTR_RUM_PLATFORM, ATTR_RUM_SUMMARY_SEQ, ATTR_RUM_SUMMARY_TIME_ON_VIEW, ATTR_RUM_VIEW_ID, ATTR_RUM_VIEW_KIND, ATTR_RUM_VIEW_NAME, } from "@libo11y/spec";
|
|
13
|
+
import { ATTR_SESSION_ID, summaryCountKey, summaryVitalKey } from "../attrs.js";
|
|
14
|
+
const MAX_CLOSED = 10;
|
|
15
|
+
export function createSummaryAccumulator(opts) {
|
|
16
|
+
const tracked = new Map();
|
|
17
|
+
let current;
|
|
18
|
+
const accrue = (t) => {
|
|
19
|
+
if (t.resumedAt === undefined)
|
|
20
|
+
return;
|
|
21
|
+
const n = opts.now();
|
|
22
|
+
t.visibleMillis += Math.max(0, n - t.resumedAt);
|
|
23
|
+
t.resumedAt = n;
|
|
24
|
+
};
|
|
25
|
+
const pending = (t) => t.seq === 0 || t.counts.size > 0 || t.vitals.size > 0 || t.visibleMillis > 0;
|
|
26
|
+
const send = (t) => {
|
|
27
|
+
accrue(t);
|
|
28
|
+
if (!pending(t))
|
|
29
|
+
return;
|
|
30
|
+
const attrs = {
|
|
31
|
+
...(opts.context?.() ?? {}),
|
|
32
|
+
[ATTR_RUM_PLATFORM]: opts.platform,
|
|
33
|
+
[ATTR_RUM_VIEW_ID]: t.view.id,
|
|
34
|
+
[ATTR_RUM_VIEW_NAME]: t.view.name,
|
|
35
|
+
[ATTR_RUM_VIEW_KIND]: t.view.kind,
|
|
36
|
+
[ATTR_SESSION_ID]: t.view.sessionId,
|
|
37
|
+
[ATTR_RUM_SUMMARY_SEQ]: t.seq,
|
|
38
|
+
[ATTR_RUM_SUMMARY_TIME_ON_VIEW]: t.visibleMillis / 1000,
|
|
39
|
+
};
|
|
40
|
+
for (const [k, n] of t.counts)
|
|
41
|
+
if (n !== 0)
|
|
42
|
+
attrs[summaryCountKey(k)] = n;
|
|
43
|
+
for (const [v, x] of t.vitals) {
|
|
44
|
+
attrs[summaryVitalKey(v)] = x;
|
|
45
|
+
t.sent.add(v);
|
|
46
|
+
}
|
|
47
|
+
t.seq += 1;
|
|
48
|
+
t.counts = new Map();
|
|
49
|
+
t.vitals = new Map();
|
|
50
|
+
t.visibleMillis = 0;
|
|
51
|
+
opts.emit(attrs);
|
|
52
|
+
};
|
|
53
|
+
const prune = () => {
|
|
54
|
+
const closed = [...tracked.values()].filter((t) => !t.open && !t.pinned);
|
|
55
|
+
for (const t of closed.slice(0, Math.max(0, closed.length - MAX_CLOSED)))
|
|
56
|
+
tracked.delete(t.view.id);
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
open(view) {
|
|
60
|
+
const t = {
|
|
61
|
+
view,
|
|
62
|
+
seq: 0,
|
|
63
|
+
counts: new Map(),
|
|
64
|
+
vitals: new Map(),
|
|
65
|
+
sent: new Set(),
|
|
66
|
+
visibleMillis: 0,
|
|
67
|
+
resumedAt: opts.visible() ? opts.now() : undefined,
|
|
68
|
+
open: true,
|
|
69
|
+
pinned: false,
|
|
70
|
+
};
|
|
71
|
+
tracked.set(view.id, t);
|
|
72
|
+
current = t;
|
|
73
|
+
},
|
|
74
|
+
close(viewId) {
|
|
75
|
+
const t = tracked.get(viewId);
|
|
76
|
+
if (t === undefined || !t.open)
|
|
77
|
+
return;
|
|
78
|
+
send(t);
|
|
79
|
+
t.open = false;
|
|
80
|
+
t.resumedAt = undefined;
|
|
81
|
+
if (current === t)
|
|
82
|
+
current = undefined;
|
|
83
|
+
prune();
|
|
84
|
+
},
|
|
85
|
+
rename(viewId, name) {
|
|
86
|
+
const t = tracked.get(viewId);
|
|
87
|
+
if (t !== undefined)
|
|
88
|
+
t.view = { ...t.view, name };
|
|
89
|
+
},
|
|
90
|
+
count(key, n = 1) {
|
|
91
|
+
if (current === undefined)
|
|
92
|
+
return;
|
|
93
|
+
current.counts.set(key, (current.counts.get(key) ?? 0) + n);
|
|
94
|
+
},
|
|
95
|
+
vital(viewId, name, value) {
|
|
96
|
+
const t = tracked.get(viewId);
|
|
97
|
+
if (t === undefined || t.sent.has(name) || t.vitals.has(name))
|
|
98
|
+
return;
|
|
99
|
+
t.vitals.set(name, value);
|
|
100
|
+
},
|
|
101
|
+
pin(viewId, pinned) {
|
|
102
|
+
const t = tracked.get(viewId);
|
|
103
|
+
if (t !== undefined)
|
|
104
|
+
t.pinned = pinned;
|
|
105
|
+
},
|
|
106
|
+
visibility(visible) {
|
|
107
|
+
if (current === undefined)
|
|
108
|
+
return;
|
|
109
|
+
if (visible)
|
|
110
|
+
current.resumedAt ??= opts.now();
|
|
111
|
+
else {
|
|
112
|
+
accrue(current);
|
|
113
|
+
current.resumedAt = undefined;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
flush() {
|
|
117
|
+
for (const t of tracked.values())
|
|
118
|
+
send(t);
|
|
119
|
+
prune();
|
|
120
|
+
},
|
|
121
|
+
reset() {
|
|
122
|
+
tracked.clear();
|
|
123
|
+
current = undefined;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { RumCollector } from "../contracts/runtime.ts";
|
|
2
|
+
import { type HistoryHost } from "./history.ts";
|
|
3
|
+
import { type PaintHost } from "./paint.ts";
|
|
4
|
+
/** A view span ends this long after it started at the latest. */
|
|
5
|
+
export declare const VIEW_SPAN_CAP_MILLIS = 10000;
|
|
6
|
+
/** In history mode, a soft view rendered once the DOM was quiet this long. */
|
|
7
|
+
export declare const RENDER_IDLE_MILLIS = 100;
|
|
8
|
+
/** How long a load view span due to end waits for app_load before it starts as a root. */
|
|
9
|
+
export declare const APP_LOAD_WAIT_MILLIS = 1000;
|
|
10
|
+
export interface ViewsOptions {
|
|
11
|
+
/** Start a soft view whenever the URL path changes. Default true. Router adapters pass false and call rum.startView. */
|
|
12
|
+
readonly history?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** The part of MutationObserver the collectors use. */
|
|
15
|
+
export interface MutationObserverLike {
|
|
16
|
+
observe(target: object, options: MutationObserverInit): void;
|
|
17
|
+
disconnect(): void;
|
|
18
|
+
}
|
|
19
|
+
export type MutationObserverCtor = new (cb: () => void) => MutationObserverLike;
|
|
20
|
+
/** What views() needs from the page. Internal: tests inject a fake. */
|
|
21
|
+
export interface ViewsEnv {
|
|
22
|
+
/** performance.timeOrigin. */
|
|
23
|
+
readonly timeOrigin: number;
|
|
24
|
+
readonly paint: PaintHost;
|
|
25
|
+
readonly history?: HistoryHost;
|
|
26
|
+
readonly MutationObserver?: MutationObserverCtor;
|
|
27
|
+
/** What mutations are observed on: document.documentElement. */
|
|
28
|
+
readonly root?: object;
|
|
29
|
+
setTimeout(cb: () => void, ms: number): unknown;
|
|
30
|
+
clearTimeout(handle: unknown): void;
|
|
31
|
+
/** The navigation entry's loadEventEnd, 0 until the load event ended. */
|
|
32
|
+
navigationLoadEnd(): number;
|
|
33
|
+
/** Calls cb once the load event ended. Returns its cancellation. */
|
|
34
|
+
whenLoaded(cb: () => void): () => void;
|
|
35
|
+
}
|
|
36
|
+
/** The DOM mutations that count as rendering. */
|
|
37
|
+
export declare const MUTATIONS: MutationObserverInit;
|
|
38
|
+
export declare function viewsWith(opts: ViewsOptions, env: ViewsEnv): RumCollector;
|
|
39
|
+
/** The views collector: view spans for every view, and soft navigations in history mode. */
|
|
40
|
+
export declare function views(opts?: ViewsOptions): RumCollector;
|