@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.
Files changed (129) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +461 -2
  3. package/dist/actions/collector.d.ts +29 -0
  4. package/dist/actions/collector.js +260 -0
  5. package/dist/actions/naming.d.ts +15 -0
  6. package/dist/actions/naming.js +65 -0
  7. package/dist/actions/record.d.ts +13 -0
  8. package/dist/actions/record.js +28 -0
  9. package/dist/actions/settle.d.ts +28 -0
  10. package/dist/actions/settle.js +53 -0
  11. package/dist/attrs.d.ts +10 -0
  12. package/dist/attrs.js +12 -0
  13. package/dist/bind.d.ts +23 -0
  14. package/dist/bind.js +55 -0
  15. package/dist/bootstrap/buffer.d.ts +72 -0
  16. package/dist/bootstrap/buffer.js +95 -0
  17. package/dist/bootstrap/replay.d.ts +10 -0
  18. package/dist/bootstrap/replay.js +11 -0
  19. package/dist/bootstrap.d.ts +17 -0
  20. package/dist/bootstrap.js +33 -0
  21. package/dist/bus.d.ts +3 -0
  22. package/dist/bus.js +21 -0
  23. package/dist/config/clamp.d.ts +34 -0
  24. package/dist/config/clamp.js +79 -0
  25. package/dist/config/remote.d.ts +80 -0
  26. package/dist/config/remote.js +209 -0
  27. package/dist/context-manager.d.ts +8 -0
  28. package/dist/context-manager.js +29 -0
  29. package/dist/contracts/binding.d.ts +80 -0
  30. package/dist/contracts/binding.js +1 -0
  31. package/dist/contracts/retention.d.ts +28 -0
  32. package/dist/contracts/retention.js +1 -0
  33. package/dist/contracts/runtime.d.ts +154 -0
  34. package/dist/contracts/runtime.js +1 -0
  35. package/dist/contracts/session.d.ts +63 -0
  36. package/dist/contracts/session.js +1 -0
  37. package/dist/contracts/summary.d.ts +28 -0
  38. package/dist/contracts/summary.js +1 -0
  39. package/dist/contracts/views.d.ts +71 -0
  40. package/dist/contracts/views.js +1 -0
  41. package/dist/core-setup.d.ts +86 -0
  42. package/dist/core-setup.js +902 -0
  43. package/dist/core.d.ts +42 -0
  44. package/dist/core.js +35 -0
  45. package/dist/errors/breadcrumbs.d.ts +16 -0
  46. package/dist/errors/breadcrumbs.js +27 -0
  47. package/dist/errors/collector.d.ts +17 -0
  48. package/dist/errors/collector.js +186 -0
  49. package/dist/errors/normalize.d.ts +7 -0
  50. package/dist/errors/normalize.js +31 -0
  51. package/dist/errors/record.d.ts +20 -0
  52. package/dist/errors/record.js +46 -0
  53. package/dist/frustration/detect.d.ts +38 -0
  54. package/dist/frustration/detect.js +62 -0
  55. package/dist/ids.d.ts +2 -0
  56. package/dist/ids.js +2 -0
  57. package/dist/index.d.ts +19 -0
  58. package/dist/index.js +12 -0
  59. package/dist/internal.d.ts +28 -0
  60. package/dist/internal.js +26 -0
  61. package/dist/loaf.d.ts +15 -0
  62. package/dist/loaf.js +27 -0
  63. package/dist/options.d.ts +84 -0
  64. package/dist/options.js +1 -0
  65. package/dist/pending-spans.d.ts +29 -0
  66. package/dist/pending-spans.js +92 -0
  67. package/dist/query.d.ts +2 -0
  68. package/dist/query.js +19 -0
  69. package/dist/resources/collector.d.ts +76 -0
  70. package/dist/resources/collector.js +270 -0
  71. package/dist/resources/server-timing.d.ts +18 -0
  72. package/dist/resources/server-timing.js +21 -0
  73. package/dist/retention/buffer.d.ts +25 -0
  74. package/dist/retention/buffer.js +104 -0
  75. package/dist/retention/exporters.d.ts +17 -0
  76. package/dist/retention/exporters.js +67 -0
  77. package/dist/retention/sessions.d.ts +4 -0
  78. package/dist/retention/sessions.js +30 -0
  79. package/dist/session/local-store.d.ts +25 -0
  80. package/dist/session/local-store.js +91 -0
  81. package/dist/session/manager.d.ts +36 -0
  82. package/dist/session/manager.js +169 -0
  83. package/dist/session/sampling.d.ts +4 -0
  84. package/dist/session/sampling.js +20 -0
  85. package/dist/session/state.d.ts +4 -0
  86. package/dist/session/state.js +28 -0
  87. package/dist/settings.d.ts +8 -0
  88. package/dist/settings.js +36 -0
  89. package/dist/setup.d.ts +40 -0
  90. package/dist/setup.js +49 -0
  91. package/dist/span-processor.d.ts +38 -0
  92. package/dist/span-processor.js +125 -0
  93. package/dist/summary/accumulator.d.ts +25 -0
  94. package/dist/summary/accumulator.js +126 -0
  95. package/dist/version.d.ts +2 -0
  96. package/dist/version.js +2 -0
  97. package/dist/views/collector.d.ts +40 -0
  98. package/dist/views/collector.js +285 -0
  99. package/dist/views/history.d.ts +18 -0
  100. package/dist/views/history.js +42 -0
  101. package/dist/views/naming.d.ts +6 -0
  102. package/dist/views/naming.js +12 -0
  103. package/dist/views/paint.d.ts +12 -0
  104. package/dist/views/paint.js +15 -0
  105. package/dist/views/tracker.d.ts +28 -0
  106. package/dist/views/tracker.js +117 -0
  107. package/dist/vitals/attribution.d.ts +14 -0
  108. package/dist/vitals/attribution.js +60 -0
  109. package/dist/vitals/collector.d.ts +24 -0
  110. package/dist/vitals/collector.js +232 -0
  111. package/dist/vitals/lcp.d.ts +32 -0
  112. package/dist/vitals/lcp.js +49 -0
  113. package/dist/webext/chrome.d.ts +45 -0
  114. package/dist/webext/chrome.js +13 -0
  115. package/dist/webext/config-storage.d.ts +7 -0
  116. package/dist/webext/config-storage.js +47 -0
  117. package/dist/webext/scope.d.ts +22 -0
  118. package/dist/webext/scope.js +50 -0
  119. package/dist/webext/session-access.d.ts +13 -0
  120. package/dist/webext/session-access.js +9 -0
  121. package/dist/webext/session-store.d.ts +5 -0
  122. package/dist/webext/session-store.js +94 -0
  123. package/dist/webext/setup.d.ts +71 -0
  124. package/dist/webext/setup.js +193 -0
  125. package/dist/webext.d.ts +10 -0
  126. package/dist/webext.js +8 -0
  127. package/dist/wire.d.ts +13 -0
  128. package/dist/wire.js +30 -0
  129. package/package.json +57 -4
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Parents requests to the open action (decision P10). When the inner context manager has no active
3
+ * span, the open action's context is active, so fetch and XHR spans a click handler starts, even
4
+ * after an await the StackContextManager cannot follow, become the action's children. An action
5
+ * that has ended is never active.
6
+ */
7
+ import { type ContextManager, type Span } from "@opentelemetry/api";
8
+ export declare function rumContextManager(inner: ContextManager, action: () => Span | undefined): ContextManager;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Parents requests to the open action (decision P10). When the inner context manager has no active
3
+ * span, the open action's context is active, so fetch and XHR spans a click handler starts, even
4
+ * after an await the StackContextManager cannot follow, become the action's children. An action
5
+ * that has ended is never active.
6
+ */
7
+ import { trace } from "@opentelemetry/api";
8
+ export function rumContextManager(inner, action) {
9
+ const cm = {
10
+ active() {
11
+ const ctx = inner.active();
12
+ if (trace.getSpan(ctx) !== undefined)
13
+ return ctx;
14
+ const a = action();
15
+ return a === undefined || !a.isRecording() ? ctx : trace.setSpan(ctx, a);
16
+ },
17
+ with: (ctx, fn, thisArg, ...args) => inner.with(ctx, fn, thisArg, ...args),
18
+ bind: (ctx, target) => inner.bind(ctx, target),
19
+ enable() {
20
+ inner.enable();
21
+ return cm;
22
+ },
23
+ disable() {
24
+ inner.disable();
25
+ return cm;
26
+ },
27
+ };
28
+ return cm;
29
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Contract, R3f extended by R5a. A platform binding: what setupRumCore hands the platform it runs
3
+ * on, and what the platform reports back. The web binding (bind.ts) runs @libo11y/web's setup, the
4
+ * extension binding @libo11y/webext's content script setup. A binding runs nothing at module
5
+ * evaluation and needs no browser global of its own, so a platform without a DOM can supply one.
6
+ */
7
+ import type { ExporterSet, Telemetry } from "@libo11y/core";
8
+ import type { ConsentController, Delivery, HttpTransportOptions, SendMode, SignalEndpoints, UrlMatcher } from "@libo11y/web/portable";
9
+ import type { ContextManager } from "@opentelemetry/api";
10
+ import type { SpanExporter, SpanProcessor } from "@opentelemetry/sdk-trace";
11
+ import type { RequestStartHook } from "./runtime.ts";
12
+ /** The parts of a platform's setup options RUM reads. WebSetupOptions has every one. */
13
+ export interface RumPlatformOptions {
14
+ readonly hermetic?: boolean;
15
+ readonly spanExporter?: SpanExporter;
16
+ readonly spanProcessor?: SpanProcessor;
17
+ readonly contextManager?: () => ContextManager;
18
+ readonly transport?: {
19
+ readonly timeoutMillis?: number;
20
+ };
21
+ readonly instrumentations?: {
22
+ readonly ignoreUrls?: readonly UrlMatcher[];
23
+ };
24
+ }
25
+ /** What a platform reports once it built its delivery, before core builds anything. */
26
+ export interface RumPlatformInfo {
27
+ readonly endpoints: SignalEndpoints;
28
+ readonly delivery: Delivery;
29
+ /** The send mode, for the exporters' unload splitting. Default () => "normal". */
30
+ readonly mode?: () => SendMode;
31
+ }
32
+ /** What the RUM core hands a platform binding, to wrap the parts of the platform's setup RUM needs. */
33
+ export interface RumWrappers {
34
+ /** Takes the consent controller the platform built; RUM's gate. Returns the controller to use. */
35
+ readonly consentController: (c: ConsentController) => ConsentController;
36
+ /**
37
+ * Takes the platform's delivery and endpoints, and the OTEL_* variables it reports, before core
38
+ * builds anything, for RUM's span exporter and retention flush.
39
+ */
40
+ readonly platform: (info: RumPlatformInfo, env: Readonly<Record<string, string>>) => void;
41
+ /** Routes the platform's exporter set by session. Build it with delta metric temporality, as the gateway needs. */
42
+ readonly exporterSet: (set: ExporterSet) => ExporterSet;
43
+ /** RUM's hook, for the fetch and XHR instrumentation's onStart. */
44
+ readonly onRequestStart: RequestStartHook;
45
+ /** Adds the gateway wire parameters to an HTTP transport, for a binding that sends over HTTP. */
46
+ readonly transportOptions: (o: HttpTransportOptions) => HttpTransportOptions;
47
+ }
48
+ /**
49
+ * Runs a platform's setup with RUM's wrappers. options are the platform's own, with RUM's span
50
+ * processor, context manager and forwarding span exporter in place.
51
+ */
52
+ export type RumBind<O extends RumPlatformOptions, T extends Telemetry> = (options: O, wrappers: RumWrappers) => Promise<T>;
53
+ /**
54
+ * What RUM does on the platform's lifecycle signals. Each is called unbound, with any arguments
55
+ * ignored, so a binding may add one as an event listener directly.
56
+ */
57
+ export interface RumLifecycleHandlers {
58
+ /**
59
+ * The page or app is being hidden. One hide may signal more than once, as a page's pagehide and
60
+ * then visibilitychange: RUM runs its hide hooks on the first and sends what is pending on each.
61
+ */
62
+ readonly hide: () => void;
63
+ /** Visible again after a hide. */
64
+ readonly show: () => void;
65
+ /** A page hidden into the back/forward cache is back: its next hide is a new hide, even without a show. */
66
+ readonly restore: () => void;
67
+ }
68
+ /** When the page or app is on screen, as its platform reports it. RUM calls each member unbound. */
69
+ export interface RumLifecycle {
70
+ /** False while the page is hidden or the app is in the background. */
71
+ readonly visible: () => boolean;
72
+ /**
73
+ * Calls handlers on the platform's lifecycle signals. RUM subscribes before the binding runs the
74
+ * platform's setup, so RUM's hide work runs before a flush on hide the platform subscribes.
75
+ * Returns the function that unsubscribes.
76
+ */
77
+ readonly subscribe: (handlers: RumLifecycleHandlers) => () => void;
78
+ /** Calls listener on user activity, which keeps the session alive. Returns the function that unsubscribes. */
79
+ readonly activity?: (listener: () => void) => () => void;
80
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ /** Contract, frozen by R3a, extended by R3b. The retention buffer for sessions not yet kept. */
2
+ import type { Delivery, OtlpBatch, QueueSize } from "@libo11y/web/internal";
3
+ import type { SessionDecision } from "./session.ts";
4
+ export interface RetentionBuffer {
5
+ /** Stores a batch. Never rejects: a storage failure is reported through diag and the batch is lost. */
6
+ put(batch: OtlpBatch): Promise<void>;
7
+ /**
8
+ * Sends every stored batch that keep accepts (default every batch) through delivery, and removes
9
+ * every batch it claimed, sent or not. Resolves with the batches handed over. Never rejects.
10
+ */
11
+ flush(delivery: Delivery, keep?: (batch: OtlpBatch) => boolean): Promise<number>;
12
+ /** Removes every stored batch. Never rejects. */
13
+ clear(): Promise<void>;
14
+ /**
15
+ * Removes the stored batches match accepts, leaving the others claimable at once, so an ended
16
+ * unkept session removes its own records and no other tab's. Batches another context is sending
17
+ * are not seen. Resolves with the batches removed. Never rejects.
18
+ */
19
+ discard(match: (batch: OtlpBatch) => boolean): Promise<number>;
20
+ size(): Promise<QueueSize>;
21
+ close(): void;
22
+ }
23
+ /** What the retaining exporters ask. */
24
+ export interface RetentionRouting {
25
+ decide(sessionId: string | undefined): SessionDecision;
26
+ /** Hands a serialized batch of buffered records to the retention buffer. */
27
+ buffer(batch: OtlpBatch): void;
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Contract, frozen by R3a, extended by R3b, R3c, R5a and R6b. What every collector receives. Collectors are created by factories such
3
+ * as views() and run only while consent is granted: start is called on grant, the returned stop
4
+ * on revoke and shutdown.
5
+ */
6
+ import type { LogAttributes, RecordingGate, Telemetry } from "@libo11y/core";
7
+ import type { RUMErrorSource, RUMFrustrationType, RUMPlatform } from "@libo11y/spec";
8
+ import type { HttpRequestInfo } from "@libo11y/web";
9
+ import type { Attributes, Context, Span, Tracer } from "@opentelemetry/api";
10
+ import type { BootstrapEntry } from "../bootstrap/buffer.ts";
11
+ import type { RemoteConfigDocument } from "../config/clamp.ts";
12
+ import type { SessionManager } from "./session.ts";
13
+ import type { SummaryAccumulator } from "./summary.ts";
14
+ import type { ViewTracker } from "./views.ts";
15
+ export type CollectorName = "views" | "actions" | "errors" | "vitals" | "resources" | "long_animation_frames" | "replay";
16
+ /** Internal signals between collectors. at is performance.now(). */
17
+ export type RumSignal = {
18
+ readonly type: "error";
19
+ readonly source: RUMErrorSource;
20
+ readonly at: number;
21
+ } | {
22
+ readonly type: "frustration";
23
+ readonly frustration: RUMFrustrationType;
24
+ readonly selector?: string;
25
+ readonly at: number;
26
+ } | {
27
+ readonly type: "request-start";
28
+ readonly spanId: string;
29
+ readonly parentSpanId?: string;
30
+ readonly method: string;
31
+ readonly url: string;
32
+ readonly at: number;
33
+ } | {
34
+ readonly type: "request-end";
35
+ readonly spanId: string;
36
+ readonly status?: number;
37
+ /** The span's error.type when it ended without a response, such as AbortError or abort. */
38
+ readonly errorType?: string;
39
+ readonly at: number;
40
+ } | {
41
+ readonly type: "mutation";
42
+ readonly at: number;
43
+ } | {
44
+ readonly type: "breadcrumb";
45
+ readonly kind: string;
46
+ readonly label: string;
47
+ readonly at: number;
48
+ /** Lets a later rename relabel it: a view start carries its view ID. */
49
+ readonly key?: string;
50
+ }
51
+ /** An event the bootstrap buffered before RUM loaded, replayed once the collectors started (R3d). */
52
+ | {
53
+ readonly type: "bootstrap";
54
+ readonly entry: BootstrapEntry;
55
+ };
56
+ /** A log record body: the semconv browser.web_vital event carries its fields in one (R3c). */
57
+ export type EventBody = Readonly<Record<string, string | number>>;
58
+ export interface RumBus {
59
+ publish(signal: RumSignal): void;
60
+ subscribe(listener: (signal: RumSignal) => void): () => void;
61
+ }
62
+ export type RequestStartHook = (span: Span, request: HttpRequestInfo) => void;
63
+ /** The options and remote configuration after clamping. */
64
+ export interface RumSettings {
65
+ readonly sessionSampleRate: number;
66
+ readonly sessionIdleMillis: number;
67
+ readonly sessionMaxMillis: number;
68
+ readonly queryAllowList: ReadonlySet<string>;
69
+ /** Collectors the app installed and remote configuration left on. */
70
+ readonly enabled: ReadonlySet<CollectorName>;
71
+ readonly configVersion?: string;
72
+ readonly viewName: (url: URL) => string;
73
+ /** The app's public client token, sent as client_token on every gateway request (R6b). */
74
+ readonly clientToken?: string;
75
+ }
76
+ /**
77
+ * What a context limits the collectors to (R3f). Unset fields limit nothing. setupRumExtension sets
78
+ * them in a content script, whose document is the host page's.
79
+ */
80
+ export interface RumScope {
81
+ /** actions() records only the click, keydown and submit events this accepts. */
82
+ readonly action?: (event: Event) => boolean;
83
+ /** errors() records only the error, unhandledrejection and securitypolicyviolation events this accepts. */
84
+ readonly error?: (event: Event) => boolean;
85
+ /** views() follows no URL change, so the context keeps one view. */
86
+ readonly fixedView?: boolean;
87
+ /** Attributes every view span carries, such as url.domain in a content script. */
88
+ readonly viewAttributes?: Attributes;
89
+ }
90
+ export interface RumRuntime {
91
+ readonly platform: RUMPlatform;
92
+ /** The context's limits on what collectors record. Absent on the web. */
93
+ readonly scope?: RumScope;
94
+ readonly telemetry: Telemetry;
95
+ readonly tracer: Tracer;
96
+ readonly gate: RecordingGate;
97
+ readonly session: SessionManager;
98
+ readonly views: ViewTracker;
99
+ readonly summary: SummaryAccumulator;
100
+ readonly bus: RumBus;
101
+ settings(): RumSettings;
102
+ /** performance.now(). */
103
+ now(): number;
104
+ /**
105
+ * The epoch milliseconds now() counts from (performance.timeOrigin on the web, R5a), so a span
106
+ * placed at a now() time needs no performance global of its own.
107
+ */
108
+ readonly timeOrigin: number;
109
+ /**
110
+ * Emits event name with session.id and o11y.rum.view.id of the current session and view added
111
+ * (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.
113
+ */
114
+ emit(name: string, attrs?: LogAttributes, ctx?: Context, body?: EventBody): void;
115
+ /** Adds a hook run on every fetch and XHR client span right after it starts. */
116
+ onRequestStart(hook: RequestStartHook): () => void;
117
+ /** The open action's span, while one is open. Requests started meanwhile become its children (R3b). */
118
+ activeAction(): Span | undefined;
119
+ setActiveAction(span: Span | undefined): void;
120
+ /** The last breadcrumbs, oldest first, as "<kind> <label> -<age>ms" (R3b). */
121
+ breadcrumbs(): readonly string[];
122
+ /**
123
+ * 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.
125
+ */
126
+ ignored(url: string): boolean;
127
+ /**
128
+ * 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
130
+ * removes it.
131
+ */
132
+ onHide(hook: () => void): () => void;
133
+ /**
134
+ * The remote configuration document the current session started under, if any (R6b): the one
135
+ * settings() applies, for a collector that reads keys of its own, such as replay.
136
+ */
137
+ remoteDocument(): RemoteConfigDocument | undefined;
138
+ /**
139
+ * Adds attributes to every event of the given names that emit sends (R6b). attrs runs at each
140
+ * emit; undefined adds nothing, and the caller's attributes win on a conflict. Returns a
141
+ * function that removes it.
142
+ */
143
+ addEventAttributes(names: readonly string[], attrs: () => LogAttributes | undefined): () => void;
144
+ /**
145
+ * 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.
147
+ */
148
+ ignoreUrl(url: string): () => void;
149
+ }
150
+ export interface RumCollector {
151
+ readonly name: CollectorName;
152
+ /** Starts collecting. Returns the function that stops it. */
153
+ start(runtime: RumRuntime): () => void;
154
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Contract, frozen by R3a. The session: one per user visit, shared by every tab of an origin (and
3
+ * every context of an extension), ending after sessionIdleMillis without activity or after
4
+ * sessionMaxMillis.
5
+ */
6
+ import type { RUMSessionRetained } from "@libo11y/spec";
7
+ /** The shared session record, as stored. Times are epoch milliseconds. */
8
+ export interface SessionState {
9
+ readonly id: string;
10
+ readonly startedAt: number;
11
+ readonly lastActivityAt: number;
12
+ /** The head-sampling decision: sessionSampled(id, rate) at creation. */
13
+ readonly sampled: boolean;
14
+ /** Why the session is kept, once it is. head_sampled from creation when sampled. */
15
+ readonly retained?: RUMSessionRetained;
16
+ /** The remote configuration version the session started under. */
17
+ readonly configVersion?: string;
18
+ }
19
+ /** Where the session is shared. localStorage plus BroadcastChannel on the web, chrome.storage.session in extensions. */
20
+ export interface SessionStore {
21
+ /** Reads the stored session. Resolves undefined when there is none or it is unreadable. Never rejects. */
22
+ load(): Promise<SessionState | undefined>;
23
+ /** Stores state (undefined removes it) and tells the other contexts. Never throws. */
24
+ save(state: SessionState | undefined): void;
25
+ /** Calls listener when another context saves. Returns a function that unsubscribes. */
26
+ onChange(listener: (state: SessionState | undefined) => void): () => void;
27
+ close(): void;
28
+ }
29
+ /** What the retention router does with a record of a session. */
30
+ export type SessionDecision = "send" | "buffer" | "drop";
31
+ /** local is true when this context caused the change. */
32
+ export type SessionEvent = {
33
+ readonly type: "start";
34
+ readonly session: SessionState;
35
+ readonly previous?: SessionState;
36
+ readonly local: boolean;
37
+ } | {
38
+ readonly type: "end";
39
+ readonly session: SessionState;
40
+ readonly local: boolean;
41
+ } | {
42
+ readonly type: "kept";
43
+ readonly session: SessionState;
44
+ readonly local: boolean;
45
+ };
46
+ export interface SessionManager {
47
+ /** Loads the shared session and starts listening. Called when consent is granted. */
48
+ start(): Promise<void>;
49
+ /** The live session, rotating an expired one. Undefined before start and after clear. */
50
+ current(): SessionState | undefined;
51
+ /** User activity: extends the session, or rotates it when it expired. */
52
+ touch(): void;
53
+ /** Whether the current session is kept (head-sampled or retained). */
54
+ kept(): boolean;
55
+ /** Keeps the current session for reason. True when this call changed it from unkept to kept. */
56
+ retain(reason: "error" | "frustration"): boolean;
57
+ /** send for a kept session, buffer for the current unkept one, drop for an old unkept one. */
58
+ decide(sessionId: string | undefined): SessionDecision;
59
+ /** Forgets the session and removes it from the store (consent revoked). */
60
+ clear(): void;
61
+ subscribe(listener: (event: SessionEvent) => void): () => void;
62
+ close(): void;
63
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ /** Contract, frozen by R3a, extended by R3c. The per-view counters behind o11y.rum.view.summary. */
2
+ import type { RUMErrorSource, RUMFrustrationType, RUMViewKind } from "@libo11y/spec";
3
+ export type SummaryCountKey = `errors.${RUMErrorSource}` | `frustrations.${RUMFrustrationType}` | "requests" | "long_animation_frames";
4
+ export type VitalName = "lcp" | "inp" | "cls" | "fcp" | "ttfb";
5
+ export interface SummaryView {
6
+ readonly id: string;
7
+ readonly name: string;
8
+ readonly kind: RUMViewKind;
9
+ readonly sessionId: string;
10
+ }
11
+ export interface SummaryAccumulator {
12
+ /** Tracks view as the current view; its time on view runs while the page is visible. */
13
+ open(view: SummaryView): void;
14
+ /** Sends the view's summary (at least once) and stops its clock. Late vitals still reach it. */
15
+ close(viewId: string): void;
16
+ rename(viewId: string, name: string): void;
17
+ /** Adds n (default 1) to a count of the current view. */
18
+ count(key: SummaryCountKey, n?: number): void;
19
+ /** Records a vital of a view once; later values for the same view and vital are ignored. Seconds, CLS unitless. */
20
+ vital(viewId: string, name: VitalName, value: number): void;
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. */
23
+ pin(viewId: string, pinned: boolean): void;
24
+ /** Sends a summary for every view with unsent data. */
25
+ flush(): void;
26
+ /** Drops everything unsent (consent revoked). */
27
+ reset(): void;
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,71 @@
1
+ /** Contract, frozen by R3a, extended by R3b. Views: a page or route. performance.now() times. */
2
+ import type { RUMViewKind } from "@libo11y/spec";
3
+ import type { SpanContext } from "@opentelemetry/api";
4
+ export interface ViewState {
5
+ readonly id: string;
6
+ readonly name: string;
7
+ readonly kind: RUMViewKind;
8
+ /** performance.now() when the view started. */
9
+ readonly startTime: number;
10
+ /** The session the view belongs to. */
11
+ readonly sessionId: string;
12
+ /** The view span's context, once R3b's tracker has started it. */
13
+ readonly spanContext?: SpanContext;
14
+ }
15
+ export type ViewEvent = {
16
+ readonly type: "start";
17
+ readonly view: ViewState;
18
+ readonly previous?: ViewState;
19
+ /** The action that caused the view, from ViewStartOptions.cause. */
20
+ readonly cause?: SpanContext;
21
+ /** From ViewStartOptions.rendered: the content is already on screen. */
22
+ readonly rendered?: true;
23
+ } | {
24
+ readonly type: "rename";
25
+ readonly view: ViewState;
26
+ }
27
+ /** The routed content of the view rendered (rendered()). */
28
+ | {
29
+ readonly type: "rendered";
30
+ readonly view: ViewState;
31
+ /** performance.now() it rendered at, when that was before now (a render reported before consent). */
32
+ readonly at?: number;
33
+ } | {
34
+ readonly type: "end";
35
+ readonly view: ViewState;
36
+ };
37
+ export interface ViewStartOptions {
38
+ readonly kind: RUMViewKind;
39
+ /** Default: the tracker's namer applied to location. */
40
+ readonly name?: string;
41
+ /** Default now. */
42
+ readonly startTime?: number;
43
+ /** The action that caused the view, linked from the view span (R3b). */
44
+ readonly cause?: SpanContext;
45
+ /** The view's content is already on screen, as after a session change: its span ends at the next paint. */
46
+ readonly rendered?: boolean;
47
+ }
48
+ export interface ViewTracker {
49
+ current(): ViewState | undefined;
50
+ /** The view that loaded the page (initial, prerender, or the latest bfcache_restore). Vitals attach to it. */
51
+ loadView(): ViewState | undefined;
52
+ /** Ends the current view and starts another. */
53
+ start(opts: ViewStartOptions): ViewState;
54
+ rename(name: string): void;
55
+ /**
56
+ * The routed view rendered: emits rendered for the current view, whose span ends at the next
57
+ * paint. at (performance.now()) is when it rendered, for a render that happened earlier.
58
+ */
59
+ rendered(at?: number): void;
60
+ /** The span processor reports web's documentLoad span here, so the initial view span can be its child. */
61
+ appLoadStarted(ctx: SpanContext): void;
62
+ /** The context appLoadStarted reported, once it has. */
63
+ appLoad(): SpanContext | undefined;
64
+ /** Records the view span's context on the view, so actions on the view can link to it. */
65
+ attachSpan(viewId: string, ctx: SpanContext): void;
66
+ /** Starts the load view. Called once the session exists. */
67
+ begin(): void;
68
+ /** Ends the current view without starting another (hide is not an end; revoke and shutdown are). */
69
+ end(): void;
70
+ subscribe(listener: (event: ViewEvent) => void): () => void;
71
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,86 @@
1
+ /**
2
+ * setupRumCore: RUM over a platform binding (decision P1, R3f, R5a). In order: settings, the
3
+ * retention buffer (opened lazily), the session manager, the bus, the summary accumulator, the
4
+ * view tracker, the span processor, then the binding, which runs the platform's setup with RUM's
5
+ * wrappers. After setup: consent wiring (collectors run only while granted), session events, hide
6
+ * handling and activity from the platform's lifecycle. The returned handle is the platform's
7
+ * telemetry extended in place with rum. setupRumWith (setup.ts) binds it to the web.
8
+ *
9
+ * Nothing here imports a browser-only module or reads a browser global; every platform part is a
10
+ * dependency (core.test.ts enforces the imports), so a platform without a DOM, such as React
11
+ * Native, can bind it.
12
+ *
13
+ * The span processor reaches core as options.spanProcessor. Core treats an injected processor as
14
+ * the traces route ("injected") and then uses no span exporter the platform builds, so RUM also
15
+ * injects a forwarding span exporter. When the binding hands RUM its platform's delivery (before
16
+ * core builds anything), RUM builds the span exporter for the route traces would have had without
17
+ * RUM: none in hermetic mode, else OTEL_TRACES_EXPORTER from the platform's env. An app that
18
+ * injects its own spanExporter keeps it, routed by session; one that injects only a spanProcessor
19
+ * keeps sole charge of span export.
20
+ *
21
+ * 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.
24
+ */
25
+ import { type Telemetry } from "@libo11y/core";
26
+ import { type RUMPlatform } from "@libo11y/spec";
27
+ import { type PersistentQueue, type QueueOptions } from "@libo11y/web/portable";
28
+ import type { RumBind, RumLifecycle, RumPlatformOptions } from "./contracts/binding.ts";
29
+ import type { RumRuntime, RumScope } from "./contracts/runtime.ts";
30
+ import type { SessionStore } from "./contracts/session.ts";
31
+ import type { Rum, RumOptions } from "./options.ts";
32
+ /** What setupRumCore builds on: the platform's binding and lifecycle, and RUM's own dependencies. */
33
+ export interface RumCoreDeps<O extends RumPlatformOptions, T extends Telemetry> {
34
+ /** The platform summaries, session starts and app_load record. */
35
+ readonly platform: RUMPlatform;
36
+ /** Runs the platform's setup with RUM's wrappers. */
37
+ readonly bind: RumBind<O, T>;
38
+ /** When the page or app is shown, hidden and used. */
39
+ readonly lifecycle: RumLifecycle;
40
+ readonly openRetentionQueue: (opts: QueueOptions) => Promise<PersistentQueue>;
41
+ /** The session manager first uses the store when consent is granted, so it must open nothing before. */
42
+ readonly sessionStore: () => SessionStore;
43
+ /** Epoch milliseconds. */
44
+ readonly now: () => number;
45
+ /** performance.now(). */
46
+ readonly perfNow: () => number;
47
+ /**
48
+ * The epoch milliseconds perfNow counts from. A platform whose runtime may lack
49
+ * performance.timeOrigin, as React Native, passes its own. Unset where perfNow is a fake clock:
50
+ * RumRuntime.timeOrigin is then performance.timeOrigin, and request ends are read on perfNow alone.
51
+ */
52
+ readonly timeOrigin?: number;
53
+ /** The page URL, or the app's fixed URL where there is no page. */
54
+ readonly location: () => string;
55
+ readonly prerendered: () => boolean;
56
+ readonly connection: () => string | undefined;
57
+ /** Where pageshow fires, for the view tracker's back/forward-cache restores. Unset outside a page. */
58
+ readonly window?: EventTarget | undefined;
59
+ /** The fetch remote configuration uses. Default web's pristineFetch, which is never instrumented. */
60
+ readonly fetch?: typeof fetch;
61
+ /**
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.
64
+ */
65
+ readonly storage?: () => ConfigCacheStorage | undefined | Promise<ConfigCacheStorage | undefined>;
66
+ /** 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. */
67
+ readonly scope?: RumScope;
68
+ /**
69
+ * Takes what a bootstrap buffered before RUM loaded, at the end of setup. RUM replays it through
70
+ * the returned function when setup started with consent granted and no scope. Default none.
71
+ */
72
+ readonly bootstrap?: () => (runtime: RumRuntime) => void;
73
+ }
74
+ /** The synchronous storage remote configuration caches in. */
75
+ export type ConfigCacheStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;
76
+ /** The RUM runtime behind a RUM telemetry, for the adapter packages and the browser fixtures. */
77
+ export declare function runtimeOf(telemetry: object): RumRuntime | undefined;
78
+ /** How long a tab that ended a session waits to hear whether another tab's rotation won. */
79
+ export declare const SESSION_END_ELECTION_MILLIS = 500;
80
+ /**
81
+ * RUM over deps.bind: options are the platform's setup options, rumOptions RUM's own. Resolves with
82
+ * the platform's telemetry, with rum added.
83
+ */
84
+ export declare function setupRumCore<O extends RumPlatformOptions, T extends Telemetry>(options: O, rumOptions: RumOptions | undefined, deps: RumCoreDeps<O, T>): Promise<T & {
85
+ readonly rum: Rum;
86
+ }>;