@libo11y/rum 0.4.0 → 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.
Files changed (67) hide show
  1. package/README.md +37 -20
  2. package/dist/actions/collector.js +2 -2
  3. package/dist/actions/naming.d.ts +1 -1
  4. package/dist/actions/naming.js +1 -1
  5. package/dist/actions/record.d.ts +1 -1
  6. package/dist/actions/record.js +1 -1
  7. package/dist/actions/settle.d.ts +1 -1
  8. package/dist/actions/settle.js +1 -1
  9. package/dist/bind.d.ts +1 -1
  10. package/dist/bootstrap/buffer.d.ts +1 -1
  11. package/dist/bootstrap/buffer.js +1 -1
  12. package/dist/bootstrap/replay.d.ts +1 -1
  13. package/dist/config/clamp.d.ts +2 -2
  14. package/dist/config/clamp.js +2 -2
  15. package/dist/config/remote.d.ts +30 -7
  16. package/dist/config/remote.js +78 -46
  17. package/dist/context-manager.d.ts +1 -1
  18. package/dist/context-manager.js +1 -1
  19. package/dist/contracts/binding.d.ts +4 -2
  20. package/dist/contracts/retention.d.ts +7 -3
  21. package/dist/contracts/runtime.d.ts +24 -14
  22. package/dist/contracts/session.d.ts +6 -1
  23. package/dist/contracts/summary.d.ts +2 -2
  24. package/dist/contracts/views.d.ts +3 -3
  25. package/dist/core-setup.d.ts +43 -8
  26. package/dist/core-setup.js +286 -71
  27. package/dist/core.d.ts +13 -9
  28. package/dist/core.js +6 -5
  29. package/dist/errors/breadcrumbs.d.ts +1 -1
  30. package/dist/errors/breadcrumbs.js +1 -1
  31. package/dist/errors/record.d.ts +1 -1
  32. package/dist/errors/record.js +1 -1
  33. package/dist/frustration/detect.d.ts +1 -1
  34. package/dist/index.d.ts +1 -1
  35. package/dist/internal.d.ts +15 -9
  36. package/dist/internal.js +4 -5
  37. package/dist/pending-spans.d.ts +1 -1
  38. package/dist/query.d.ts +1 -1
  39. package/dist/query.js +1 -1
  40. package/dist/retention/buffer.d.ts +9 -5
  41. package/dist/retention/buffer.js +41 -15
  42. package/dist/retention/exporters.d.ts +56 -2
  43. package/dist/retention/exporters.js +214 -0
  44. package/dist/session/manager.js +1 -1
  45. package/dist/session/sampling.js +1 -1
  46. package/dist/setup.d.ts +3 -1
  47. package/dist/setup.js +14 -6
  48. package/dist/span-processor.d.ts +3 -3
  49. package/dist/span-processor.js +2 -2
  50. package/dist/summary/accumulator.d.ts +7 -2
  51. package/dist/summary/accumulator.js +4 -2
  52. package/dist/version.d.ts +1 -1
  53. package/dist/version.js +1 -1
  54. package/dist/views/collector.js +8 -7
  55. package/dist/views/naming.d.ts +1 -1
  56. package/dist/views/naming.js +1 -1
  57. package/dist/views/tracker.d.ts +15 -6
  58. package/dist/views/tracker.js +10 -13
  59. package/dist/vitals/collector.js +2 -2
  60. package/dist/webext/config-storage.d.ts +15 -4
  61. package/dist/webext/config-storage.js +78 -27
  62. package/dist/webext/scope.d.ts +1 -1
  63. package/dist/webext/session-store.js +12 -2
  64. package/dist/webext/setup.d.ts +5 -2
  65. package/dist/webext/setup.js +29 -16
  66. package/dist/webext.d.ts +1 -1
  67. package/package.json +11 -8
@@ -1,45 +1,96 @@
1
1
  /**
2
2
  * The remote configuration cache in an extension: chrome.storage.local, shared by every context of
3
3
  * the extension and never the host page's localStorage. Remote configuration reads its cache
4
- * synchronously, so this reads the stored value once, keeps it in memory and follows other
5
- * contexts' writes through storage.onChanged. Writes go to memory at once and to storage after.
4
+ * synchronously, so this reads the stored values once, keeps them in memory and follows other
5
+ * contexts' writes through storage.onChanged. Writes go to memory at once and to storage after,
6
+ * and flush resolves once they reached storage. An update (a pin) reads the stored value afresh
7
+ * under navigator.locks where the context runs on the extension's origin, serialized across
8
+ * extension-origin contexts, so contexts pinning at once keep every pin in order. A content script's
9
+ * navigator.locks is the host page's, so it updates without a lock. A removal runs under the same
10
+ * lock, and an update begun before it writes nothing.
6
11
  */
7
12
  import { diag } from "@opentelemetry/api";
8
- import { CONFIG_STORAGE_KEY } from "../config/remote.js";
9
- /** A cache over chrome.storage.local once the stored value is read, or undefined without the area. Never rejects. */
10
- export async function chromeConfigStorage(chrome) {
13
+ import { CONFIG_PINNED_STORAGE_KEY, CONFIG_STORAGE_KEY } from "../config/remote.js";
14
+ const KEYS = [CONFIG_STORAGE_KEY, CONFIG_PINNED_STORAGE_KEY];
15
+ function isExtensionOrigin(chrome) {
16
+ const base = chrome.runtime.getURL?.("");
17
+ const href = globalThis.location?.href;
18
+ if (base === undefined || base === "" || href === undefined)
19
+ return false;
20
+ try {
21
+ const page = new URL(href);
22
+ const ext = new URL(base);
23
+ return page.protocol === ext.protocol && ext.host !== "" && page.host === ext.host;
24
+ }
25
+ catch {
26
+ return false;
27
+ }
28
+ }
29
+ /**
30
+ * A cache over chrome.storage.local once the stored values are read, or undefined without the area.
31
+ * locks null updates without a lock. Never rejects.
32
+ */
33
+ export async function chromeConfigStorage(chrome, locks = isExtensionOrigin(chrome)
34
+ ? globalThis.navigator?.locks
35
+ : undefined) {
11
36
  const area = chrome.storage?.local;
12
37
  if (area === undefined)
13
38
  return undefined;
14
39
  const values = new Map();
15
- try {
16
- const stored = (await area.get(CONFIG_STORAGE_KEY))[CONFIG_STORAGE_KEY];
17
- if (typeof stored === "string")
18
- values.set(CONFIG_STORAGE_KEY, stored);
19
- }
20
- catch (err) {
21
- diag.debug("o11y: rum: remote configuration cache unreadable", err);
22
- }
23
- const onChanged = (changes, areaName) => {
24
- if (areaName !== "local" || !(CONFIG_STORAGE_KEY in changes))
25
- return;
26
- const next = changes[CONFIG_STORAGE_KEY]?.newValue;
27
- if (typeof next === "string")
28
- values.set(CONFIG_STORAGE_KEY, next);
40
+ const keep = (key, v) => {
41
+ if (typeof v === "string")
42
+ values.set(key, v);
29
43
  else
30
- values.delete(CONFIG_STORAGE_KEY);
44
+ values.delete(key);
45
+ };
46
+ const failed = (err) => diag.debug("o11y: rum: the remote configuration cache failed", err);
47
+ for (const key of KEYS)
48
+ keep(key, (await area.get(key).catch(failed))?.[key]);
49
+ const onChanged = (changes, areaName) => {
50
+ if (areaName === "local")
51
+ for (const key of KEYS)
52
+ if (key in changes)
53
+ keep(key, changes[key]?.newValue);
31
54
  };
32
55
  chrome.storage?.onChanged.addListener(onChanged);
33
- const failed = (err) => diag.debug("o11y: rum: remote configuration cache write failed", err);
56
+ /** Writes not yet in storage. */
57
+ const writes = new Set();
58
+ const track = (write) => {
59
+ const done = write.catch(failed).then(() => {
60
+ writes.delete(done);
61
+ });
62
+ writes.add(done);
63
+ };
64
+ const set = (key, value) => {
65
+ values.set(key, value);
66
+ return area.set({ [key]: value });
67
+ };
68
+ const locked = (key, run) => track(locks?.request(key, run) ?? run());
69
+ /** Bumped by each removal, so an update begun before it writes nothing. */
70
+ let removals = 0;
34
71
  return {
35
72
  getItem: (key) => values.get(key) ?? null,
36
- setItem(key, value) {
37
- values.set(key, value);
38
- area.set({ [key]: value }).catch(failed);
39
- },
73
+ setItem: (key, value) => track(set(key, value)),
40
74
  removeItem(key) {
41
- values.delete(key);
42
- area.remove(key).catch(failed);
75
+ removals++;
76
+ keep(key, undefined);
77
+ locked(key, () => area.remove(key));
78
+ },
79
+ updateItem(key, fn) {
80
+ const gen = removals;
81
+ locked(key, async () => {
82
+ if (gen !== removals)
83
+ return;
84
+ const stored = (await area.get(key))[key];
85
+ // Removed since the update began, before or while it waited on the lock or the read.
86
+ if (gen !== removals)
87
+ return;
88
+ keep(key, stored);
89
+ await set(key, fn(values.get(key) ?? null));
90
+ });
91
+ },
92
+ async flush() {
93
+ await Promise.all([...writes]);
43
94
  },
44
95
  /** Stops following other contexts' writes. */
45
96
  close: () => chrome.storage?.onChanged.removeListener(onChanged),
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The content script rules (decision P20). A content script shares the host page's document, so
2
+ * The content script rules. A content script shares the host page's document, so
3
3
  * RUM there records only what is the extension's own: actions inside the extension's UI root,
4
4
  * errors from the extension's own scripts, and one fixed view whose span names the host only by
5
5
  * url.domain.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The extension session store (decision P20): the session record in chrome.storage.session, which
2
+ * The extension session store: the session record in chrome.storage.session, which
3
3
  * every context of the extension shares and the browser clears when it closes, and
4
4
  * storage.onChanged, which tells the other contexts of a save. Records are the web store's version
5
5
  * 1 JSON plus w, a token naming the store that wrote it, so a store ignores its own saves when the
@@ -44,6 +44,8 @@ export function chromeSessionStore(chrome) {
44
44
  for (const l of listeners)
45
45
  l(s);
46
46
  };
47
+ /** Saves not yet in storage. */
48
+ const writes = new Set();
47
49
  let listening = false;
48
50
  const listen = () => {
49
51
  if (listening)
@@ -73,12 +75,20 @@ export function chromeSessionStore(chrome) {
73
75
  const write = state === undefined
74
76
  ? area.remove(SESSION_STORAGE_KEY)
75
77
  : area.set({ [SESSION_STORAGE_KEY]: { v: 1, ...state, w: token } });
76
- write.catch((err) => failed("write", err));
78
+ const done = write
79
+ .catch((err) => failed("write", err))
80
+ .then(() => {
81
+ writes.delete(done);
82
+ });
83
+ writes.add(done);
77
84
  }
78
85
  catch (err) {
79
86
  failed("write", err);
80
87
  }
81
88
  },
89
+ async flush() {
90
+ await Promise.all([...writes]);
91
+ },
82
92
  onChange(listener) {
83
93
  listen();
84
94
  listeners.add(listener);
@@ -1,6 +1,6 @@
1
1
  import type { ContentScriptSetupOptions, ContentScriptTelemetry, WorkerSetupOptions, WorkerTelemetry } from "@libo11y/webext";
2
2
  import { type ContentDeps, type WorkerDeps } from "@libo11y/webext/internal";
3
- import type { WebRumBind } from "../bind.ts";
3
+ import { type WebRumBind } from "../bind.ts";
4
4
  import type { Rum, RumOptions } from "../options.ts";
5
5
  import { type RumDeps } from "../setup.ts";
6
6
  import { type RumChromeLike } from "./chrome.ts";
@@ -32,7 +32,10 @@ export interface RumExtensionDeps {
32
32
  readonly chrome: RumChromeLike | undefined;
33
33
  readonly content: ContentDeps;
34
34
  /** RUM's dependencies. The platform, binding, session store and cache storage are replaced, and in a content script the retention queue. */
35
- readonly rum: RumDeps;
35
+ readonly rum: Omit<RumDeps, "web" | "sessionStore"> & {
36
+ readonly web?: RumDeps["web"];
37
+ readonly sessionStore?: RumDeps["sessionStore"];
38
+ };
36
39
  /** Install the telemetry globally. Default true. */
37
40
  readonly install?: boolean;
38
41
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * RUM in Manifest V3 extensions (decision P20). setupRumExtension runs in extension pages (popups,
2
+ * RUM in Manifest V3 extensions. setupRumExtension runs in extension pages (popups,
3
3
  * options pages, side panels) and content scripts; setupRumWorker runs in the service worker.
4
4
  *
5
5
  * Both contexts of setupRumExtension bind through @libo11y/webext's content script setup, so their
@@ -24,7 +24,11 @@ import { O11yError } from "@libo11y/core";
24
24
  import { browserEnv, createExporterSet, createMemoryQueue } from "@libo11y/web/internal";
25
25
  import { defaultContentDeps, defaultWorkerDeps, setupContentScriptWith, setupWorkerWith, } from "@libo11y/webext/internal";
26
26
  import { diag } from "@opentelemetry/api";
27
- import { defaultRumDeps, setupRumWith } from "../setup.js";
27
+ import { webLifecycle } from "../bind.js";
28
+ import { takeBootstrap } from "../bootstrap/buffer.js";
29
+ import { replayBootstrap } from "../bootstrap/replay.js";
30
+ import { setupRumCore } from "../core-setup.js";
31
+ import { defaultBrowserRumDeps } from "../setup.js";
28
32
  import { gatewayTransportOptions } from "../wire.js";
29
33
  import { getRumChrome } from "./chrome.js";
30
34
  import { chromeConfigStorage } from "./config-storage.js";
@@ -40,7 +44,7 @@ const HOST_PAGE_COLLECTORS = new Set([
40
44
  ]);
41
45
  /** The real dependencies of this context. */
42
46
  export function defaultRumExtensionDeps() {
43
- return { chrome: getRumChrome(), content: defaultContentDeps(), rum: defaultRumDeps() };
47
+ return { chrome: getRumChrome(), content: defaultContentDeps(), rum: defaultBrowserRumDeps() };
44
48
  }
45
49
  /** The binding of extension contexts: @libo11y/webext's setupContentScriptWith with RUM's wrappers. */
46
50
  export function contentBinding(content, install) {
@@ -52,10 +56,12 @@ export function contentBinding(content, install) {
52
56
  const platform = content.webPlatform(po);
53
57
  return {
54
58
  ...platform,
55
- // Logs are routed by session; app metrics export with delta temporality for the gateway.
59
+ // Logs are routed by session; app metrics export with delta temporality for the gateway,
60
+ // behind the consent boundary.
56
61
  buildExporters: (req) => w.exporterSet(createExporterSet(req, po.delivery, po.endpoints, {
57
62
  ...(po.mode === undefined ? {} : { mode: po.mode }),
58
63
  metricTemporality: "delta",
64
+ gate: po.gate,
59
65
  })),
60
66
  };
61
67
  },
@@ -108,13 +114,20 @@ export async function setupRumExtensionWith(options, deps) {
108
114
  let cache;
109
115
  let shutDown = false;
110
116
  const extensionBase = chrome.runtime.getURL?.("") ?? `chrome-extension://${chrome.runtime.id}/`;
111
- const rumDeps = {
117
+ const viewName = content?.viewName ?? CONTENT_VIEW_NAME;
118
+ const rumOptions = inContent ? { ...rum, viewName: () => viewName } : rum;
119
+ const t = await setupRumCore({
120
+ ...base,
121
+ ...(inContent ? { instrumentations: { ...base.instrumentations, documentLoad: false } } : {}),
122
+ }, rumOptions, {
112
123
  ...deps.rum,
113
124
  platform: "webext",
114
125
  bind: contentBinding(deps.content, deps.install !== false),
126
+ lifecycle: deps.rum.lifecycle ?? webLifecycle(deps.rum.document, deps.rum.window),
115
127
  sessionStore: () => chromeSessionStore(chrome),
116
128
  storage: async () => {
117
- const opened = cache ?? (await chromeConfigStorage(chrome));
129
+ // A content script's navigator.locks is the host page's, so it updates without a lock.
130
+ const opened = cache ?? (await chromeConfigStorage(chrome, inContent ? null : undefined));
118
131
  // A cache that finished loading after shutdown stops listening at once.
119
132
  if (shutDown)
120
133
  opened?.close();
@@ -122,6 +135,10 @@ export async function setupRumExtensionWith(options, deps) {
122
135
  cache = opened;
123
136
  return opened;
124
137
  },
138
+ bootstrap: () => {
139
+ const early = takeBootstrap();
140
+ return (runtime) => replayBootstrap(runtime, early);
141
+ },
125
142
  ...(inContent
126
143
  ? {
127
144
  openRetentionQueue: async (o) => createMemoryQueue(o),
@@ -133,22 +150,16 @@ export async function setupRumExtensionWith(options, deps) {
133
150
  }),
134
151
  }
135
152
  : {}),
136
- };
137
- const viewName = content?.viewName ?? CONTENT_VIEW_NAME;
138
- const rumOptions = inContent ? { ...rum, viewName: () => viewName } : rum;
139
- const t = await setupRumWith({
140
- ...base,
141
- ...(inContent ? { instrumentations: { ...base.instrumentations, documentLoad: false } } : {}),
142
- ...(rumOptions === undefined ? {} : { rum: rumOptions }),
143
- }, rumDeps);
153
+ });
144
154
  const handle = t;
145
155
  const rumShutdown = handle.shutdown.bind(handle);
146
- handle.shutdown = async (opts) => {
156
+ let shutdownPromise;
157
+ handle.shutdown = (opts) => (shutdownPromise ??= (async () => {
147
158
  shutDown = true;
148
159
  const report = await rumShutdown(opts);
149
160
  cache?.close();
150
161
  return report;
151
- };
162
+ })());
152
163
  return handle;
153
164
  }
154
165
  /**
@@ -184,6 +195,8 @@ export async function setupRumWorkerWith(options, deps) {
184
195
  buildExporters: (req) => createExporterSet(req, po.delivery, po.endpoints, {
185
196
  ...(po.mode === undefined ? {} : { mode: po.mode }),
186
197
  metricTemporality: "delta",
198
+ // The consent boundary: nothing measured before a revoke is exported after it.
199
+ gate: po.gate,
187
200
  }),
188
201
  };
189
202
  },
package/dist/webext.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * allowRumSessionInContentScripts for a worker set up with @libo11y/webext's setupWorker directly.
5
5
  * The collectors come from @libo11y/rum; the types its options and handle name are re-exported here.
6
6
  */
7
- export type { AddErrorOptions, BootstrapEntry, CollectorName, EventBody, RemoteConfig, RemoteConfigClient, RemoteConfigConnection, RemoteConfigDocument, RemoteConfigOpenOptions, RemoteConfigTimers, RequestStartHook, Rum, RumBus, RumCollector, RumOptions, RumRuntime, RumScope, RumSettings, RumSignal, SessionDecision, SessionEvent, SessionManager, SessionState, SummaryAccumulator, SummaryCountKey, SummaryView, ViewEvent, ViewStartOptions, ViewState, ViewTracker, VitalName, } from "./index.ts";
7
+ export type { AddErrorOptions, BootstrapEntry, CollectorName, EventBody, RemoteConfig, RemoteConfigClient, RemoteConfigConnection, RemoteConfigDocument, RemoteConfigOpenOptions, RemoteConfigStorage, RemoteConfigTimers, RequestStartHook, Rum, RumBus, RumCollector, RumOptions, RumRuntime, RumScope, RumSettings, RumSignal, SessionDecision, SessionEvent, SessionManager, SessionState, SummaryAccumulator, SummaryCountKey, SummaryView, ViewEvent, ViewStartOptions, ViewState, ViewTracker, VitalName, } from "./index.ts";
8
8
  export type { ChromeSessionStorageLike, RumChromeLike, StorageAreaLike, StorageChangeListener, StorageChanges, } from "./webext/chrome.ts";
9
9
  export { allowRumSessionInContentScripts } from "./webext/session-access.ts";
10
10
  export { CONTENT_VIEW_NAME, type RumExtensionOptions, type RumExtensionTelemetry, type RumWorkerSetupOptions, setupRumExtension, setupRumWorker, } from "./webext/setup.ts";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libo11y/rum",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "libo11y RUM for browsers and MV3 extensions: sessions shared across tabs, consent, session sampling with a retention buffer, views, actions, frustration signals, errors with breadcrumbs, web vitals with attribution, resources and per-view summaries over @libo11y/web.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -29,22 +29,25 @@
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "git+https://github.com/jamesainslie/libo11y.git",
32
+ "url": "git+https://github.com/libo11y/libo11y.git",
33
33
  "directory": "ts/packages/rum"
34
34
  },
35
35
  "dependencies": {
36
- "@libo11y/core": "0.4.0",
37
- "@libo11y/spec": "0.4.0",
38
- "@libo11y/web": "0.4.0",
39
- "@libo11y/webext": "0.4.0",
40
- "@opentelemetry/api": "1.10.0",
36
+ "@libo11y/core": "0.6.0",
37
+ "@libo11y/spec": "0.6.0",
38
+ "@libo11y/web": "0.6.0",
39
+ "@libo11y/webext": "0.6.0",
41
40
  "@opentelemetry/core": "3.1.1",
42
41
  "@opentelemetry/sdk-logs": "3.1.1",
43
42
  "@opentelemetry/sdk-trace": "3.1.1",
44
43
  "web-vitals": "5.3.0"
45
44
  },
45
+ "peerDependencies": {
46
+ "@opentelemetry/api": ">=1.10.0 <1.11.0"
47
+ },
46
48
  "devDependencies": {
47
- "@libo11y/test": "0.4.0",
49
+ "@libo11y/test": "0.6.0",
50
+ "@opentelemetry/api": "1.10.0",
48
51
  "fake-indexeddb": "6.2.7"
49
52
  },
50
53
  "files": [