@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,157 @@
|
|
|
1
|
+
import { getConfig } from '../core/config';
|
|
2
|
+
import { uuid } from '../core/ids';
|
|
3
|
+
import { getDeviceInfo } from '../core/native';
|
|
4
|
+
import { incrTelemetry } from '../core/telemetry';
|
|
5
|
+
import { attributesJSON, emit } from './emit';
|
|
6
|
+
import { fiberFromEvent, resolveTapTarget } from './tap-target';
|
|
7
|
+
import { getCurrentViewInfo } from './view-state';
|
|
8
|
+
|
|
9
|
+
/** Taps on one target within this window count toward a rage tap. */
|
|
10
|
+
const RAGE_WINDOW_MS = 1000;
|
|
11
|
+
const RAGE_TAPS = 3;
|
|
12
|
+
/** A touch that moves farther than this is a scroll or a swipe, not a tap. */
|
|
13
|
+
const TAP_SLOP_DP = 12;
|
|
14
|
+
const MAX_TAP_MS = 1500;
|
|
15
|
+
|
|
16
|
+
export function addAction(name: string, attributes?: Record<string, unknown>) {
|
|
17
|
+
if (!name) return;
|
|
18
|
+
const data: Record<string, unknown> = {
|
|
19
|
+
event_type: 'action',
|
|
20
|
+
action_id: uuid(),
|
|
21
|
+
action_name: name,
|
|
22
|
+
action_type: 'custom',
|
|
23
|
+
is_frustration: 0,
|
|
24
|
+
};
|
|
25
|
+
const attrs = attributesJSON(attributes);
|
|
26
|
+
if (attrs) data.event_attributes = attrs;
|
|
27
|
+
emit(data);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let recentTaps: Array<{ name: string; t: number }> = [];
|
|
31
|
+
|
|
32
|
+
function trackRage(name: string, at: number): boolean {
|
|
33
|
+
recentTaps = recentTaps.filter((x) => at - x.t <= RAGE_WINDOW_MS);
|
|
34
|
+
recentTaps.push({ name, t: at });
|
|
35
|
+
const same = recentTaps.filter((x) => x.name === name).length;
|
|
36
|
+
if (same >= RAGE_TAPS) {
|
|
37
|
+
recentTaps = recentTaps.filter((x) => x.name !== name);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface TouchPoint {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
t: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Called by OodleProvider when a touch ends close to where it started. */
|
|
50
|
+
export function recordTap(startEvent: unknown, start: TouchPoint, end: TouchPoint) {
|
|
51
|
+
const config = getConfig();
|
|
52
|
+
if (!config?.trackInteractions) return;
|
|
53
|
+
if (end.t - start.t > MAX_TAP_MS) return;
|
|
54
|
+
if (Math.hypot(end.x - start.x, end.y - start.y) > TAP_SLOP_DP) return;
|
|
55
|
+
let target;
|
|
56
|
+
try {
|
|
57
|
+
const fiber = fiberFromEvent(startEvent);
|
|
58
|
+
if (!fiber) {
|
|
59
|
+
incrTelemetry('tap_fiber_missing');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
target = resolveTapTarget(fiber, {
|
|
63
|
+
actionNameAttribute: config.actionNameAttribute,
|
|
64
|
+
useAccessibilityLabel: config.useAccessibilityLabel,
|
|
65
|
+
});
|
|
66
|
+
} catch {
|
|
67
|
+
target = null;
|
|
68
|
+
}
|
|
69
|
+
if (!target) return;
|
|
70
|
+
const d = getDeviceInfo();
|
|
71
|
+
const base = {
|
|
72
|
+
event_type: 'action',
|
|
73
|
+
app_widget_name: target.name,
|
|
74
|
+
app_widget_id: target.path,
|
|
75
|
+
action_text: target.label,
|
|
76
|
+
app_screen_coordinate_x: Math.round(end.x),
|
|
77
|
+
app_screen_coordinate_y: Math.round(end.y),
|
|
78
|
+
viewport_width: Math.round(d.screenWidth),
|
|
79
|
+
viewport_height: Math.round(d.screenHeight),
|
|
80
|
+
};
|
|
81
|
+
emit({ ...base, action_id: uuid(), action_name: target.name, action_type: 'tap', is_frustration: 0 });
|
|
82
|
+
const view = getCurrentViewInfo();
|
|
83
|
+
if (trackRage(`${view?.id ?? ''}:${target.name}`, end.t)) {
|
|
84
|
+
emit({
|
|
85
|
+
...base,
|
|
86
|
+
action_id: uuid(),
|
|
87
|
+
action_name: target.name,
|
|
88
|
+
action_type: 'rage_tap',
|
|
89
|
+
action_frustration_types: 'rage_click',
|
|
90
|
+
is_frustration: 1,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// --- Operations: a named piece of work that succeeds or fails ---
|
|
96
|
+
|
|
97
|
+
const operations = new Map<string, number>();
|
|
98
|
+
|
|
99
|
+
function opKey(name: string, key?: string) {
|
|
100
|
+
return `${name}\u0000${key ?? ''}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function startOperation(name: string, key?: string) {
|
|
104
|
+
if (!name) return;
|
|
105
|
+
operations.set(opKey(name, key), Date.now());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function endOperation(
|
|
109
|
+
name: string,
|
|
110
|
+
key: string | undefined,
|
|
111
|
+
status: 'succeeded' | 'failed',
|
|
112
|
+
failureReason?: string,
|
|
113
|
+
attributes?: Record<string, unknown>,
|
|
114
|
+
) {
|
|
115
|
+
const k = opKey(name, key);
|
|
116
|
+
const started = operations.get(k);
|
|
117
|
+
operations.delete(k);
|
|
118
|
+
const props: Record<string, unknown> = {
|
|
119
|
+
...(attributes ?? {}),
|
|
120
|
+
operation_key: key ?? '',
|
|
121
|
+
status,
|
|
122
|
+
};
|
|
123
|
+
if (started !== undefined) props.duration_ms = Date.now() - started;
|
|
124
|
+
if (failureReason) props.failure_reason = failureReason;
|
|
125
|
+
emit({
|
|
126
|
+
event_type: 'custom',
|
|
127
|
+
custom_event_name: name,
|
|
128
|
+
custom_event_properties: JSON.stringify(props),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function succeedOperation(name: string, key?: string, attributes?: Record<string, unknown>) {
|
|
133
|
+
endOperation(name, key, 'succeeded', undefined, attributes);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function failOperation(
|
|
137
|
+
name: string,
|
|
138
|
+
key?: string,
|
|
139
|
+
reason = 'error',
|
|
140
|
+
attributes?: Record<string, unknown>,
|
|
141
|
+
) {
|
|
142
|
+
endOperation(name, key, 'failed', reason, attributes);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function trackEvent(name: string, properties?: Record<string, unknown>) {
|
|
146
|
+
if (!name) return;
|
|
147
|
+
emit({
|
|
148
|
+
event_type: 'custom',
|
|
149
|
+
custom_event_name: name,
|
|
150
|
+
custom_event_properties: attributesJSON(properties),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function resetActions() {
|
|
155
|
+
recentTaps = [];
|
|
156
|
+
operations.clear();
|
|
157
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { getConfig } from '../core/config';
|
|
2
|
+
import { getConsent } from '../core/consent';
|
|
3
|
+
import { baseContext } from '../core/context';
|
|
4
|
+
import { uuid } from '../core/ids';
|
|
5
|
+
import { logger } from '../core/logger';
|
|
6
|
+
import { tryConsume } from '../core/rate-limiter';
|
|
7
|
+
import { claimSessionStart, getSession, incrementSessionCount, peekSession } from '../core/session';
|
|
8
|
+
import { incrTelemetry } from '../core/telemetry';
|
|
9
|
+
import {
|
|
10
|
+
EVENTS_BATCH_KEY,
|
|
11
|
+
enqueue,
|
|
12
|
+
isServerRateLimited,
|
|
13
|
+
upsert,
|
|
14
|
+
} from '../core/transport';
|
|
15
|
+
|
|
16
|
+
export type EventData = Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Types that a broken app can produce in a loop. Views and visibility
|
|
20
|
+
* changes are left out: they happen at human speed, and losing one
|
|
21
|
+
* breaks a pairing (a view with no end, a hide with no return).
|
|
22
|
+
*/
|
|
23
|
+
const RATE_LIMITED = new Set(['error', 'action', 'console', 'resource', 'long_task']);
|
|
24
|
+
|
|
25
|
+
/** Fields beforeSend cannot change: the event's identity. */
|
|
26
|
+
const FIXED_FIELDS = ['session_id', 'event_type', 'timestamp', 'log_record_uid', 'view_id'];
|
|
27
|
+
|
|
28
|
+
let viewCounter: ((data: EventData) => void) | null = null;
|
|
29
|
+
|
|
30
|
+
/** The views module counts each event in the view it happened in. */
|
|
31
|
+
export function setViewCounter(fn: (data: EventData) => void) {
|
|
32
|
+
viewCounter = fn;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function applyBeforeSend(event: EventData): EventData | null {
|
|
36
|
+
const hook = getConfig()?.beforeSend;
|
|
37
|
+
if (!hook) return event;
|
|
38
|
+
const fixed: EventData = {};
|
|
39
|
+
for (const f of FIXED_FIELDS) fixed[f] = event[f];
|
|
40
|
+
try {
|
|
41
|
+
const out = hook(event);
|
|
42
|
+
if (out === null) return null;
|
|
43
|
+
const base = out && typeof out === 'object' ? out : event;
|
|
44
|
+
return { ...base, ...fixed };
|
|
45
|
+
} catch (e) {
|
|
46
|
+
// The hook may exist to remove something that must not be sent, so
|
|
47
|
+
// an event whose hook throws is dropped rather than sent unchanged.
|
|
48
|
+
logger.warn('beforeSend threw; the event is dropped', e);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function canRecord(): boolean {
|
|
54
|
+
return getConfig() !== null && getConsent() !== 'not-granted';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function markSessionStart(event: EventData) {
|
|
58
|
+
if (typeof event.session_id === 'string' && claimSessionStart(event.session_id)) {
|
|
59
|
+
event.session_is_new = 1;
|
|
60
|
+
const previous = peekSession()?.previousId;
|
|
61
|
+
if (previous) event.session_previous_id = previous;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Builds an event on the current context and queues it. */
|
|
66
|
+
export function emit(data: EventData, activity = true) {
|
|
67
|
+
if (!canRecord()) return;
|
|
68
|
+
if (!getSession(activity).sampled) return;
|
|
69
|
+
if (isServerRateLimited('events')) return;
|
|
70
|
+
const type = String(data.event_type ?? '');
|
|
71
|
+
if (RATE_LIMITED.has(type)) {
|
|
72
|
+
if (isServerRateLimited(type) || !tryConsume(type)) {
|
|
73
|
+
incrTelemetry('events_rate_limited');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
incrementSessionCount(type);
|
|
78
|
+
viewCounter?.(data);
|
|
79
|
+
const built: EventData = { log_record_uid: uuid(), ...baseContext(activity), ...data };
|
|
80
|
+
markSessionStart(built);
|
|
81
|
+
const event = applyBeforeSend(built);
|
|
82
|
+
if (event) enqueue(EVENTS_BATCH_KEY, event);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Sends one update of a view. Readers keep the highest `view_update_seq`
|
|
87
|
+
* per `view_id`, and count the view from its first update. Unsent
|
|
88
|
+
* updates of one view collapse into the newest, which keeps the flags
|
|
89
|
+
* that count the view and the session.
|
|
90
|
+
*/
|
|
91
|
+
export function emitViewUpdate(data: EventData) {
|
|
92
|
+
if (!canRecord()) return;
|
|
93
|
+
const session = getSession(false);
|
|
94
|
+
if (!session.sampled) return;
|
|
95
|
+
if (isServerRateLimited('events')) return;
|
|
96
|
+
if (data.view_update_seq === 1) incrementSessionCount('view');
|
|
97
|
+
const built: EventData = { log_record_uid: uuid(), ...baseContext(false), ...data };
|
|
98
|
+
if (data.view_update_seq === 1) built.view_is_new = 1;
|
|
99
|
+
markSessionStart(built);
|
|
100
|
+
const event = applyBeforeSend(built);
|
|
101
|
+
if (!event) return;
|
|
102
|
+
upsert(EVENTS_BATCH_KEY, `view:${String(data.view_id)}`, event, (prev, next) => {
|
|
103
|
+
const merged = { ...next };
|
|
104
|
+
if (prev.view_is_new === 1) merged.view_is_new = 1;
|
|
105
|
+
if (prev.session_is_new === 1) merged.session_is_new = 1;
|
|
106
|
+
if (prev.session_previous_id) merged.session_previous_id = prev.session_previous_id;
|
|
107
|
+
return merged;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Queues an event that is already complete, such as a crash from the last
|
|
113
|
+
* run. It keeps its own session and time and does not touch the live
|
|
114
|
+
* session.
|
|
115
|
+
*/
|
|
116
|
+
export function emitRaw(event: EventData) {
|
|
117
|
+
if (!canRecord()) return;
|
|
118
|
+
const out = applyBeforeSend({ log_record_uid: uuid(), ...event });
|
|
119
|
+
if (out) enqueue(EVENTS_BATCH_KEY, out);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Attributes go out as one JSON string, the same way as custom event properties. */
|
|
123
|
+
export function attributesJSON(attrs: Record<string, unknown> | undefined): string {
|
|
124
|
+
if (!attrs || typeof attrs !== 'object') return '';
|
|
125
|
+
try {
|
|
126
|
+
const s = JSON.stringify(attrs);
|
|
127
|
+
return s === '{}' ? '' : s;
|
|
128
|
+
} catch {
|
|
129
|
+
return '';
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { appBuild, loadLastContext, saveLastContext, staticFields } from '../core/context';
|
|
2
|
+
import { LOG_PREFIX } from '../core/logger';
|
|
3
|
+
import { native, storage } from '../core/native';
|
|
4
|
+
import { persistOutbox } from '../core/transport';
|
|
5
|
+
import { attributesJSON, emit, emitRaw } from './emit';
|
|
6
|
+
import { isoTime } from '../core/clock';
|
|
7
|
+
|
|
8
|
+
const MAX_STACK_CHARS = 16_000;
|
|
9
|
+
const MAX_MESSAGE_CHARS = 4_000;
|
|
10
|
+
const MAX_CONSOLE_CHARS = 8_000;
|
|
11
|
+
/** Native code skips its own report of a JS fatal it sees within this window. */
|
|
12
|
+
export const JS_CRASH_MARKER_KEY = 'js_crash_marker';
|
|
13
|
+
|
|
14
|
+
export type ErrorSource =
|
|
15
|
+
| 'source'
|
|
16
|
+
| 'promise'
|
|
17
|
+
| 'custom'
|
|
18
|
+
| 'console'
|
|
19
|
+
| 'network'
|
|
20
|
+
| 'native'
|
|
21
|
+
| 'anr'
|
|
22
|
+
| 'app_hang';
|
|
23
|
+
|
|
24
|
+
interface Normalized {
|
|
25
|
+
message: string;
|
|
26
|
+
type: string;
|
|
27
|
+
stack: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function clip(s: string, max: number): string {
|
|
31
|
+
return s.length > max ? s.slice(0, max) + '…' : s;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function normalizeError(error: unknown): Normalized {
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
return {
|
|
37
|
+
message: clip(error.message || String(error), MAX_MESSAGE_CHARS),
|
|
38
|
+
type: error.name || 'Error',
|
|
39
|
+
stack: clip(error.stack || '', MAX_STACK_CHARS),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (error && typeof error === 'object') {
|
|
43
|
+
const o = error as Record<string, unknown>;
|
|
44
|
+
if (typeof o.message === 'string') {
|
|
45
|
+
return {
|
|
46
|
+
message: clip(o.message, MAX_MESSAGE_CHARS),
|
|
47
|
+
type: typeof o.name === 'string' ? o.name : 'Error',
|
|
48
|
+
stack: typeof o.stack === 'string' ? clip(o.stack, MAX_STACK_CHARS) : '',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return { message: clip(JSON.stringify(error), MAX_MESSAGE_CHARS), type: 'Error', stack: '' };
|
|
53
|
+
} catch {
|
|
54
|
+
// fall through
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { message: clip(String(error), MAX_MESSAGE_CHARS), type: 'Error', stack: '' };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function addError(
|
|
61
|
+
error: unknown,
|
|
62
|
+
attributes?: Record<string, unknown>,
|
|
63
|
+
source: ErrorSource = 'custom',
|
|
64
|
+
isCrash = false,
|
|
65
|
+
) {
|
|
66
|
+
const n = normalizeError(error);
|
|
67
|
+
const data: Record<string, unknown> = {
|
|
68
|
+
event_type: 'error',
|
|
69
|
+
exception_message: n.message,
|
|
70
|
+
exception_type: n.type,
|
|
71
|
+
exception_stacktrace: n.stack,
|
|
72
|
+
error_source: source,
|
|
73
|
+
error_is_crash: isCrash ? 1 : 0,
|
|
74
|
+
// Errors the app reports itself are handled; the rest reached a
|
|
75
|
+
// global handler.
|
|
76
|
+
error_handling: source === 'custom' ? 'handled' : 'unhandled',
|
|
77
|
+
};
|
|
78
|
+
const attrs = attributesJSON(attributes);
|
|
79
|
+
if (attrs) data.event_attributes = attrs;
|
|
80
|
+
emit(data);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// --- Global JS error handler ---
|
|
84
|
+
|
|
85
|
+
type GlobalHandler = (error: unknown, isFatal?: boolean) => void;
|
|
86
|
+
interface ErrorUtilsLike {
|
|
87
|
+
getGlobalHandler(): GlobalHandler;
|
|
88
|
+
setGlobalHandler(h: GlobalHandler): void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let previousHandler: GlobalHandler | null = null;
|
|
92
|
+
let errorTrackingInstalled = false;
|
|
93
|
+
|
|
94
|
+
function errorUtils(): ErrorUtilsLike | null {
|
|
95
|
+
const eu = (globalThis as unknown as { ErrorUtils?: ErrorUtilsLike }).ErrorUtils;
|
|
96
|
+
return eu && typeof eu.setGlobalHandler === 'function' ? eu : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The process can end as soon as the previous handler returns, so a fatal
|
|
101
|
+
* error is written to disk before it is passed on. The next start sends it.
|
|
102
|
+
*
|
|
103
|
+
* It is not also sent now. A send can reach the collector in the moment
|
|
104
|
+
* before the process ends, and the next start would then send the same
|
|
105
|
+
* crash from disk, so it counts two times.
|
|
106
|
+
*/
|
|
107
|
+
function handleGlobalError(error: unknown, isFatal?: boolean) {
|
|
108
|
+
try {
|
|
109
|
+
addError(error, undefined, 'source', Boolean(isFatal));
|
|
110
|
+
if (isFatal) {
|
|
111
|
+
storage.set(JS_CRASH_MARKER_KEY, String(Date.now()));
|
|
112
|
+
saveLastContext();
|
|
113
|
+
persistOutbox({ sync: true });
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
// never stop the app's own handler from running
|
|
117
|
+
}
|
|
118
|
+
if (previousHandler) previousHandler(error, isFatal);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function installErrorTracking() {
|
|
122
|
+
if (errorTrackingInstalled) return;
|
|
123
|
+
const eu = errorUtils();
|
|
124
|
+
if (eu) {
|
|
125
|
+
previousHandler = eu.getGlobalHandler();
|
|
126
|
+
eu.setGlobalHandler(handleGlobalError);
|
|
127
|
+
}
|
|
128
|
+
installPromiseRejectionTracking();
|
|
129
|
+
errorTrackingInstalled = true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function uninstallErrorTracking() {
|
|
133
|
+
const eu = errorUtils();
|
|
134
|
+
if (eu && previousHandler && errorTrackingInstalled) eu.setGlobalHandler(previousHandler);
|
|
135
|
+
previousHandler = null;
|
|
136
|
+
errorTrackingInstalled = false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface HermesLike {
|
|
140
|
+
enablePromiseRejectionTracker?: (opts: {
|
|
141
|
+
allRejections: boolean;
|
|
142
|
+
onUnhandled: (id: number, rejection: unknown) => void;
|
|
143
|
+
onHandled: (id: number) => void;
|
|
144
|
+
}) => void;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function installPromiseRejectionTracking() {
|
|
148
|
+
const hermes = (globalThis as unknown as { HermesInternal?: HermesLike }).HermesInternal;
|
|
149
|
+
if (!hermes?.enablePromiseRejectionTracker) return;
|
|
150
|
+
const dev = Boolean((globalThis as unknown as { __DEV__?: boolean }).__DEV__);
|
|
151
|
+
// This replaces React Native's own development tracker, so in a
|
|
152
|
+
// development build the warning it showed is kept.
|
|
153
|
+
hermes.enablePromiseRejectionTracker({
|
|
154
|
+
allRejections: true,
|
|
155
|
+
onUnhandled(_id, rejection) {
|
|
156
|
+
addError(rejection, undefined, 'promise');
|
|
157
|
+
if (dev) {
|
|
158
|
+
const n = normalizeError(rejection);
|
|
159
|
+
originalConsole.warn?.(`Possible unhandled promise rejection: ${n.message}`);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
onHandled() {},
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// --- Console ---
|
|
167
|
+
|
|
168
|
+
const originalConsole: { error?: (...a: unknown[]) => void; warn?: (...a: unknown[]) => void } = {};
|
|
169
|
+
let consoleInstalled = false;
|
|
170
|
+
|
|
171
|
+
function consoleText(args: unknown[]): string {
|
|
172
|
+
const parts: string[] = [];
|
|
173
|
+
for (const a of args) {
|
|
174
|
+
if (typeof a === 'string') parts.push(a);
|
|
175
|
+
else if (a instanceof Error) parts.push(`${a.name}: ${a.message}`);
|
|
176
|
+
else {
|
|
177
|
+
try {
|
|
178
|
+
parts.push(JSON.stringify(a));
|
|
179
|
+
} catch {
|
|
180
|
+
parts.push(String(a));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return clip(parts.join(' '), MAX_CONSOLE_CHARS);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let inConsole = false;
|
|
188
|
+
|
|
189
|
+
export function installConsoleTracking() {
|
|
190
|
+
if (consoleInstalled) return;
|
|
191
|
+
consoleInstalled = true;
|
|
192
|
+
for (const level of ['error', 'warn'] as const) {
|
|
193
|
+
const orig = console[level];
|
|
194
|
+
originalConsole[level] = orig.bind(console);
|
|
195
|
+
console[level] = (...args: unknown[]) => {
|
|
196
|
+
if (!inConsole && !(typeof args[0] === 'string' && args[0].startsWith(LOG_PREFIX))) {
|
|
197
|
+
inConsole = true;
|
|
198
|
+
try {
|
|
199
|
+
emit({ event_type: 'console', console_level: level, console_message: consoleText(args) });
|
|
200
|
+
} catch {
|
|
201
|
+
// ignore
|
|
202
|
+
} finally {
|
|
203
|
+
inConsole = false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
orig.apply(console, args);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function uninstallConsoleTracking() {
|
|
212
|
+
if (!consoleInstalled) return;
|
|
213
|
+
if (originalConsole.error) console.error = originalConsole.error;
|
|
214
|
+
if (originalConsole.warn) console.warn = originalConsole.warn;
|
|
215
|
+
consoleInstalled = false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// --- Crash from the previous run, and native stalls ---
|
|
219
|
+
|
|
220
|
+
/** Sends the native crash that ended the last run, on the session it happened in. */
|
|
221
|
+
export function reportPendingCrash() {
|
|
222
|
+
const crash = native.consumePendingCrash();
|
|
223
|
+
if (!crash) return;
|
|
224
|
+
const last = loadLastContext();
|
|
225
|
+
if (last && last.sampled === false) return;
|
|
226
|
+
const user = (last?.user ?? null) as Record<string, unknown> | null;
|
|
227
|
+
const fields = staticFields();
|
|
228
|
+
emitRaw({
|
|
229
|
+
...fields,
|
|
230
|
+
service_version: last?.version || fields.service_version,
|
|
231
|
+
app_build: last?.app_build || appBuild(),
|
|
232
|
+
session_id: last?.session_id ?? '',
|
|
233
|
+
user_id: user?.id ? String(user.id) : '',
|
|
234
|
+
user_name: user?.name ? String(user.name) : '',
|
|
235
|
+
user_email: user?.email ? String(user.email) : '',
|
|
236
|
+
user_status: user ? 'identified' : 'anonymous',
|
|
237
|
+
timestamp: isoTime(crash.timestamp || Date.now()),
|
|
238
|
+
app_screen_name: last?.app_screen_name ?? '',
|
|
239
|
+
view_id: last?.view_id ?? '',
|
|
240
|
+
tags: last?.tags ?? {},
|
|
241
|
+
event_type: 'error',
|
|
242
|
+
exception_message: clip(crash.message || crash.type, MAX_MESSAGE_CHARS),
|
|
243
|
+
exception_type: crash.type,
|
|
244
|
+
exception_stacktrace: clip(crash.stack || '', MAX_STACK_CHARS),
|
|
245
|
+
// A system ANR record ended the app too, so it counts as a crash.
|
|
246
|
+
error_source: crash.source === 'anr' ? 'anr' : 'native',
|
|
247
|
+
error_is_crash: 1,
|
|
248
|
+
error_handling: 'unhandled',
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Turns ANR and hang records from the native watchdog into error events. */
|
|
253
|
+
export function drainNativeEvents() {
|
|
254
|
+
for (const e of native.drainNativeEvents()) {
|
|
255
|
+
if (e.type === 'anr' || e.type === 'app_hang') {
|
|
256
|
+
const anr = e.type === 'anr';
|
|
257
|
+
emit({
|
|
258
|
+
event_type: 'error',
|
|
259
|
+
timestamp: isoTime(e.timestamp),
|
|
260
|
+
exception_message:
|
|
261
|
+
e.message ||
|
|
262
|
+
(anr
|
|
263
|
+
? `Application Not Responding: main thread blocked for ${e.duration_ms ?? 0} ms`
|
|
264
|
+
: `App hang: main thread blocked for ${e.duration_ms ?? 0} ms`),
|
|
265
|
+
exception_type: anr ? 'ANR' : 'AppHang',
|
|
266
|
+
exception_stacktrace: clip(e.stack || '', MAX_STACK_CHARS),
|
|
267
|
+
error_source: e.type,
|
|
268
|
+
error_is_crash: 0,
|
|
269
|
+
error_handling: 'unhandled',
|
|
270
|
+
long_task_duration_ms: e.duration_ms ?? 0,
|
|
271
|
+
});
|
|
272
|
+
} else if (e.type === 'memory_warning') {
|
|
273
|
+
emit({
|
|
274
|
+
event_type: 'error',
|
|
275
|
+
timestamp: isoTime(e.timestamp),
|
|
276
|
+
exception_message: e.message || 'Memory warning',
|
|
277
|
+
exception_type: 'MemoryWarning',
|
|
278
|
+
error_source: 'native',
|
|
279
|
+
error_is_crash: 0,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { AppState, Platform, type AppStateStatus, type NativeEventSubscription } from 'react-native';
|
|
2
|
+
import { refreshConnectivity, saveLastContext } from '../core/context';
|
|
3
|
+
import { flushSession, isSessionExpired } from '../core/session';
|
|
4
|
+
import { flush, persistOutbox } from '../core/transport';
|
|
5
|
+
import { canRecord, emit } from './emit';
|
|
6
|
+
import { drainNativeEvents } from './errors';
|
|
7
|
+
import { startLongTaskTracking, stopLongTaskTracking } from './longtasks';
|
|
8
|
+
import { flushActiveView, pauseViews, resumeViews } from './views';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The app state field of the OpenTelemetry `device.app.lifecycle` event
|
|
12
|
+
* (`android.app.state` or `ios.app.state`). The collector maps the state
|
|
13
|
+
* to the visibility the replay player shows.
|
|
14
|
+
*/
|
|
15
|
+
const APP_STATE_FIELD = Platform.OS === 'ios' ? 'ios_app_state' : 'android_app_state';
|
|
16
|
+
|
|
17
|
+
/** How often ANR and hang records are collected from native code. */
|
|
18
|
+
const NATIVE_DRAIN_MS = 5000;
|
|
19
|
+
|
|
20
|
+
export interface LifecycleHooks {
|
|
21
|
+
onBackground?: () => void;
|
|
22
|
+
onForeground?: (newSession: boolean) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let subscription: NativeEventSubscription | null = null;
|
|
26
|
+
let drainTimer: ReturnType<typeof setInterval> | null = null;
|
|
27
|
+
let foreground = true;
|
|
28
|
+
let hooks: LifecycleHooks = {};
|
|
29
|
+
|
|
30
|
+
export function isForeground(): boolean {
|
|
31
|
+
return foreground;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function startDrain() {
|
|
35
|
+
if (drainTimer) return;
|
|
36
|
+
drainTimer = setInterval(drainNativeEvents, NATIVE_DRAIN_MS);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function stopDrain() {
|
|
40
|
+
if (drainTimer) clearInterval(drainTimer);
|
|
41
|
+
drainTimer = null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function toBackground() {
|
|
45
|
+
if (!foreground) return;
|
|
46
|
+
foreground = false;
|
|
47
|
+
stopLongTaskTracking();
|
|
48
|
+
stopDrain();
|
|
49
|
+
drainNativeEvents();
|
|
50
|
+
flushActiveView();
|
|
51
|
+
emit({ event_type: 'visibility', [APP_STATE_FIELD]: 'background' }, false);
|
|
52
|
+
pauseViews();
|
|
53
|
+
hooks.onBackground?.();
|
|
54
|
+
saveLastContext();
|
|
55
|
+
flushSession();
|
|
56
|
+
flush();
|
|
57
|
+
// The OS can end a background app without warning.
|
|
58
|
+
persistOutbox();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function toForeground() {
|
|
62
|
+
if (foreground) return;
|
|
63
|
+
foreground = true;
|
|
64
|
+
const newSession = isSessionExpired();
|
|
65
|
+
refreshConnectivity();
|
|
66
|
+
// In a new session the hide belongs to the old one, so there is no
|
|
67
|
+
// return to pair it with.
|
|
68
|
+
if (!newSession) emit({ event_type: 'visibility', [APP_STATE_FIELD]: 'foreground' });
|
|
69
|
+
resumeViews();
|
|
70
|
+
hooks.onForeground?.(newSession);
|
|
71
|
+
drainNativeEvents();
|
|
72
|
+
startDrain();
|
|
73
|
+
startLongTaskTracking();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function onChange(state: AppStateStatus) {
|
|
77
|
+
if (!canRecord()) return;
|
|
78
|
+
// iOS reports 'inactive' for the app switcher and system sheets; the
|
|
79
|
+
// app is still on screen, so only 'background' counts as leaving.
|
|
80
|
+
if (state === 'background') toBackground();
|
|
81
|
+
else if (state === 'active') toForeground();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function installLifecycle(h: LifecycleHooks) {
|
|
85
|
+
hooks = h;
|
|
86
|
+
if (subscription) return;
|
|
87
|
+
foreground = AppState.currentState !== 'background';
|
|
88
|
+
subscription = AppState.addEventListener('change', onChange);
|
|
89
|
+
if (foreground) {
|
|
90
|
+
startDrain();
|
|
91
|
+
startLongTaskTracking();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function uninstallLifecycle() {
|
|
96
|
+
subscription?.remove();
|
|
97
|
+
subscription = null;
|
|
98
|
+
stopDrain();
|
|
99
|
+
stopLongTaskTracking();
|
|
100
|
+
hooks = {};
|
|
101
|
+
foreground = true;
|
|
102
|
+
}
|