@oodle-ai/mobile-sdk-reactnative 0.0.0-stage → 0.1.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 +201 -0
- package/OodleMobileSdkReactNative.podspec +24 -0
- package/README.md +191 -2
- package/android/build.gradle +55 -0
- package/android/proguard-rules.pro +5 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/ai/oodle/rum/Connectivity.kt +19 -0
- package/android/src/main/java/ai/oodle/rum/CrashReporter.kt +88 -0
- package/android/src/main/java/ai/oodle/rum/DeviceInfo.kt +50 -0
- package/android/src/main/java/ai/oodle/rum/ExitReasons.kt +104 -0
- package/android/src/main/java/ai/oodle/rum/NativeEvents.kt +24 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumModule.kt +96 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumPackage.kt +26 -0
- package/android/src/main/java/ai/oodle/rum/Storage.kt +84 -0
- package/android/src/main/java/ai/oodle/rum/Vitals.kt +188 -0
- package/android/src/main/java/ai/oodle/rum/Watchdog.kt +81 -0
- package/android/src/main/java/ai/oodle/rum/WireframeCapture.kt +398 -0
- package/bin/oodle-rum-upload.mjs +104 -0
- package/ios/OodleCrashReporter.h +16 -0
- package/ios/OodleCrashReporter.m +202 -0
- package/ios/OodleRum.h +18 -0
- package/ios/OodleRum.mm +129 -0
- package/ios/OodleRumImpl.swift +450 -0
- package/ios/WireframeWalker.swift +279 -0
- package/lib/core/clock.d.ts +19 -0
- package/lib/core/clock.js +47 -0
- package/lib/core/clock.js.map +1 -0
- package/lib/core/config.d.ts +142 -0
- package/lib/core/config.js +178 -0
- package/lib/core/config.js.map +1 -0
- package/lib/core/consent.d.ts +5 -0
- package/lib/core/consent.js +33 -0
- package/lib/core/consent.js.map +1 -0
- package/lib/core/context.d.ts +36 -0
- package/lib/core/context.js +172 -0
- package/lib/core/context.js.map +1 -0
- package/lib/core/flags.d.ts +3 -0
- package/lib/core/flags.js +20 -0
- package/lib/core/flags.js.map +1 -0
- package/lib/core/ids.d.ts +7 -0
- package/lib/core/ids.js +40 -0
- package/lib/core/ids.js.map +1 -0
- package/lib/core/internal.d.ts +6 -0
- package/lib/core/internal.js +10 -0
- package/lib/core/internal.js.map +1 -0
- package/lib/core/logger.d.ts +8 -0
- package/lib/core/logger.js +38 -0
- package/lib/core/logger.js.map +1 -0
- package/lib/core/native.d.ts +71 -0
- package/lib/core/native.js +156 -0
- package/lib/core/native.js.map +1 -0
- package/lib/core/rate-limiter.d.ts +7 -0
- package/lib/core/rate-limiter.js +31 -0
- package/lib/core/rate-limiter.js.map +1 -0
- package/lib/core/session.d.ts +51 -0
- package/lib/core/session.js +196 -0
- package/lib/core/session.js.map +1 -0
- package/lib/core/size.d.ts +15 -0
- package/lib/core/size.js +68 -0
- package/lib/core/size.js.map +1 -0
- package/lib/core/tags.d.ts +10 -0
- package/lib/core/tags.js +33 -0
- package/lib/core/tags.js.map +1 -0
- package/lib/core/telemetry.d.ts +6 -0
- package/lib/core/telemetry.js +37 -0
- package/lib/core/telemetry.js.map +1 -0
- package/lib/core/tracing.d.ts +9 -0
- package/lib/core/tracing.js +57 -0
- package/lib/core/tracing.js.map +1 -0
- package/lib/core/transport.d.ts +76 -0
- package/lib/core/transport.js +560 -0
- package/lib/core/transport.js.map +1 -0
- package/lib/core/user.d.ts +12 -0
- package/lib/core/user.js +41 -0
- package/lib/core/user.js.map +1 -0
- package/lib/events/actions.d.ts +13 -0
- package/lib/events/actions.js +147 -0
- package/lib/events/actions.js.map +1 -0
- package/lib/events/emit.d.ts +21 -0
- package/lib/events/emit.js +144 -0
- package/lib/events/emit.js.map +1 -0
- package/lib/events/errors.d.ts +19 -0
- package/lib/events/errors.js +257 -0
- package/lib/events/errors.js.map +1 -0
- package/lib/events/lifecycle.d.ts +7 -0
- package/lib/events/lifecycle.js +101 -0
- package/lib/events/lifecycle.js.map +1 -0
- package/lib/events/longtasks.d.ts +2 -0
- package/lib/events/longtasks.js +42 -0
- package/lib/events/longtasks.js.map +1 -0
- package/lib/events/resources.d.ts +15 -0
- package/lib/events/resources.js +156 -0
- package/lib/events/resources.js.map +1 -0
- package/lib/events/tap-target.d.ts +42 -0
- package/lib/events/tap-target.js +191 -0
- package/lib/events/tap-target.js.map +1 -0
- package/lib/events/view-state.d.ts +13 -0
- package/lib/events/view-state.js +25 -0
- package/lib/events/view-state.js.map +1 -0
- package/lib/events/views.d.ts +21 -0
- package/lib/events/views.js +347 -0
- package/lib/events/views.js.map +1 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +24 -0
- package/lib/index.js.map +1 -0
- package/lib/navigation/react-navigation.d.ts +33 -0
- package/lib/navigation/react-navigation.js +61 -0
- package/lib/navigation/react-navigation.js.map +1 -0
- package/lib/react/OodlePrivacy.d.ts +15 -0
- package/lib/react/OodlePrivacy.js +13 -0
- package/lib/react/OodlePrivacy.js.map +1 -0
- package/lib/react/OodleProvider.d.ts +18 -0
- package/lib/react/OodleProvider.js +72 -0
- package/lib/react/OodleProvider.js.map +1 -0
- package/lib/replay/recorder.d.ts +22 -0
- package/lib/replay/recorder.js +317 -0
- package/lib/replay/recorder.js.map +1 -0
- package/lib/replay/rrweb.d.ts +105 -0
- package/lib/replay/rrweb.js +335 -0
- package/lib/replay/rrweb.js.map +1 -0
- package/lib/rum.d.ts +52 -0
- package/lib/rum.js +281 -0
- package/lib/rum.js.map +1 -0
- package/lib/specs/NativeOodleRum.d.ts +43 -0
- package/lib/specs/NativeOodleRum.js +5 -0
- package/lib/specs/NativeOodleRum.js.map +1 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +6 -0
- package/lib/version.js.map +1 -0
- package/package.json +91 -4
- package/react-native.config.js +10 -0
- package/src/core/clock.ts +43 -0
- package/src/core/config.ts +344 -0
- package/src/core/consent.ts +32 -0
- package/src/core/context.ts +189 -0
- package/src/core/flags.ts +15 -0
- package/src/core/ids.ts +35 -0
- package/src/core/internal.ts +6 -0
- package/src/core/logger.ts +37 -0
- package/src/core/native.ts +201 -0
- package/src/core/rate-limiter.ts +34 -0
- package/src/core/session.ts +209 -0
- package/src/core/size.ts +79 -0
- package/src/core/tags.ts +30 -0
- package/src/core/telemetry.ts +35 -0
- package/src/core/tracing.ts +60 -0
- package/src/core/transport.ts +590 -0
- package/src/core/user.ts +45 -0
- package/src/events/actions.ts +157 -0
- package/src/events/emit.ts +131 -0
- package/src/events/errors.ts +283 -0
- package/src/events/lifecycle.ts +102 -0
- package/src/events/longtasks.ts +40 -0
- package/src/events/resources.ts +167 -0
- package/src/events/tap-target.ts +210 -0
- package/src/events/view-state.ts +30 -0
- package/src/events/views.ts +356 -0
- package/src/index.ts +34 -0
- package/src/navigation/react-navigation.ts +72 -0
- package/src/react/OodlePrivacy.tsx +25 -0
- package/src/react/OodleProvider.tsx +88 -0
- package/src/replay/recorder.ts +310 -0
- package/src/replay/rrweb.ts +460 -0
- package/src/rum.ts +295 -0
- package/src/specs/NativeOodleRum.ts +51 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Dimensions, Platform } from 'react-native';
|
|
2
|
+
import NativeOodleRum, { type Spec } from '../specs/NativeOodleRum';
|
|
3
|
+
import { logger } from './logger';
|
|
4
|
+
|
|
5
|
+
export interface DeviceInfo {
|
|
6
|
+
brand: string;
|
|
7
|
+
model: string;
|
|
8
|
+
osName: string;
|
|
9
|
+
osVersion: string;
|
|
10
|
+
appVersion: string;
|
|
11
|
+
appBuild: string;
|
|
12
|
+
bundleId: string;
|
|
13
|
+
deviceType: 'mobile' | 'tablet' | 'other';
|
|
14
|
+
screenWidth: number;
|
|
15
|
+
screenHeight: number;
|
|
16
|
+
scale: number;
|
|
17
|
+
locale: string;
|
|
18
|
+
/** Wall clock time the process started, or 0 when unknown. */
|
|
19
|
+
processStartMs: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface NativeVitals {
|
|
23
|
+
refresh_rate_avg?: number;
|
|
24
|
+
refresh_rate_min?: number;
|
|
25
|
+
slow_frame_count?: number;
|
|
26
|
+
frozen_frame_count?: number;
|
|
27
|
+
memory_avg_bytes?: number;
|
|
28
|
+
memory_max_bytes?: number;
|
|
29
|
+
cpu_ms_per_sec?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface NativeEvent {
|
|
33
|
+
type: 'anr' | 'app_hang' | 'memory_warning';
|
|
34
|
+
timestamp: number;
|
|
35
|
+
duration_ms?: number;
|
|
36
|
+
message?: string;
|
|
37
|
+
stack?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface NativeCrash {
|
|
41
|
+
timestamp: number;
|
|
42
|
+
type: string;
|
|
43
|
+
message: string;
|
|
44
|
+
stack: string;
|
|
45
|
+
/** Where the crash was seen: the JVM or iOS handlers, or the system exit record. */
|
|
46
|
+
source: 'android' | 'ios' | 'native' | 'anr';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const mod: Spec | null = NativeOodleRum ?? null;
|
|
50
|
+
|
|
51
|
+
let warned = false;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* True when the native module is linked. Expo Go, web and unit tests run
|
|
55
|
+
* without it; the SDK then works from JS alone, without replay, crash
|
|
56
|
+
* capture or vitals.
|
|
57
|
+
*/
|
|
58
|
+
export function hasNative(): boolean {
|
|
59
|
+
if (!mod && !warned) {
|
|
60
|
+
warned = true;
|
|
61
|
+
logger.warn(
|
|
62
|
+
'Native module OodleRum is not linked. Rebuild the app after ' +
|
|
63
|
+
'installing the package. Running without replay, crash capture ' +
|
|
64
|
+
'and vitals.',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return mod !== null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parse<T>(raw: string | null | undefined, fallback: T): T {
|
|
71
|
+
if (!raw) return fallback;
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(raw) as T;
|
|
74
|
+
} catch {
|
|
75
|
+
return fallback;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function call<T>(fn: (m: Spec) => T, fallback: T): T {
|
|
80
|
+
if (!mod) return fallback;
|
|
81
|
+
try {
|
|
82
|
+
return fn(mod);
|
|
83
|
+
} catch (e) {
|
|
84
|
+
logger.debug('native call failed', e);
|
|
85
|
+
return fallback;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function jsDeviceInfo(): DeviceInfo {
|
|
90
|
+
const { width, height, scale } = Dimensions.get('window');
|
|
91
|
+
const constants = (Platform as unknown as {
|
|
92
|
+
constants?: Record<string, unknown>;
|
|
93
|
+
}).constants;
|
|
94
|
+
const isPad = Boolean(
|
|
95
|
+
(Platform as unknown as { isPad?: boolean }).isPad,
|
|
96
|
+
);
|
|
97
|
+
return {
|
|
98
|
+
brand: String(constants?.Brand ?? constants?.Manufacturer ?? ''),
|
|
99
|
+
model: String(constants?.Model ?? ''),
|
|
100
|
+
osName: Platform.OS === 'ios' ? 'iOS' : Platform.OS === 'android' ? 'Android' : Platform.OS,
|
|
101
|
+
osVersion: String(Platform.Version ?? ''),
|
|
102
|
+
appVersion: '',
|
|
103
|
+
appBuild: '',
|
|
104
|
+
bundleId: '',
|
|
105
|
+
deviceType: isPad || Math.min(width, height) >= 600 ? 'tablet' : 'mobile',
|
|
106
|
+
screenWidth: width,
|
|
107
|
+
screenHeight: height,
|
|
108
|
+
scale,
|
|
109
|
+
locale: '',
|
|
110
|
+
processStartMs: 0,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let deviceInfo: DeviceInfo | null = null;
|
|
115
|
+
|
|
116
|
+
export function getDeviceInfo(): DeviceInfo {
|
|
117
|
+
if (deviceInfo) return deviceInfo;
|
|
118
|
+
const fallback = jsDeviceInfo();
|
|
119
|
+
const raw = call((m) => m.getDeviceInfo(), '');
|
|
120
|
+
deviceInfo = { ...fallback, ...parse<Partial<DeviceInfo>>(raw, {}) };
|
|
121
|
+
return deviceInfo;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const memory = new Map<string, string>();
|
|
125
|
+
|
|
126
|
+
export const storage = {
|
|
127
|
+
get(key: string): string | null {
|
|
128
|
+
if (!mod) return memory.get(key) ?? null;
|
|
129
|
+
return call((m) => m.storageGet(key), null);
|
|
130
|
+
},
|
|
131
|
+
set(key: string, value: string): boolean {
|
|
132
|
+
if (!mod) {
|
|
133
|
+
memory.set(key, value);
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return call((m) => m.storageSet(key, value), false);
|
|
137
|
+
},
|
|
138
|
+
/**
|
|
139
|
+
* Writes on a native background thread, for large values on paths that
|
|
140
|
+
* can wait. Falls back to a synchronous write.
|
|
141
|
+
*/
|
|
142
|
+
setAsync(key: string, value: string): void {
|
|
143
|
+
if (!mod) {
|
|
144
|
+
memory.set(key, value);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (typeof mod.storageSetAsync !== 'function') {
|
|
148
|
+
call((m) => m.storageSet(key, value), false);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
mod.storageSetAsync(key, value).catch(() => undefined);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
logger.debug('native call failed', e);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
remove(key: string): void {
|
|
158
|
+
if (!mod) {
|
|
159
|
+
memory.delete(key);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
call((m) => m.storageRemove(key), false);
|
|
163
|
+
},
|
|
164
|
+
getJSON<T>(key: string, fallback: T): T {
|
|
165
|
+
return parse(storage.get(key), fallback);
|
|
166
|
+
},
|
|
167
|
+
setJSON(key: string, value: unknown): boolean {
|
|
168
|
+
try {
|
|
169
|
+
return storage.set(key, JSON.stringify(value));
|
|
170
|
+
} catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export const native = {
|
|
177
|
+
enableCrashReporting: () => call((m) => m.enableCrashReporting(), false),
|
|
178
|
+
consumePendingCrash: (): NativeCrash | null =>
|
|
179
|
+
parse<NativeCrash | null>(call((m) => m.consumePendingCrash(), null), null),
|
|
180
|
+
startWatchdog: (ms: number) => call((m) => m.startWatchdog(ms), false),
|
|
181
|
+
drainNativeEvents: (): NativeEvent[] =>
|
|
182
|
+
parse<NativeEvent[]>(call((m) => m.drainNativeEvents(), '[]'), []),
|
|
183
|
+
startVitals: () => call((m) => m.startVitals(), false),
|
|
184
|
+
collectVitals: (): NativeVitals =>
|
|
185
|
+
parse<NativeVitals>(call((m) => m.collectVitals(), '{}'), {}),
|
|
186
|
+
getConnectivity: () => call((m) => m.getConnectivity(), 'unknown'),
|
|
187
|
+
captureWireframes(options: unknown): Promise<string> {
|
|
188
|
+
if (!mod) return Promise.reject(new Error('native module missing'));
|
|
189
|
+
try {
|
|
190
|
+
return mod.captureWireframes(JSON.stringify(options));
|
|
191
|
+
} catch (e) {
|
|
192
|
+
return Promise.reject(e);
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/** Test hook: forget the cached device info. */
|
|
198
|
+
export function resetNativeCache() {
|
|
199
|
+
deviceInfo = null;
|
|
200
|
+
memory.clear();
|
|
201
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const RATE_PER_SEC = 50;
|
|
2
|
+
const BURST = 200;
|
|
3
|
+
|
|
4
|
+
interface Bucket {
|
|
5
|
+
tokens: number;
|
|
6
|
+
lastRefill: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const buckets = new Map<string, Bucket>();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Token bucket per event type. A render loop that throws or logs on
|
|
13
|
+
* every frame must not flood the queue and push out the events that
|
|
14
|
+
* explain it.
|
|
15
|
+
*/
|
|
16
|
+
export function tryConsume(category: string): boolean {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
let b = buckets.get(category);
|
|
19
|
+
if (!b) {
|
|
20
|
+
b = { tokens: BURST, lastRefill: now };
|
|
21
|
+
buckets.set(category, b);
|
|
22
|
+
}
|
|
23
|
+
b.tokens = Math.min(BURST, b.tokens + ((now - b.lastRefill) / 1000) * RATE_PER_SEC);
|
|
24
|
+
b.lastRefill = now;
|
|
25
|
+
if (b.tokens >= 1) {
|
|
26
|
+
b.tokens--;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function resetBuckets() {
|
|
33
|
+
buckets.clear();
|
|
34
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { uuid } from './ids';
|
|
2
|
+
import { storage } from './native';
|
|
3
|
+
|
|
4
|
+
const SESSION_KEY = 'session';
|
|
5
|
+
/** A session ends after this much time with no activity. */
|
|
6
|
+
export const SESSION_TIMEOUT_MS = 15 * 60 * 1000;
|
|
7
|
+
/** A session never lasts longer than this. */
|
|
8
|
+
export const SESSION_MAX_DURATION_MS = 4 * 60 * 60 * 1000;
|
|
9
|
+
|
|
10
|
+
export interface SessionData {
|
|
11
|
+
id: string;
|
|
12
|
+
createdAt: number;
|
|
13
|
+
lastActivity: number;
|
|
14
|
+
viewCount: number;
|
|
15
|
+
errorCount: number;
|
|
16
|
+
actionCount: number;
|
|
17
|
+
sampled: boolean;
|
|
18
|
+
replaySampled: boolean;
|
|
19
|
+
replaySegmentSeq: number;
|
|
20
|
+
/** An event of this session was marked as its first. */
|
|
21
|
+
started: boolean;
|
|
22
|
+
/** The session this one followed (`session.previous_id`), if any. */
|
|
23
|
+
previousId?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let current: SessionData | null = null;
|
|
27
|
+
let loaded = false;
|
|
28
|
+
let sessionRate = 100;
|
|
29
|
+
let replayRate = 100;
|
|
30
|
+
let saveTimer: ReturnType<typeof setTimeout> | null = null;
|
|
31
|
+
const renewListeners: Array<(s: SessionData, previous: SessionData | null) => void> = [];
|
|
32
|
+
|
|
33
|
+
export function setSampleRates(session: number, replay: number) {
|
|
34
|
+
sessionRate = session;
|
|
35
|
+
replayRate = replay;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function roll(rate: number): boolean {
|
|
39
|
+
return Math.random() * 100 < rate;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function write() {
|
|
43
|
+
if (saveTimer) {
|
|
44
|
+
clearTimeout(saveTimer);
|
|
45
|
+
saveTimer = null;
|
|
46
|
+
}
|
|
47
|
+
if (current) storage.setJSON(SESSION_KEY, current);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function scheduleWrite() {
|
|
51
|
+
if (saveTimer) return;
|
|
52
|
+
saveTimer = setTimeout(() => {
|
|
53
|
+
saveTimer = null;
|
|
54
|
+
write();
|
|
55
|
+
}, 1000);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function load(): SessionData | null {
|
|
59
|
+
const raw = storage.getJSON<Partial<SessionData> | null>(SESSION_KEY, null);
|
|
60
|
+
if (!raw || typeof raw.id !== 'string') return null;
|
|
61
|
+
const now = Date.now();
|
|
62
|
+
return {
|
|
63
|
+
id: raw.id,
|
|
64
|
+
createdAt: raw.createdAt ?? now,
|
|
65
|
+
lastActivity: raw.lastActivity ?? now,
|
|
66
|
+
viewCount: raw.viewCount ?? 0,
|
|
67
|
+
errorCount: raw.errorCount ?? 0,
|
|
68
|
+
actionCount: raw.actionCount ?? 0,
|
|
69
|
+
sampled: raw.sampled ?? true,
|
|
70
|
+
replaySampled: raw.replaySampled ?? false,
|
|
71
|
+
replaySegmentSeq: raw.replaySegmentSeq ?? 0,
|
|
72
|
+
started: raw.started ?? true,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function expired(s: SessionData, now: number): boolean {
|
|
77
|
+
return (
|
|
78
|
+
now - s.lastActivity > SESSION_TIMEOUT_MS ||
|
|
79
|
+
now - s.createdAt > SESSION_MAX_DURATION_MS
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function onSessionRenew(
|
|
84
|
+
fn: (s: SessionData, previous: SessionData | null) => void,
|
|
85
|
+
) {
|
|
86
|
+
renewListeners.push(fn);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function renew(now: number): SessionData {
|
|
90
|
+
const previous = current;
|
|
91
|
+
const sampled = roll(sessionRate);
|
|
92
|
+
current = {
|
|
93
|
+
id: uuid(),
|
|
94
|
+
createdAt: now,
|
|
95
|
+
lastActivity: now,
|
|
96
|
+
viewCount: 0,
|
|
97
|
+
errorCount: 0,
|
|
98
|
+
actionCount: 0,
|
|
99
|
+
sampled,
|
|
100
|
+
replaySampled: sampled && roll(replayRate),
|
|
101
|
+
replaySegmentSeq: 0,
|
|
102
|
+
started: false,
|
|
103
|
+
previousId: previous?.id,
|
|
104
|
+
};
|
|
105
|
+
write();
|
|
106
|
+
for (const fn of renewListeners) {
|
|
107
|
+
try {
|
|
108
|
+
fn(current, previous);
|
|
109
|
+
} catch {
|
|
110
|
+
// A listener must not stop the session from starting.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return current;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Returns the live session, starting a new one when there is none or
|
|
118
|
+
* the last one ran out. `activity` marks user-visible work; background
|
|
119
|
+
* work such as periodic view updates passes false so it cannot keep an
|
|
120
|
+
* idle session alive.
|
|
121
|
+
*/
|
|
122
|
+
export function getSession(activity = true): SessionData {
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
if (!loaded) {
|
|
125
|
+
loaded = true;
|
|
126
|
+
current = load();
|
|
127
|
+
}
|
|
128
|
+
if (!current || expired(current, now)) return renew(now);
|
|
129
|
+
if (activity) {
|
|
130
|
+
current.lastActivity = now;
|
|
131
|
+
scheduleWrite();
|
|
132
|
+
}
|
|
133
|
+
return current;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function getSessionId(): string {
|
|
137
|
+
return getSession(false).id;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The session as it is now, without starting or renewing one. */
|
|
141
|
+
export function peekSession(): SessionData | null {
|
|
142
|
+
if (!loaded) {
|
|
143
|
+
loaded = true;
|
|
144
|
+
current = load();
|
|
145
|
+
}
|
|
146
|
+
return current;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** True when the stored session would be replaced by the next event. */
|
|
150
|
+
export function isSessionExpired(now = Date.now()): boolean {
|
|
151
|
+
const s = peekSession();
|
|
152
|
+
return !s || expired(s, now);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Ends the session. The next event starts a new one. */
|
|
156
|
+
export function stopSession() {
|
|
157
|
+
if (current) {
|
|
158
|
+
current.lastActivity = 0;
|
|
159
|
+
write();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Segment indices name the stored replay object, so they must never
|
|
165
|
+
* repeat within a session. The counter is persisted with the session
|
|
166
|
+
* and written through at once, because a lost increment would let the
|
|
167
|
+
* next app start overwrite a stored segment.
|
|
168
|
+
*/
|
|
169
|
+
export function nextReplaySegmentIndex(): number {
|
|
170
|
+
const s = getSession(false);
|
|
171
|
+
const index = s.replaySegmentSeq;
|
|
172
|
+
s.replaySegmentSeq = index + 1;
|
|
173
|
+
write();
|
|
174
|
+
return index;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* True once per session, for the event that is sent first. Readers count
|
|
179
|
+
* sessions from this flag, whatever kind of event comes first.
|
|
180
|
+
*/
|
|
181
|
+
export function claimSessionStart(sessionId: string): boolean {
|
|
182
|
+
const s = peekSession();
|
|
183
|
+
if (!s || s.id !== sessionId || s.started) return false;
|
|
184
|
+
s.started = true;
|
|
185
|
+
write();
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function incrementSessionCount(eventType: string) {
|
|
190
|
+
const s = getSession(false);
|
|
191
|
+
if (eventType === 'view') s.viewCount++;
|
|
192
|
+
else if (eventType === 'error') s.errorCount++;
|
|
193
|
+
else if (eventType === 'action') s.actionCount++;
|
|
194
|
+
else return;
|
|
195
|
+
scheduleWrite();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function flushSession() {
|
|
199
|
+
write();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Test hook. */
|
|
203
|
+
export function resetSessionState() {
|
|
204
|
+
if (saveTimer) clearTimeout(saveTimer);
|
|
205
|
+
saveTimer = null;
|
|
206
|
+
current = null;
|
|
207
|
+
loaded = false;
|
|
208
|
+
renewListeners.length = 0;
|
|
209
|
+
}
|
package/src/core/size.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const MAX_VISITED_NODES = 500_000;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Byte estimate for a JSON-serializable value.
|
|
5
|
+
*
|
|
6
|
+
* Walks the whole object graph rather than scoring a
|
|
7
|
+
* nested object as a fixed constant. rrweb full
|
|
8
|
+
* snapshots and mutation batches keep all of their
|
|
9
|
+
* weight below the top level, so a shallow estimate
|
|
10
|
+
* reports a multi-megabyte snapshot as ~100 bytes and
|
|
11
|
+
* every size guard built on it silently stops working.
|
|
12
|
+
*
|
|
13
|
+
* Callers only ever compare the result against a
|
|
14
|
+
* threshold, so pass `limit` to stop walking as soon
|
|
15
|
+
* as the value is known to be over it.
|
|
16
|
+
*/
|
|
17
|
+
export function estimateJsonBytes(
|
|
18
|
+
value: unknown,
|
|
19
|
+
limit = Number.POSITIVE_INFINITY,
|
|
20
|
+
): number {
|
|
21
|
+
let total = 0;
|
|
22
|
+
let visited = 0;
|
|
23
|
+
const stack: unknown[] = [value];
|
|
24
|
+
|
|
25
|
+
while (stack.length > 0) {
|
|
26
|
+
if (total >= limit) return total;
|
|
27
|
+
if (++visited > MAX_VISITED_NODES) return total;
|
|
28
|
+
|
|
29
|
+
const v = stack.pop();
|
|
30
|
+
if (v === null || v === undefined) {
|
|
31
|
+
total += 4;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
switch (typeof v) {
|
|
36
|
+
case 'string':
|
|
37
|
+
total += v.length + 2;
|
|
38
|
+
break;
|
|
39
|
+
case 'number':
|
|
40
|
+
total += 8;
|
|
41
|
+
break;
|
|
42
|
+
case 'boolean':
|
|
43
|
+
total += 5;
|
|
44
|
+
break;
|
|
45
|
+
case 'object': {
|
|
46
|
+
if (Array.isArray(v)) {
|
|
47
|
+
total += 2 + v.length;
|
|
48
|
+
for (let i = 0; i < v.length; i++) {
|
|
49
|
+
stack.push(v[i]);
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
total += 2;
|
|
53
|
+
for (const key in v as Record<
|
|
54
|
+
string,
|
|
55
|
+
unknown
|
|
56
|
+
>) {
|
|
57
|
+
if (
|
|
58
|
+
!Object.prototype.hasOwnProperty.call(
|
|
59
|
+
v,
|
|
60
|
+
key,
|
|
61
|
+
)
|
|
62
|
+
) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
total += key.length + 4;
|
|
66
|
+
stack.push(
|
|
67
|
+
(v as Record<string, unknown>)[key],
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return total;
|
|
79
|
+
}
|
package/src/core/tags.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { stringifyTags, type TagValue } from './config';
|
|
2
|
+
|
|
3
|
+
let tags: Record<string, string> = {};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Config tags are the base, not a reset: shared code can call setTags()
|
|
7
|
+
* before init() runs, and an explicit setTags() wins on a key both set.
|
|
8
|
+
*/
|
|
9
|
+
export function initTags(base: Record<string, string>) {
|
|
10
|
+
tags = { ...base, ...tags };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function setTags(next: Record<string, TagValue>) {
|
|
14
|
+
tags = { ...tags, ...stringifyTags(next) };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function removeTag(key: string) {
|
|
18
|
+
if (!(key in tags)) return;
|
|
19
|
+
const copy = { ...tags };
|
|
20
|
+
delete copy[key];
|
|
21
|
+
tags = copy;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getTags(): Record<string, string> {
|
|
25
|
+
return tags;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function resetTags() {
|
|
29
|
+
tags = {};
|
|
30
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const COUNTER_NAMES = [
|
|
2
|
+
'events_rate_limited',
|
|
3
|
+
'events_should_send_dropped',
|
|
4
|
+
'send_failures',
|
|
5
|
+
'compression_failures',
|
|
6
|
+
'retry_drops',
|
|
7
|
+
'transport_drops',
|
|
8
|
+
'replay_events_dropped',
|
|
9
|
+
'replay_capture_failures',
|
|
10
|
+
'replay_rebases',
|
|
11
|
+
'outbox_drops',
|
|
12
|
+
// A tap with no React fiber on its event: React internals changed, and
|
|
13
|
+
// taps are not named until the SDK is updated.
|
|
14
|
+
'tap_fiber_missing',
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
export type TelemetryCounter = (typeof COUNTER_NAMES)[number];
|
|
18
|
+
|
|
19
|
+
const counters = Object.fromEntries(
|
|
20
|
+
COUNTER_NAMES.map((n) => [n, 0]),
|
|
21
|
+
) as Record<TelemetryCounter, number>;
|
|
22
|
+
|
|
23
|
+
export function incrTelemetry(key: TelemetryCounter, amount = 1) {
|
|
24
|
+
counters[key] += amount;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Returns the counters since the last call, or null when all are zero. */
|
|
28
|
+
export function takeTelemetry(): Record<TelemetryCounter, number> | null {
|
|
29
|
+
let any = false;
|
|
30
|
+
for (const n of COUNTER_NAMES) if (counters[n] > 0) any = true;
|
|
31
|
+
if (!any) return null;
|
|
32
|
+
const snapshot = { ...counters };
|
|
33
|
+
for (const n of COUNTER_NAMES) counters[n] = 0;
|
|
34
|
+
return snapshot;
|
|
35
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Propagator } from './config';
|
|
2
|
+
import { hexId } from './ids';
|
|
3
|
+
|
|
4
|
+
export interface TraceIds {
|
|
5
|
+
traceId: string; // 32 hex chars
|
|
6
|
+
spanId: string; // 16 hex chars
|
|
7
|
+
sampled: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function newTraceIds(sampleRate: number): TraceIds {
|
|
11
|
+
return {
|
|
12
|
+
traceId: hexId(16),
|
|
13
|
+
spanId: hexId(8),
|
|
14
|
+
sampled: Math.random() * 100 < sampleRate,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function hexToDecimal(hex: string): string | null {
|
|
19
|
+
const big = (globalThis as unknown as { BigInt?: (v: string) => { toString(r?: number): string } })
|
|
20
|
+
.BigInt;
|
|
21
|
+
if (typeof big !== 'function') return null;
|
|
22
|
+
try {
|
|
23
|
+
return big('0x' + hex).toString(10);
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Header names in lower case, as the XHR tracker compares them. */
|
|
30
|
+
export function tracingHeaders(
|
|
31
|
+
ids: TraceIds,
|
|
32
|
+
propagators: Propagator[],
|
|
33
|
+
): Record<string, string> {
|
|
34
|
+
const out: Record<string, string> = {};
|
|
35
|
+
const flag = ids.sampled ? '01' : '00';
|
|
36
|
+
for (const p of propagators) {
|
|
37
|
+
if (p === 'tracecontext') {
|
|
38
|
+
out.traceparent = `00-${ids.traceId}-${ids.spanId}-${flag}`;
|
|
39
|
+
} else if (p === 'b3') {
|
|
40
|
+
out.b3 = `${ids.traceId}-${ids.spanId}-${ids.sampled ? '1' : '0'}`;
|
|
41
|
+
} else if (p === 'b3multi') {
|
|
42
|
+
out['x-b3-traceid'] = ids.traceId;
|
|
43
|
+
out['x-b3-spanid'] = ids.spanId;
|
|
44
|
+
out['x-b3-sampled'] = ids.sampled ? '1' : '0';
|
|
45
|
+
} else if (p === 'datadog') {
|
|
46
|
+
// Datadog ids are the low 64 bits in decimal; the high 64 bits go
|
|
47
|
+
// in x-datadog-tags.
|
|
48
|
+
const traceLow = hexToDecimal(ids.traceId.slice(16));
|
|
49
|
+
const parent = hexToDecimal(ids.spanId);
|
|
50
|
+
if (traceLow && parent) {
|
|
51
|
+
out['x-datadog-trace-id'] = traceLow;
|
|
52
|
+
out['x-datadog-parent-id'] = parent;
|
|
53
|
+
out['x-datadog-sampling-priority'] = ids.sampled ? '1' : '0';
|
|
54
|
+
out['x-datadog-origin'] = 'rum';
|
|
55
|
+
out['x-datadog-tags'] = `_dd.p.tid=${ids.traceId.slice(0, 16)}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|