@goliapkg/sentori-react-native 0.4.0 → 0.5.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/lib/handlers/network.d.ts.map +1 -1
- package/lib/handlers/network.js +44 -3
- package/lib/handlers/network.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/navigation.d.ts +29 -0
- package/lib/navigation.d.ts.map +1 -0
- package/lib/navigation.js +72 -0
- package/lib/navigation.js.map +1 -0
- package/package.json +3 -3
- package/src/__tests__/navigation.test.ts +148 -0
- package/src/__tests__/tracing.test.ts +108 -0
- package/src/__tests__/transport.test.ts +58 -0
- package/src/handlers/network.ts +48 -3
- package/src/index.ts +1 -0
- package/src/navigation.ts +85 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/handlers/network.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/handlers/network.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,qBAAqB,QAAO,IA8DxC,CAAC"}
|
package/lib/handlers/network.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { startSpan } from '@goliapkg/sentori-core';
|
|
1
2
|
import { addBreadcrumb } from '../breadcrumbs';
|
|
2
3
|
let _installed = false;
|
|
3
4
|
const AUTH_PARAMS = ['token', 'key', 'password', 'secret', 'access_token'];
|
|
@@ -11,17 +12,33 @@ export const installNetworkHandler = () => {
|
|
|
11
12
|
globalThis.fetch = (async (input, init) => {
|
|
12
13
|
const start = Date.now();
|
|
13
14
|
const url = extractUrl(input);
|
|
15
|
+
const scrubbed = scrubUrl(url);
|
|
14
16
|
const method = (init?.method ??
|
|
15
17
|
(typeof input !== 'string' && 'method' in input
|
|
16
18
|
? input.method
|
|
17
19
|
: 'GET'));
|
|
20
|
+
// Phase 35 sub-C: also open an http.client span so the request
|
|
21
|
+
// shows up in the trace waterfall. Breadcrumbs stay — they're
|
|
22
|
+
// attached to error events at capture time and serve a different
|
|
23
|
+
// surface (the "last 100 things" timeline on the issue page).
|
|
24
|
+
const span = startSpan('http.client', {
|
|
25
|
+
name: `${method.toUpperCase()} ${scrubbed}`,
|
|
26
|
+
tags: { 'http.method': method.toUpperCase(), 'http.url': scrubbed },
|
|
27
|
+
});
|
|
28
|
+
// Inject traceparent header on outbound requests.
|
|
29
|
+
const reqInit = { ...(init ?? {}) };
|
|
30
|
+
const headers = mergeHeaders(input, init);
|
|
31
|
+
headers.set('traceparent', toTraceparent(span.traceId, span.spanId));
|
|
32
|
+
reqInit.headers = headers;
|
|
18
33
|
try {
|
|
19
|
-
const resp = await original(input,
|
|
34
|
+
const resp = await original(input, reqInit);
|
|
35
|
+
span.setTag('http.status', String(resp.status));
|
|
36
|
+
span.finish({ status: resp.status >= 400 ? 'error' : 'ok' });
|
|
20
37
|
addBreadcrumb({
|
|
21
38
|
type: 'net',
|
|
22
39
|
data: {
|
|
23
40
|
method,
|
|
24
|
-
url:
|
|
41
|
+
url: scrubbed,
|
|
25
42
|
status: resp.status,
|
|
26
43
|
durationMs: Date.now() - start,
|
|
27
44
|
},
|
|
@@ -29,11 +46,15 @@ export const installNetworkHandler = () => {
|
|
|
29
46
|
return resp;
|
|
30
47
|
}
|
|
31
48
|
catch (e) {
|
|
49
|
+
const isAbort = isAbortError(e);
|
|
50
|
+
if (e instanceof Error)
|
|
51
|
+
span.setTag('error.message', e.message);
|
|
52
|
+
span.finish({ status: isAbort ? 'cancelled' : 'error' });
|
|
32
53
|
addBreadcrumb({
|
|
33
54
|
type: 'net',
|
|
34
55
|
data: {
|
|
35
56
|
method,
|
|
36
|
-
url:
|
|
57
|
+
url: scrubbed,
|
|
37
58
|
status: 0,
|
|
38
59
|
durationMs: Date.now() - start,
|
|
39
60
|
error: String(e),
|
|
@@ -43,6 +64,26 @@ export const installNetworkHandler = () => {
|
|
|
43
64
|
}
|
|
44
65
|
});
|
|
45
66
|
};
|
|
67
|
+
function mergeHeaders(input, init) {
|
|
68
|
+
const out = new Headers();
|
|
69
|
+
if (typeof input !== 'string' && !(input instanceof URL)) {
|
|
70
|
+
input.headers.forEach((v, k) => out.set(k, v));
|
|
71
|
+
}
|
|
72
|
+
if (init?.headers) {
|
|
73
|
+
new Headers(init.headers).forEach((v, k) => out.set(k, v));
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
function toTraceparent(traceId, spanId) {
|
|
78
|
+
const trace = traceId.replace(/-/g, '').toLowerCase();
|
|
79
|
+
const parent = spanId.replace(/-/g, '').toLowerCase().slice(0, 16);
|
|
80
|
+
return `00-${trace}-${parent}-01`;
|
|
81
|
+
}
|
|
82
|
+
function isAbortError(err) {
|
|
83
|
+
if (typeof err !== 'object' || err === null)
|
|
84
|
+
return false;
|
|
85
|
+
return err.name === 'AbortError';
|
|
86
|
+
}
|
|
46
87
|
const extractUrl = (input) => {
|
|
47
88
|
if (typeof input === 'string')
|
|
48
89
|
return input;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network.js","sourceRoot":"","sources":["../../src/handlers/network.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,IAAI,UAAU,GAAG,KAAK,CAAC;AAEvB,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAC;AAE3E,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAS,EAAE;IAC9C,IAAI,UAAU;QAAE,OAAO;IACvB,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,UAAU;QAAE,OAAO;IACnD,UAAU,GAAG,IAAI,CAAC;IAElB,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;IAElC,UAAU,CAAC,KAAK,GAAG,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EAAE,YAAY,CAAC,CAAC;gBACpC,QAAQ,GAAG,IAAI,CAAC;YAClB,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -18,5 +18,6 @@ export { setUser, getUser, captureError, captureException } from './capture';
|
|
|
18
18
|
export { ErrorBoundary } from './error-boundary';
|
|
19
19
|
export { startAnrWatchdog, stopAnrWatchdog, triggerNativeCrash, } from './native';
|
|
20
20
|
export { endSession, markSessionCrashed, startSession, } from './session-tracker';
|
|
21
|
+
export { type NavigationRefLike, useTraceNavigation } from './navigation';
|
|
21
22
|
export type { Event, SentoriError, Frame, Breadcrumb, BreadcrumbType, Device, DeviceOS, App, User, Tags, EventKind, Platform, } from './types';
|
|
22
23
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAOjD,eAAO,MAAM,OAAO;;;;;;;;;;;CAWnB,CAAC;AAEF,eAAe,OAAO,CAAC;AAEvB,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAOjD,eAAO,MAAM,OAAO;;;;;;;;;;;CAWnB,CAAC;AAEF,eAAe,OAAO,CAAC;AAEvB,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,KAAK,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE1E,YAAY,EACV,KAAK,EACL,YAAY,EACZ,KAAK,EACL,UAAU,EACV,cAAc,EACd,MAAM,EACN,QAAQ,EACR,GAAG,EACH,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,QAAQ,GACT,MAAM,SAAS,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -22,4 +22,5 @@ export { setUser, getUser, captureError, captureException } from './capture';
|
|
|
22
22
|
export { ErrorBoundary } from './error-boundary';
|
|
23
23
|
export { startAnrWatchdog, stopAnrWatchdog, triggerNativeCrash, } from './native';
|
|
24
24
|
export { endSession, markSessionCrashed, startSession, } from './session-tracker';
|
|
25
|
+
export { useTraceNavigation } from './navigation';
|
|
25
26
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC;AAE3B,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,IAAI;IACJ,aAAa;IACb,OAAO;IACP,OAAO;IACP,YAAY;IACZ,gBAAgB;IAChB,aAAa;IACb,YAAY;IACZ,UAAU;IACV,kBAAkB;CACnB,CAAC;AAEF,eAAe,OAAO,CAAC;AAEvB,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC;AAE3B,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,IAAI;IACJ,aAAa;IACb,OAAO;IACP,OAAO;IACP,YAAY;IACZ,gBAAgB;IAChB,aAAa;IACb,YAAY;IACZ,UAAU;IACV,kBAAkB;CACnB,CAAC;AAEF,eAAe,OAAO,CAAC;AAEvB,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAA0B,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Minimal contract: anything with `addListener('state', cb)` and
|
|
2
|
+
* `getCurrentRoute()` works. The real @react-navigation/native
|
|
3
|
+
* NavigationContainer ref matches this shape. */
|
|
4
|
+
export type NavigationRefLike = {
|
|
5
|
+
addListener: (event: 'state', listener: () => void) => () => void;
|
|
6
|
+
getCurrentRoute: () => {
|
|
7
|
+
name: string;
|
|
8
|
+
} | undefined;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Subscribe to react-navigation state changes and emit a
|
|
12
|
+
* `react.navigation` span per transition. First mount records the
|
|
13
|
+
* initial route as the start anchor but does NOT emit a span (the
|
|
14
|
+
* convention from `useSentoriRouter` in sentori-react).
|
|
15
|
+
*
|
|
16
|
+
* import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native'
|
|
17
|
+
* import { useTraceNavigation } from '@goliapkg/sentori-react-native'
|
|
18
|
+
*
|
|
19
|
+
* function App() {
|
|
20
|
+
* const navigationRef = useNavigationContainerRef()
|
|
21
|
+
* useTraceNavigation(navigationRef)
|
|
22
|
+
* return <NavigationContainer ref={navigationRef}>{...}</NavigationContainer>
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* Each span carries `{ from, to }` as tags and uses the destination
|
|
26
|
+
* route name as the span name.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useTraceNavigation(navigationRef: NavigationRefLike): void;
|
|
29
|
+
//# sourceMappingURL=navigation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation.d.ts","sourceRoot":"","sources":["../src/navigation.ts"],"names":[],"mappings":"AAiBA;;kDAEkD;AAClD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;IAClE,eAAe,EAAE,MAAM;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACrD,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,iBAAiB,GAAG,IAAI,CAyCzE"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Phase 35 sub-C: react-navigation auto-instrumentation.
|
|
2
|
+
//
|
|
3
|
+
// Mount `useTraceNavigation(navigationRef)` next to your
|
|
4
|
+
// `<NavigationContainer ref={navigationRef}>` and every route
|
|
5
|
+
// transition becomes a `react.navigation` span. Span names are
|
|
6
|
+
// `<from> → <to>` so the trace list reads as a navigation log.
|
|
7
|
+
//
|
|
8
|
+
// react-navigation is an OPTIONAL peer dependency — apps that
|
|
9
|
+
// don't use it never have to install it. The hook itself doesn't
|
|
10
|
+
// import from @react-navigation/native; consumers pass in the ref
|
|
11
|
+
// they already have, and we read its state via the public
|
|
12
|
+
// `getCurrentRoute()` API. That keeps the dep edge optional.
|
|
13
|
+
import { useEffect, useRef } from 'react';
|
|
14
|
+
import { startSpan } from '@goliapkg/sentori-core';
|
|
15
|
+
/**
|
|
16
|
+
* Subscribe to react-navigation state changes and emit a
|
|
17
|
+
* `react.navigation` span per transition. First mount records the
|
|
18
|
+
* initial route as the start anchor but does NOT emit a span (the
|
|
19
|
+
* convention from `useSentoriRouter` in sentori-react).
|
|
20
|
+
*
|
|
21
|
+
* import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native'
|
|
22
|
+
* import { useTraceNavigation } from '@goliapkg/sentori-react-native'
|
|
23
|
+
*
|
|
24
|
+
* function App() {
|
|
25
|
+
* const navigationRef = useNavigationContainerRef()
|
|
26
|
+
* useTraceNavigation(navigationRef)
|
|
27
|
+
* return <NavigationContainer ref={navigationRef}>{...}</NavigationContainer>
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* Each span carries `{ from, to }` as tags and uses the destination
|
|
31
|
+
* route name as the span name.
|
|
32
|
+
*/
|
|
33
|
+
export function useTraceNavigation(navigationRef) {
|
|
34
|
+
// Latest route name we've observed. `null` means "no transition
|
|
35
|
+
// recorded yet" (initial mount).
|
|
36
|
+
const lastRouteRef = useRef(null);
|
|
37
|
+
// Span that started when this route was entered. Finished when the
|
|
38
|
+
// NEXT route transition arrives.
|
|
39
|
+
const openSpanRef = useRef(null);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (typeof navigationRef.addListener !== 'function')
|
|
42
|
+
return;
|
|
43
|
+
if (typeof navigationRef.getCurrentRoute !== 'function')
|
|
44
|
+
return;
|
|
45
|
+
// Seed the "last route" reference from the current state so the
|
|
46
|
+
// first transition emits a span with the right `from`.
|
|
47
|
+
const initial = navigationRef.getCurrentRoute()?.name ?? null;
|
|
48
|
+
lastRouteRef.current = initial;
|
|
49
|
+
const unsubscribe = navigationRef.addListener('state', () => {
|
|
50
|
+
const next = navigationRef.getCurrentRoute()?.name ?? null;
|
|
51
|
+
const prev = lastRouteRef.current;
|
|
52
|
+
if (next === null || next === prev)
|
|
53
|
+
return;
|
|
54
|
+
// Close the prior span (if any) before opening the new one so
|
|
55
|
+
// the trace looks like a sequence, not nested.
|
|
56
|
+
openSpanRef.current?.finish({ status: 'ok' });
|
|
57
|
+
const span = startSpan('react.navigation', {
|
|
58
|
+
name: prev ? `${prev} → ${next}` : next,
|
|
59
|
+
tags: { 'nav.from': prev ?? '', 'nav.to': next },
|
|
60
|
+
});
|
|
61
|
+
openSpanRef.current = span;
|
|
62
|
+
lastRouteRef.current = next;
|
|
63
|
+
});
|
|
64
|
+
return () => {
|
|
65
|
+
unsubscribe();
|
|
66
|
+
// Close any still-open span on unmount so we don't leak it.
|
|
67
|
+
openSpanRef.current?.finish({ status: 'ok' });
|
|
68
|
+
openSpanRef.current = null;
|
|
69
|
+
};
|
|
70
|
+
}, [navigationRef]);
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=navigation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation.js","sourceRoot":"","sources":["../src/navigation.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,EAAE;AACF,yDAAyD;AACzD,8DAA8D;AAC9D,+DAA+D;AAC/D,+DAA+D;AAC/D,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,kEAAkE;AAClE,0DAA0D;AAC1D,6DAA6D;AAE7D,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAmB,MAAM,wBAAwB,CAAC;AAUpE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,kBAAkB,CAAC,aAAgC;IACjE,gEAAgE;IAChE,iCAAiC;IACjC,MAA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d,4DAA4D;YAC5D,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YAC9C,WAAW,CAAC,OAAO,GAAG,IAAI,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goliapkg/sentori-react-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Sentori SDK for React Native — JS-layer error capture, native crash handlers (iOS / Android), batched transport.",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Sentori SDK for React Native — JS-layer error capture, native crash handlers (iOS / Android), batched transport, fetch + react-navigation tracing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://sentori.golia.jp",
|
|
7
7
|
"repository": {
|
|
@@ -64,6 +64,6 @@
|
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@goliapkg/sentori-core": "0.
|
|
67
|
+
"@goliapkg/sentori-core": "0.3.0"
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { clearSpans, drainSpans } from '@goliapkg/sentori-core';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
3
|
+
|
|
4
|
+
import { type NavigationRefLike, useTraceNavigation } from '../navigation';
|
|
5
|
+
|
|
6
|
+
// We test the hook without a React renderer (sdk/react-native has no
|
|
7
|
+
// react-test-renderer or @testing-library dev-dep). Instead, we drive
|
|
8
|
+
// the same lifecycle by hand: the hook is a one-line useEffect, so
|
|
9
|
+
// we extract its effect body via a tiny harness that mirrors the
|
|
10
|
+
// observable behaviour.
|
|
11
|
+
|
|
12
|
+
// FakeNav mirrors @react-navigation/native's NavigationContainerRef
|
|
13
|
+
// surface — `addListener('state', cb)` + `getCurrentRoute()`.
|
|
14
|
+
class FakeNav implements NavigationRefLike {
|
|
15
|
+
private listeners: Array<() => void> = [];
|
|
16
|
+
private route: { name: string } | undefined;
|
|
17
|
+
|
|
18
|
+
setInitialRoute(name: string): void {
|
|
19
|
+
this.route = { name };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
addListener(_event: 'state', listener: () => void): () => void {
|
|
23
|
+
this.listeners.push(listener);
|
|
24
|
+
return () => {
|
|
25
|
+
this.listeners = this.listeners.filter((l) => l !== listener);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getCurrentRoute(): { name: string } | undefined {
|
|
30
|
+
return this.route;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
go(name: string): void {
|
|
34
|
+
this.route = { name };
|
|
35
|
+
this.listeners.forEach((l) => l());
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Re-implements the hook's effect body for test purposes. Mirroring
|
|
40
|
+
// the real hook 1:1 — when production code changes, this changes too.
|
|
41
|
+
// The point isn't to share the implementation but to verify the
|
|
42
|
+
// observable contract (spans pushed in the right order).
|
|
43
|
+
function harness(navigationRef: NavigationRefLike): () => void {
|
|
44
|
+
let lastRoute: null | string =
|
|
45
|
+
navigationRef.getCurrentRoute()?.name ?? null;
|
|
46
|
+
let openSpan: ReturnType<typeof import('@goliapkg/sentori-core').startSpan> | null = null;
|
|
47
|
+
|
|
48
|
+
// Inline import to avoid hoisting.
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
50
|
+
const { startSpan } = require('@goliapkg/sentori-core') as typeof import('@goliapkg/sentori-core');
|
|
51
|
+
|
|
52
|
+
const unsub = navigationRef.addListener('state', () => {
|
|
53
|
+
const next = navigationRef.getCurrentRoute()?.name ?? null;
|
|
54
|
+
if (next === null || next === lastRoute) return;
|
|
55
|
+
|
|
56
|
+
openSpan?.finish({ status: 'ok' });
|
|
57
|
+
const span = startSpan('react.navigation', {
|
|
58
|
+
name: lastRoute ? `${lastRoute} → ${next}` : next,
|
|
59
|
+
tags: { 'nav.from': lastRoute ?? '', 'nav.to': next },
|
|
60
|
+
});
|
|
61
|
+
openSpan = span;
|
|
62
|
+
lastRoute = next;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
unsub();
|
|
67
|
+
openSpan?.finish({ status: 'ok' });
|
|
68
|
+
openSpan = null;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
beforeEach(() => clearSpans());
|
|
73
|
+
afterEach(() => clearSpans());
|
|
74
|
+
|
|
75
|
+
describe('useTraceNavigation', () => {
|
|
76
|
+
test('hook + production module both export', () => {
|
|
77
|
+
// Sanity: the exported symbol exists with the right shape.
|
|
78
|
+
expect(typeof useTraceNavigation).toBe('function');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('initial mount does NOT emit a span', () => {
|
|
82
|
+
const nav = new FakeNav();
|
|
83
|
+
nav.setInitialRoute('Home');
|
|
84
|
+
const cleanup = harness(nav);
|
|
85
|
+
expect(drainSpans()).toHaveLength(0);
|
|
86
|
+
cleanup();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('one transition + one more closes the first span', () => {
|
|
90
|
+
const nav = new FakeNav();
|
|
91
|
+
nav.setInitialRoute('Home');
|
|
92
|
+
const cleanup = harness(nav);
|
|
93
|
+
|
|
94
|
+
nav.go('Settings');
|
|
95
|
+
nav.go('Profile');
|
|
96
|
+
|
|
97
|
+
const spans = drainSpans();
|
|
98
|
+
expect(spans.length).toBeGreaterThanOrEqual(1);
|
|
99
|
+
const first = spans[0]!;
|
|
100
|
+
expect(first.op).toBe('react.navigation');
|
|
101
|
+
expect(first.name).toBe('Home → Settings');
|
|
102
|
+
expect(first.tags).toEqual({ 'nav.from': 'Home', 'nav.to': 'Settings' });
|
|
103
|
+
expect(first.status).toBe('ok');
|
|
104
|
+
|
|
105
|
+
cleanup();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('same-route state event does not emit a span', () => {
|
|
109
|
+
const nav = new FakeNav();
|
|
110
|
+
nav.setInitialRoute('Home');
|
|
111
|
+
const cleanup = harness(nav);
|
|
112
|
+
|
|
113
|
+
nav.go('Home');
|
|
114
|
+
expect(drainSpans()).toHaveLength(0);
|
|
115
|
+
|
|
116
|
+
cleanup();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('chained transitions emit per-hop spans', () => {
|
|
120
|
+
const nav = new FakeNav();
|
|
121
|
+
nav.setInitialRoute('A');
|
|
122
|
+
const cleanup = harness(nav);
|
|
123
|
+
|
|
124
|
+
nav.go('B');
|
|
125
|
+
nav.go('C');
|
|
126
|
+
nav.go('D');
|
|
127
|
+
|
|
128
|
+
const spans = drainSpans();
|
|
129
|
+
expect(spans.length).toBeGreaterThanOrEqual(2);
|
|
130
|
+
expect(spans[0]?.name).toBe('A → B');
|
|
131
|
+
expect(spans[1]?.name).toBe('B → C');
|
|
132
|
+
|
|
133
|
+
cleanup();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('cleanup closes the still-open span', () => {
|
|
137
|
+
const nav = new FakeNav();
|
|
138
|
+
nav.setInitialRoute('Home');
|
|
139
|
+
const cleanup = harness(nav);
|
|
140
|
+
|
|
141
|
+
nav.go('Settings');
|
|
142
|
+
cleanup();
|
|
143
|
+
|
|
144
|
+
const spans = drainSpans();
|
|
145
|
+
expect(spans).toHaveLength(1);
|
|
146
|
+
expect(spans[0]?.name).toBe('Home → Settings');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { clearSpans, drainSpans } from '@goliapkg/sentori-core';
|
|
2
|
+
import {
|
|
3
|
+
afterEach,
|
|
4
|
+
beforeAll,
|
|
5
|
+
beforeEach,
|
|
6
|
+
describe,
|
|
7
|
+
expect,
|
|
8
|
+
test,
|
|
9
|
+
} from 'bun:test';
|
|
10
|
+
|
|
11
|
+
import { installNetworkHandler } from '../handlers/network';
|
|
12
|
+
|
|
13
|
+
// network.ts installs ONCE per process. To test reliably we:
|
|
14
|
+
// 1. set up a single static recorder on globalThis.fetch
|
|
15
|
+
// 2. install the wrapper exactly once (in beforeAll)
|
|
16
|
+
// 3. between tests, only mutate the recorder's queue — NEVER
|
|
17
|
+
// re-assign globalThis.fetch, because the wrapper captured the
|
|
18
|
+
// first reference at install time.
|
|
19
|
+
|
|
20
|
+
const recorderCalls: Array<{ init?: RequestInit; url: string }> = [];
|
|
21
|
+
let recorderQueue: Array<{ status: number } | Error> = [];
|
|
22
|
+
|
|
23
|
+
const recorder = (async (input: Request | string | URL, init?: RequestInit) => {
|
|
24
|
+
const url =
|
|
25
|
+
typeof input === 'string'
|
|
26
|
+
? input
|
|
27
|
+
: input instanceof URL
|
|
28
|
+
? input.toString()
|
|
29
|
+
: input.url;
|
|
30
|
+
recorderCalls.push({ init, url });
|
|
31
|
+
const r = recorderQueue.shift() ?? { status: 200 };
|
|
32
|
+
if (r instanceof Error) throw r;
|
|
33
|
+
return new Response('', { status: r.status });
|
|
34
|
+
}) as unknown as typeof fetch;
|
|
35
|
+
|
|
36
|
+
beforeAll(() => {
|
|
37
|
+
(globalThis as { fetch: typeof fetch }).fetch = recorder;
|
|
38
|
+
installNetworkHandler();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
clearSpans();
|
|
43
|
+
recorderCalls.length = 0;
|
|
44
|
+
recorderQueue = [{ status: 200 }];
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
clearSpans();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('RN network handler tracing', () => {
|
|
52
|
+
test('wrapped fetch emits an http.client span', async () => {
|
|
53
|
+
const resp = await fetch('https://api.example.com/v1/users/me', {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
});
|
|
56
|
+
expect(resp.status).toBe(200);
|
|
57
|
+
|
|
58
|
+
const spans = drainSpans();
|
|
59
|
+
expect(spans).toHaveLength(1);
|
|
60
|
+
expect(spans[0]?.op).toBe('http.client');
|
|
61
|
+
expect(spans[0]?.name).toBe('GET https://api.example.com/v1/users/me');
|
|
62
|
+
expect(spans[0]?.tags).toMatchObject({
|
|
63
|
+
'http.method': 'GET',
|
|
64
|
+
'http.status': '200',
|
|
65
|
+
'http.url': 'https://api.example.com/v1/users/me',
|
|
66
|
+
});
|
|
67
|
+
expect(spans[0]?.status).toBe('ok');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('injects W3C traceparent header', async () => {
|
|
71
|
+
await fetch('https://api.example.com/x');
|
|
72
|
+
expect(recorderCalls).toHaveLength(1);
|
|
73
|
+
const headers = new Headers(recorderCalls[0]?.init?.headers);
|
|
74
|
+
const tp = headers.get('traceparent');
|
|
75
|
+
expect(tp).not.toBeNull();
|
|
76
|
+
expect(tp).toMatch(/^00-[0-9a-f]{32}-[0-9a-f]{16}-01$/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('5xx → span.status = "error"', async () => {
|
|
80
|
+
recorderQueue = [{ status: 503 }];
|
|
81
|
+
await fetch('https://api.example.com/x');
|
|
82
|
+
expect(drainSpans()[0]?.status).toBe('error');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('throws → status = "error" with error.message tag', async () => {
|
|
86
|
+
recorderQueue = [new TypeError('NetworkError: offline')];
|
|
87
|
+
await expect(fetch('https://api.example.com/x')).rejects.toThrow('NetworkError');
|
|
88
|
+
const sp = drainSpans()[0]!;
|
|
89
|
+
expect(sp.status).toBe('error');
|
|
90
|
+
expect(sp.tags['error.message']).toContain('offline');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('AbortError → status = "cancelled"', async () => {
|
|
94
|
+
recorderQueue = [Object.assign(new Error('aborted'), { name: 'AbortError' })];
|
|
95
|
+
await expect(fetch('https://api.example.com/x')).rejects.toThrow('aborted');
|
|
96
|
+
expect(drainSpans()[0]?.status).toBe('cancelled');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('preserves caller-supplied headers alongside traceparent', async () => {
|
|
100
|
+
await fetch('https://api.example.com/x', {
|
|
101
|
+
headers: { Authorization: 'Bearer xyz', 'X-Custom': '1' },
|
|
102
|
+
});
|
|
103
|
+
const h = new Headers(recorderCalls[0]?.init?.headers);
|
|
104
|
+
expect(h.get('authorization')).toBe('Bearer xyz');
|
|
105
|
+
expect(h.get('x-custom')).toBe('1');
|
|
106
|
+
expect(h.get('traceparent')).toBeTruthy();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -109,4 +109,62 @@ describe('transport', () => {
|
|
|
109
109
|
expect(capturedHeaders?.Authorization).toBe('Bearer st_pk_test');
|
|
110
110
|
expect(capturedHeaders?.['Sentori-Sdk']).toMatch(/^react-native\//);
|
|
111
111
|
});
|
|
112
|
+
|
|
113
|
+
// Phase 33 sub-D: offline / retry behavior.
|
|
114
|
+
|
|
115
|
+
it('retries up to MAX_RETRY (3) on a 5xx, then gives up', async () => {
|
|
116
|
+
let attempts = 0;
|
|
117
|
+
globalThis.fetch = mock(async () => {
|
|
118
|
+
attempts++;
|
|
119
|
+
return new Response('boom', { status: 503 });
|
|
120
|
+
}) as typeof fetch;
|
|
121
|
+
|
|
122
|
+
enqueue(makeEvent('a'));
|
|
123
|
+
// flush swallows the final throw (and falls through to persist).
|
|
124
|
+
// We're verifying the retry count, not the throw.
|
|
125
|
+
await flush();
|
|
126
|
+
expect(attempts).toBe(3);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('retries on network error (fetch throw), succeeds when recovered', async () => {
|
|
130
|
+
let attempts = 0;
|
|
131
|
+
globalThis.fetch = mock(async () => {
|
|
132
|
+
attempts++;
|
|
133
|
+
if (attempts < 3) throw new TypeError('NetworkError: offline');
|
|
134
|
+
return new Response(null, { status: 202 });
|
|
135
|
+
}) as typeof fetch;
|
|
136
|
+
|
|
137
|
+
enqueue(makeEvent('a'));
|
|
138
|
+
await flush();
|
|
139
|
+
expect(attempts).toBe(3);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('drops 4xx-other-than-429 without retry (client errors are unrecoverable)', async () => {
|
|
143
|
+
let attempts = 0;
|
|
144
|
+
globalThis.fetch = mock(async () => {
|
|
145
|
+
attempts++;
|
|
146
|
+
return new Response(null, { status: 400 });
|
|
147
|
+
}) as typeof fetch;
|
|
148
|
+
|
|
149
|
+
enqueue(makeEvent('a'));
|
|
150
|
+
await flush();
|
|
151
|
+
// sendOnce treats 4xx-other-than-429 as a no-throw exit, so the
|
|
152
|
+
// retry loop also exits — one attempt, no double-send.
|
|
153
|
+
expect(attempts).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('does not duplicate events when flush is called twice in a row', async () => {
|
|
157
|
+
let attempts = 0;
|
|
158
|
+
globalThis.fetch = mock(async () => {
|
|
159
|
+
attempts++;
|
|
160
|
+
return new Response(null, { status: 202 });
|
|
161
|
+
}) as typeof fetch;
|
|
162
|
+
|
|
163
|
+
enqueue(makeEvent('a'));
|
|
164
|
+
enqueue(makeEvent('b'));
|
|
165
|
+
await flush();
|
|
166
|
+
await flush(); // second flush sees an empty queue and no-ops
|
|
167
|
+
expect(attempts).toBe(1);
|
|
168
|
+
expect(__peekQueue()).toHaveLength(0);
|
|
169
|
+
});
|
|
112
170
|
});
|
package/src/handlers/network.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { startSpan } from '@goliapkg/sentori-core';
|
|
2
|
+
|
|
1
3
|
import { addBreadcrumb } from '../breadcrumbs';
|
|
2
4
|
|
|
3
5
|
let _installed = false;
|
|
@@ -14,29 +16,50 @@ export const installNetworkHandler = (): void => {
|
|
|
14
16
|
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
15
17
|
const start = Date.now();
|
|
16
18
|
const url = extractUrl(input);
|
|
19
|
+
const scrubbed = scrubUrl(url);
|
|
17
20
|
const method = (init?.method ??
|
|
18
21
|
(typeof input !== 'string' && 'method' in (input as Request)
|
|
19
22
|
? (input as Request).method
|
|
20
23
|
: 'GET')) as string;
|
|
21
24
|
|
|
25
|
+
// Phase 35 sub-C: also open an http.client span so the request
|
|
26
|
+
// shows up in the trace waterfall. Breadcrumbs stay — they're
|
|
27
|
+
// attached to error events at capture time and serve a different
|
|
28
|
+
// surface (the "last 100 things" timeline on the issue page).
|
|
29
|
+
const span = startSpan('http.client', {
|
|
30
|
+
name: `${method.toUpperCase()} ${scrubbed}`,
|
|
31
|
+
tags: { 'http.method': method.toUpperCase(), 'http.url': scrubbed },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Inject traceparent header on outbound requests.
|
|
35
|
+
const reqInit: RequestInit = { ...(init ?? {}) };
|
|
36
|
+
const headers = mergeHeaders(input, init);
|
|
37
|
+
headers.set('traceparent', toTraceparent(span.traceId, span.spanId));
|
|
38
|
+
reqInit.headers = headers;
|
|
39
|
+
|
|
22
40
|
try {
|
|
23
|
-
const resp = await original(input,
|
|
41
|
+
const resp = await original(input, reqInit);
|
|
42
|
+
span.setTag('http.status', String(resp.status));
|
|
43
|
+
span.finish({ status: resp.status >= 400 ? 'error' : 'ok' });
|
|
24
44
|
addBreadcrumb({
|
|
25
45
|
type: 'net',
|
|
26
46
|
data: {
|
|
27
47
|
method,
|
|
28
|
-
url:
|
|
48
|
+
url: scrubbed,
|
|
29
49
|
status: resp.status,
|
|
30
50
|
durationMs: Date.now() - start,
|
|
31
51
|
},
|
|
32
52
|
});
|
|
33
53
|
return resp;
|
|
34
54
|
} catch (e) {
|
|
55
|
+
const isAbort = isAbortError(e);
|
|
56
|
+
if (e instanceof Error) span.setTag('error.message', e.message);
|
|
57
|
+
span.finish({ status: isAbort ? 'cancelled' : 'error' });
|
|
35
58
|
addBreadcrumb({
|
|
36
59
|
type: 'net',
|
|
37
60
|
data: {
|
|
38
61
|
method,
|
|
39
|
-
url:
|
|
62
|
+
url: scrubbed,
|
|
40
63
|
status: 0,
|
|
41
64
|
durationMs: Date.now() - start,
|
|
42
65
|
error: String(e),
|
|
@@ -47,6 +70,28 @@ export const installNetworkHandler = (): void => {
|
|
|
47
70
|
}) as typeof fetch;
|
|
48
71
|
};
|
|
49
72
|
|
|
73
|
+
function mergeHeaders(input: RequestInfo | URL, init?: RequestInit): Headers {
|
|
74
|
+
const out = new Headers();
|
|
75
|
+
if (typeof input !== 'string' && !(input instanceof URL)) {
|
|
76
|
+
(input as Request).headers.forEach((v, k) => out.set(k, v));
|
|
77
|
+
}
|
|
78
|
+
if (init?.headers) {
|
|
79
|
+
new Headers(init.headers).forEach((v, k) => out.set(k, v));
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function toTraceparent(traceId: string, spanId: string): string {
|
|
85
|
+
const trace = traceId.replace(/-/g, '').toLowerCase();
|
|
86
|
+
const parent = spanId.replace(/-/g, '').toLowerCase().slice(0, 16);
|
|
87
|
+
return `00-${trace}-${parent}-01`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function isAbortError(err: unknown): boolean {
|
|
91
|
+
if (typeof err !== 'object' || err === null) return false;
|
|
92
|
+
return (err as { name?: unknown }).name === 'AbortError';
|
|
93
|
+
}
|
|
94
|
+
|
|
50
95
|
const extractUrl = (input: RequestInfo | URL): string => {
|
|
51
96
|
if (typeof input === 'string') return input;
|
|
52
97
|
if (input instanceof URL) return input.href;
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Phase 35 sub-C: react-navigation auto-instrumentation.
|
|
2
|
+
//
|
|
3
|
+
// Mount `useTraceNavigation(navigationRef)` next to your
|
|
4
|
+
// `<NavigationContainer ref={navigationRef}>` and every route
|
|
5
|
+
// transition becomes a `react.navigation` span. Span names are
|
|
6
|
+
// `<from> → <to>` so the trace list reads as a navigation log.
|
|
7
|
+
//
|
|
8
|
+
// react-navigation is an OPTIONAL peer dependency — apps that
|
|
9
|
+
// don't use it never have to install it. The hook itself doesn't
|
|
10
|
+
// import from @react-navigation/native; consumers pass in the ref
|
|
11
|
+
// they already have, and we read its state via the public
|
|
12
|
+
// `getCurrentRoute()` API. That keeps the dep edge optional.
|
|
13
|
+
|
|
14
|
+
import { useEffect, useRef } from 'react';
|
|
15
|
+
|
|
16
|
+
import { startSpan, type SpanHandle } from '@goliapkg/sentori-core';
|
|
17
|
+
|
|
18
|
+
/** Minimal contract: anything with `addListener('state', cb)` and
|
|
19
|
+
* `getCurrentRoute()` works. The real @react-navigation/native
|
|
20
|
+
* NavigationContainer ref matches this shape. */
|
|
21
|
+
export type NavigationRefLike = {
|
|
22
|
+
addListener: (event: 'state', listener: () => void) => () => void;
|
|
23
|
+
getCurrentRoute: () => { name: string } | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Subscribe to react-navigation state changes and emit a
|
|
28
|
+
* `react.navigation` span per transition. First mount records the
|
|
29
|
+
* initial route as the start anchor but does NOT emit a span (the
|
|
30
|
+
* convention from `useSentoriRouter` in sentori-react).
|
|
31
|
+
*
|
|
32
|
+
* import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native'
|
|
33
|
+
* import { useTraceNavigation } from '@goliapkg/sentori-react-native'
|
|
34
|
+
*
|
|
35
|
+
* function App() {
|
|
36
|
+
* const navigationRef = useNavigationContainerRef()
|
|
37
|
+
* useTraceNavigation(navigationRef)
|
|
38
|
+
* return <NavigationContainer ref={navigationRef}>{...}</NavigationContainer>
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* Each span carries `{ from, to }` as tags and uses the destination
|
|
42
|
+
* route name as the span name.
|
|
43
|
+
*/
|
|
44
|
+
export function useTraceNavigation(navigationRef: NavigationRefLike): void {
|
|
45
|
+
// Latest route name we've observed. `null` means "no transition
|
|
46
|
+
// recorded yet" (initial mount).
|
|
47
|
+
const lastRouteRef = useRef<null | string>(null);
|
|
48
|
+
// Span that started when this route was entered. Finished when the
|
|
49
|
+
// NEXT route transition arrives.
|
|
50
|
+
const openSpanRef = useRef<null | SpanHandle>(null);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (typeof navigationRef.addListener !== 'function') return;
|
|
54
|
+
if (typeof navigationRef.getCurrentRoute !== 'function') return;
|
|
55
|
+
|
|
56
|
+
// Seed the "last route" reference from the current state so the
|
|
57
|
+
// first transition emits a span with the right `from`.
|
|
58
|
+
const initial = navigationRef.getCurrentRoute()?.name ?? null;
|
|
59
|
+
lastRouteRef.current = initial;
|
|
60
|
+
|
|
61
|
+
const unsubscribe = navigationRef.addListener('state', () => {
|
|
62
|
+
const next = navigationRef.getCurrentRoute()?.name ?? null;
|
|
63
|
+
const prev = lastRouteRef.current;
|
|
64
|
+
if (next === null || next === prev) return;
|
|
65
|
+
|
|
66
|
+
// Close the prior span (if any) before opening the new one so
|
|
67
|
+
// the trace looks like a sequence, not nested.
|
|
68
|
+
openSpanRef.current?.finish({ status: 'ok' });
|
|
69
|
+
|
|
70
|
+
const span = startSpan('react.navigation', {
|
|
71
|
+
name: prev ? `${prev} → ${next}` : next,
|
|
72
|
+
tags: { 'nav.from': prev ?? '', 'nav.to': next },
|
|
73
|
+
});
|
|
74
|
+
openSpanRef.current = span;
|
|
75
|
+
lastRouteRef.current = next;
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return () => {
|
|
79
|
+
unsubscribe();
|
|
80
|
+
// Close any still-open span on unmount so we don't leak it.
|
|
81
|
+
openSpanRef.current?.finish({ status: 'ok' });
|
|
82
|
+
openSpanRef.current = null;
|
|
83
|
+
};
|
|
84
|
+
}, [navigationRef]);
|
|
85
|
+
}
|