@libo11y/rum 0.4.0 → 0.7.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 (68) 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 +30 -15
  22. package/dist/contracts/session.d.ts +12 -1
  23. package/dist/contracts/summary.d.ts +15 -4
  24. package/dist/contracts/views.d.ts +3 -3
  25. package/dist/core-setup.d.ts +43 -8
  26. package/dist/core-setup.js +300 -82
  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.d.ts +2 -2
  45. package/dist/session/manager.js +18 -2
  46. package/dist/session/sampling.js +1 -1
  47. package/dist/setup.d.ts +3 -1
  48. package/dist/setup.js +14 -6
  49. package/dist/span-processor.d.ts +3 -3
  50. package/dist/span-processor.js +2 -2
  51. package/dist/summary/accumulator.d.ts +11 -5
  52. package/dist/summary/accumulator.js +27 -6
  53. package/dist/version.d.ts +1 -1
  54. package/dist/version.js +1 -1
  55. package/dist/views/collector.js +8 -7
  56. package/dist/views/naming.d.ts +1 -1
  57. package/dist/views/naming.js +1 -1
  58. package/dist/views/tracker.d.ts +15 -6
  59. package/dist/views/tracker.js +10 -13
  60. package/dist/vitals/collector.js +2 -2
  61. package/dist/webext/config-storage.d.ts +15 -4
  62. package/dist/webext/config-storage.js +78 -27
  63. package/dist/webext/scope.d.ts +1 -1
  64. package/dist/webext/session-store.js +12 -2
  65. package/dist/webext/setup.d.ts +5 -2
  66. package/dist/webext/setup.js +29 -16
  67. package/dist/webext.d.ts +1 -1
  68. package/package.json +11 -8
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Contract, frozen by R3a, extended by R3b, R3c, R5a and R6b. What every collector receives. Collectors are created by factories such
2
+ * What every collector receives. Collectors are created by factories such
3
3
  * as views() and run only while consent is granted: start is called on grant, the returned stop
4
4
  * on revoke and shutdown.
5
5
  */
@@ -48,12 +48,16 @@ export type RumSignal = {
48
48
  /** Lets a later rename relabel it: a view start carries its view ID. */
49
49
  readonly key?: string;
50
50
  }
51
- /** An event the bootstrap buffered before RUM loaded, replayed once the collectors started (R3d). */
51
+ /** An event the bootstrap buffered before RUM loaded, replayed once the collectors started. */
52
52
  | {
53
53
  readonly type: "bootstrap";
54
54
  readonly entry: BootstrapEntry;
55
+ }
56
+ /** Remote configuration changed the current session's settings, as when its own document arrived. */
57
+ | {
58
+ readonly type: "config";
55
59
  };
56
- /** A log record body: the semconv browser.web_vital event carries its fields in one (R3c). */
60
+ /** A log record body: the semconv browser.web_vital event carries its fields in one. */
57
61
  export type EventBody = Readonly<Record<string, string | number>>;
58
62
  export interface RumBus {
59
63
  publish(signal: RumSignal): void;
@@ -70,11 +74,11 @@ export interface RumSettings {
70
74
  readonly enabled: ReadonlySet<CollectorName>;
71
75
  readonly configVersion?: string;
72
76
  readonly viewName: (url: URL) => string;
73
- /** The app's public client token, sent as client_token on every gateway request (R6b). */
77
+ /** The app's public client token, sent as client_token on every gateway request. */
74
78
  readonly clientToken?: string;
75
79
  }
76
80
  /**
77
- * What a context limits the collectors to (R3f). Unset fields limit nothing. setupRumExtension sets
81
+ * What a context limits the collectors to. Unset fields limit nothing. setupRumExtension sets
78
82
  * them in a content script, whose document is the host page's.
79
83
  */
80
84
  export interface RumScope {
@@ -102,48 +106,53 @@ export interface RumRuntime {
102
106
  /** performance.now(). */
103
107
  now(): number;
104
108
  /**
105
- * The epoch milliseconds now() counts from (performance.timeOrigin on the web, R5a), so a span
109
+ * The epoch milliseconds now() counts from (performance.timeOrigin on the web), so a span
106
110
  * placed at a now() time needs no performance global of its own.
107
111
  */
108
112
  readonly timeOrigin: number;
109
113
  /**
110
114
  * Emits event name with session.id and o11y.rum.view.id of the current session and view added
111
115
  * (attrs win). Dropped while the gate is closed. With body, the record carries it as its body
112
- * (R3c), for upstream events such as semconv's browser.web_vital that define one.
116
+ * for upstream events such as semconv's browser.web_vital that define one. time (epoch
117
+ * milliseconds) is when it happened, for an event of something earlier, such as a native crash a
118
+ * later launch delivers; default now. An event with a time gets nothing current added: no
119
+ * session, view or user, and no addEventAttributes attributes, so attrs carry its own. Returns
120
+ * whether the log export pipeline took the record: false while the gate is closed, or when it
121
+ * was dropped before export, as by a failing attrReplacer.
113
122
  */
114
- emit(name: string, attrs?: LogAttributes, ctx?: Context, body?: EventBody): void;
123
+ emit(name: string, attrs?: LogAttributes, ctx?: Context, body?: EventBody, time?: number): boolean;
115
124
  /** Adds a hook run on every fetch and XHR client span right after it starts. */
116
125
  onRequestStart(hook: RequestStartHook): () => void;
117
- /** The open action's span, while one is open. Requests started meanwhile become its children (R3b). */
126
+ /** The open action's span, while one is open. Requests started meanwhile become its children. */
118
127
  activeAction(): Span | undefined;
119
128
  setActiveAction(span: Span | undefined): void;
120
- /** The last breadcrumbs, oldest first, as "<kind> <label> -<age>ms" (R3b). */
129
+ /** The last breadcrumbs, oldest first, as "<kind> <label> -<age>ms". */
121
130
  breadcrumbs(): readonly string[];
122
131
  /**
123
132
  * Whether url is RUM's own traffic or one the app told the instrumentation to ignore: the OTLP
124
- * endpoints and instrumentations.ignoreUrls (R3c). Resource spans skip these.
133
+ * endpoints and instrumentations.ignoreUrls. Resource spans skip these.
125
134
  */
126
135
  ignored(url: string): boolean;
127
136
  /**
128
137
  * Adds a hook run when the page is hidden (the first of visibilitychange to hidden and pagehide,
129
- * once per hide), before the summaries are emitted and exported (R3c). Returns a function that
138
+ * once per hide), before the summaries are emitted and exported. Returns a function that
130
139
  * removes it.
131
140
  */
132
141
  onHide(hook: () => void): () => void;
133
142
  /**
134
- * The remote configuration document the current session started under, if any (R6b): the one
143
+ * The remote configuration document the current session started under, if any: the one
135
144
  * settings() applies, for a collector that reads keys of its own, such as replay.
136
145
  */
137
146
  remoteDocument(): RemoteConfigDocument | undefined;
138
147
  /**
139
- * Adds attributes to every event of the given names that emit sends (R6b). attrs runs at each
148
+ * Adds attributes to every event of the given names that emit sends. attrs runs at each
140
149
  * emit; undefined adds nothing, and the caller's attributes win on a conflict. Returns a
141
150
  * function that removes it.
142
151
  */
143
152
  addEventAttributes(names: readonly string[], attrs: () => LogAttributes | undefined): () => void;
144
153
  /**
145
154
  * Counts url as RUM's own traffic, whatever its query and fragment, so ignored is true for it,
146
- * as for the OTLP endpoints (R6b). Returns a function that removes it.
155
+ * as for the OTLP endpoints. Returns a function that removes it.
147
156
  */
148
157
  ignoreUrl(url: string): () => void;
149
158
  }
@@ -151,4 +160,10 @@ export interface RumCollector {
151
160
  readonly name: CollectorName;
152
161
  /** Starts collecting. Returns the function that stops it. */
153
162
  start(runtime: RumRuntime): () => void;
163
+ /**
164
+ * Removes what the collector stored, in this page or an earlier one. RUM calls it whenever it
165
+ * erases its own storage (a denied start, a revoke), whether or not the collector ever started.
166
+ * Optional.
167
+ */
168
+ readonly clear?: () => void | Promise<void>;
154
169
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Contract, frozen by R3a. The session: one per user visit, shared by every tab of an origin (and
2
+ * The session: one per user visit, shared by every tab of an origin (and
3
3
  * every context of an extension), ending after sessionIdleMillis without activity or after
4
4
  * sessionMaxMillis.
5
5
  */
@@ -22,6 +22,11 @@ export interface SessionStore {
22
22
  load(): Promise<SessionState | undefined>;
23
23
  /** Stores state (undefined removes it) and tells the other contexts. Never throws. */
24
24
  save(state: SessionState | undefined): void;
25
+ /**
26
+ * Resolves once every earlier save reached storage. Never rejects. A store that writes
27
+ * asynchronously, as chrome.storage.session does, has it; one that writes at once needs none.
28
+ */
29
+ flush?(): Promise<void>;
25
30
  /** Calls listener when another context saves. Returns a function that unsubscribes. */
26
31
  onChange(listener: (state: SessionState | undefined) => void): () => void;
27
32
  close(): void;
@@ -56,6 +61,12 @@ export interface SessionManager {
56
61
  retain(reason: "error" | "frustration"): boolean;
57
62
  /** send for a kept session, buffer for the current unkept one, drop for an old unkept one. */
58
63
  decide(sessionId: string | undefined): SessionDecision;
64
+ /**
65
+ * Tells decide whether an earlier process kept a session this context does not know, as a
66
+ * native report a later launch delivers says, so a record of that session keeps that decision.
67
+ * A session this context knows keeps its own.
68
+ */
69
+ recall(sessionId: string, kept: boolean): void;
59
70
  /** Forgets the session and removes it from the store (consent revoked). */
60
71
  clear(): void;
61
72
  subscribe(listener: (event: SessionEvent) => void): () => void;
@@ -1,4 +1,4 @@
1
- /** Contract, frozen by R3a, extended by R3c. The per-view counters behind o11y.rum.view.summary. */
1
+ /** The per-view counters behind o11y.rum.view.summary. */
2
2
  import type { RUMErrorSource, RUMFrustrationType, RUMViewKind } from "@libo11y/spec";
3
3
  export type SummaryCountKey = `errors.${RUMErrorSource}` | `frustrations.${RUMFrustrationType}` | "requests" | "long_animation_frames";
4
4
  export type VitalName = "lcp" | "inp" | "cls" | "fcp" | "ttfb";
@@ -19,10 +19,21 @@ export interface SummaryAccumulator {
19
19
  /** Records a vital of a view once; later values for the same view and vital are ignored. Seconds, CLS unitless. */
20
20
  vital(viewId: string, name: VitalName, value: number): void;
21
21
  visibility(visible: boolean): void;
22
- /** Pins a view (R3c): while pinned, a closed view is not forgotten, so vitals held for it still reach it. */
22
+ /** Pins a view: while pinned, a closed view is not forgotten, so vitals held for it still reach it. */
23
23
  pin(viewId: string, pinned: boolean): void;
24
- /** Sends a summary for every view with unsent data. */
25
- flush(): void;
24
+ /**
25
+ * Sends a summary for every view with unsent data. Returns false when the pipeline did not take
26
+ * one of them.
27
+ */
28
+ flush(): boolean;
26
29
  /** Drops everything unsent (consent revoked). */
27
30
  reset(): void;
31
+ /** The seq the next summary of a tracked view will carry, undefined for a view not tracked. */
32
+ nextSeq(viewId: string): number | undefined;
33
+ /**
34
+ * Sends one summary of a view an earlier process showed, as a later launch does for a native
35
+ * crash or hang it delivers: seq, time on view 0 and key counted once. The view is not tracked.
36
+ * Returns whether the pipeline took it.
37
+ */
38
+ late(view: SummaryView, seq: number, key: SummaryCountKey): boolean;
28
39
  }
@@ -1,4 +1,4 @@
1
- /** Contract, frozen by R3a, extended by R3b. Views: a page or route. performance.now() times. */
1
+ /** Views: a page or route. performance.now() times. */
2
2
  import type { RUMViewKind } from "@libo11y/spec";
3
3
  import type { SpanContext } from "@opentelemetry/api";
4
4
  export interface ViewState {
@@ -9,7 +9,7 @@ export interface ViewState {
9
9
  readonly startTime: number;
10
10
  /** The session the view belongs to. */
11
11
  readonly sessionId: string;
12
- /** The view span's context, once R3b's tracker has started it. */
12
+ /** The view span's context, once the view tracker has started it. */
13
13
  readonly spanContext?: SpanContext;
14
14
  }
15
15
  export type ViewEvent = {
@@ -40,7 +40,7 @@ export interface ViewStartOptions {
40
40
  readonly name?: string;
41
41
  /** Default now. */
42
42
  readonly startTime?: number;
43
- /** The action that caused the view, linked from the view span (R3b). */
43
+ /** The action that caused the view, linked from the view span. */
44
44
  readonly cause?: SpanContext;
45
45
  /** The view's content is already on screen, as after a session change: its span ends at the next paint. */
46
46
  readonly rendered?: boolean;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * setupRumCore: RUM over a platform binding (decision P1, R3f, R5a). In order: settings, the
2
+ * setupRumCore: RUM over a platform binding. In order: settings, the
3
3
  * retention buffer (opened lazily), the session manager, the bus, the summary accumulator, the
4
4
  * view tracker, the span processor, then the binding, which runs the platform's setup with RUM's
5
5
  * wrappers. After setup: consent wiring (collectors run only while granted), session events, hide
@@ -18,13 +18,26 @@
18
18
  * injects its own spanExporter keeps it, routed by session; one that injects only a spanProcessor
19
19
  * keeps sole charge of span export.
20
20
  *
21
+ * Only spans and log records routed to OTLP wait in the retention buffer, which sends through the
22
+ * platform's delivery. Those of the console route, and those of an exporter the app injected, wait
23
+ * in memory instead (holdingSpanExporter, holdingLogExporter) and go to their own exporter once
24
+ * their session is kept.
25
+ *
21
26
  * Consent. Closing the gate stops RUM at once, in the gate listener, so a grant that follows a
22
- * revoke before the revoke hooks finish starts RUM again. The revoke hook then empties the
23
- * retention buffer. A page that never had consent touches no RUM storage on revoke.
27
+ * revoke before the revoke hooks finish starts RUM again. The revoke hook then removes the stored
28
+ * session, the configuration cache and the retention buffer, and waits for stores that write
29
+ * asynchronously. A start that is denied (consent.initial, or a respected privacy signal) removes
30
+ * them too, before setup resolves, so a later grant never resumes what an earlier page stored. A
31
+ * pending start touches no RUM storage.
32
+ *
33
+ * OTEL_SDK_DISABLED=true (outside hermetic mode) disables RUM as core disables its providers: no
34
+ * session, collector, remote configuration or bootstrap replay starts, on a grant or otherwise. A
35
+ * denied start and a revoke still remove what an earlier page stored.
24
36
  */
25
37
  import { type Telemetry } from "@libo11y/core";
26
- import { type RUMPlatform } from "@libo11y/spec";
38
+ import { type RUMPlatform, type RUMViewKind } from "@libo11y/spec";
27
39
  import { type PersistentQueue, type QueueOptions } from "@libo11y/web/portable";
40
+ import { type RemoteConfigStorage } from "./config/remote.ts";
28
41
  import type { RumBind, RumLifecycle, RumPlatformOptions } from "./contracts/binding.ts";
29
42
  import type { RumRuntime, RumScope } from "./contracts/runtime.ts";
30
43
  import type { SessionStore } from "./contracts/session.ts";
@@ -59,8 +72,9 @@ export interface RumCoreDeps<O extends RumPlatformOptions, T extends Telemetry>
59
72
  /** The fetch remote configuration uses. Default web's pristineFetch, which is never instrumented. */
60
73
  readonly fetch?: typeof fetch;
61
74
  /**
62
- * Where remote configuration is cached. Default none. Called only once consent is granted. It
63
- * may resolve later, as a cache in chrome.storage.local does once it has read the stored value.
75
+ * Where remote configuration is cached. Default none. Called only once consent is granted, or to
76
+ * remove the cache when consent is denied. It may resolve later, as a cache in
77
+ * chrome.storage.local does once it has read the stored value.
64
78
  */
65
79
  readonly storage?: () => ConfigCacheStorage | undefined | Promise<ConfigCacheStorage | undefined>;
66
80
  /** The context's limits on the collectors, for a content script. With fixedView, the one view starts when RUM does and is on screen at once. */
@@ -70,9 +84,30 @@ export interface RumCoreDeps<O extends RumPlatformOptions, T extends Telemetry>
70
84
  * the returned function when setup started with consent granted and no scope. Default none.
71
85
  */
72
86
  readonly bootstrap?: () => (runtime: RumRuntime) => void;
87
+ /**
88
+ * The kind of every view, for a platform with one kind of view, such as React Native's screen:
89
+ * the first view is the load view, and rum.startView's views have it too. Default the web's.
90
+ */
91
+ readonly viewKind?: RUMViewKind;
92
+ /**
93
+ * performance.now() when the first view starts, for a platform whose clock does not start at the
94
+ * page's navigation, as React Native's counts from the device's boot. Default 0, navigation start.
95
+ */
96
+ readonly initialViewStart?: () => number;
97
+ /**
98
+ * Called once with the runtime, before consent is wired, so the platform's own recorders (React
99
+ * Native's app_load) see the first grant's session start and load view, even at setup.
100
+ */
101
+ readonly onRuntime?: (runtime: RumRuntime) => void;
73
102
  }
74
- /** The synchronous storage remote configuration caches in. */
75
- export type ConfigCacheStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;
103
+ /**
104
+ * The synchronous storage remote configuration caches in. A storage that writes asynchronously, as
105
+ * chrome.storage.local does, has flush, which resolves once every earlier write reached storage and
106
+ * never rejects.
107
+ */
108
+ export type ConfigCacheStorage = RemoteConfigStorage & {
109
+ flush?(): Promise<void>;
110
+ };
76
111
  /** The RUM runtime behind a RUM telemetry, for the adapter packages and the browser fixtures. */
77
112
  export declare function runtimeOf(telemetry: object): RumRuntime | undefined;
78
113
  /** How long a tab that ended a session waits to hear whether another tab's rotation won. */