@libo11y/react-native 0.0.0-stage → 0.6.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 +202 -0
- package/README.md +65 -2
- package/dist/contracts/env.d.ts +38 -0
- package/dist/contracts/env.js +5 -0
- package/dist/contracts/storage.d.ts +18 -0
- package/dist/contracts/storage.js +4 -0
- package/dist/device-class.d.ts +7 -0
- package/dist/device-class.js +22 -0
- package/dist/env.d.ts +11 -0
- package/dist/env.js +39 -0
- package/dist/expo/app-info.d.ts +39 -0
- package/dist/expo/app-info.js +40 -0
- package/dist/expo/file-storage.d.ts +45 -0
- package/dist/expo/file-storage.js +93 -0
- package/dist/expo/options.d.ts +23 -0
- package/dist/expo/options.js +40 -0
- package/dist/expo.d.ts +38 -0
- package/dist/expo.js +57 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/instrumentation/network.d.ts +34 -0
- package/dist/instrumentation/network.js +69 -0
- package/dist/internal.d.ts +21 -0
- package/dist/internal.js +18 -0
- package/dist/lifecycle.d.ts +3 -0
- package/dist/lifecycle.js +51 -0
- package/dist/options.d.ts +109 -0
- package/dist/options.js +1 -0
- package/dist/platform.d.ts +33 -0
- package/dist/platform.js +48 -0
- package/dist/random.d.ts +8 -0
- package/dist/random.js +17 -0
- package/dist/resource.d.ts +18 -0
- package/dist/resource.js +31 -0
- package/dist/setup.d.ts +54 -0
- package/dist/setup.js +207 -0
- package/dist/storage/memory.d.ts +7 -0
- package/dist/storage/memory.js +19 -0
- package/dist/storage/queue.d.ts +23 -0
- package/dist/storage/queue.js +253 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +74 -4
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The setup options an Expo app gets from Expo, and how the app's own options go over them.
|
|
3
|
+
*/
|
|
4
|
+
import type { KeyValueStorage } from "../contracts/storage.ts";
|
|
5
|
+
import type { ReactNativeSetupOptions } from "../options.ts";
|
|
6
|
+
import type { ExpoAppInfo } from "./app-info.ts";
|
|
7
|
+
/**
|
|
8
|
+
* The options Expo fills in: where the retry queue lives, the app version, and the update ID and
|
|
9
|
+
* device class as resource attributes.
|
|
10
|
+
*/
|
|
11
|
+
export type ExpoOptions = Pick<ReactNativeSetupOptions, "storage" | "serviceVersion" | "resourceAttributes">;
|
|
12
|
+
/**
|
|
13
|
+
* storage, serviceVersion from info, and resourceAttributes with o11y.rum.app.update_id and
|
|
14
|
+
* o11y.rum.device.class from info. A field info lacks is left out.
|
|
15
|
+
*/
|
|
16
|
+
export declare function expoOptionsFrom(info: ExpoAppInfo, storage: KeyValueStorage): ExpoOptions;
|
|
17
|
+
/**
|
|
18
|
+
* options over expo: an option the app sets beats expo's, and resourceAttributes merge key by key
|
|
19
|
+
* with the app's winning. An option the app leaves undefined keeps expo's. Expo's values are
|
|
20
|
+
* defaults, so an attribute the app sets in env (OTEL_RESOURCE_ATTRIBUTES, unless hermetic) also
|
|
21
|
+
* beats expo's, service.version included, as it would beat a detected one.
|
|
22
|
+
*/
|
|
23
|
+
export declare function withExpoOptions<O extends ReactNativeSetupOptions>(expo: ExpoOptions, options: O): O & ExpoOptions;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The setup options an Expo app gets from Expo, and how the app's own options go over them.
|
|
3
|
+
*/
|
|
4
|
+
import { ATTR_RUM_APP_UPDATE_ID, ATTR_RUM_DEVICE_CLASS } from "@libo11y/spec";
|
|
5
|
+
import { parseResourceAttributes } from "@libo11y/web/portable";
|
|
6
|
+
/**
|
|
7
|
+
* storage, serviceVersion from info, and resourceAttributes with o11y.rum.app.update_id and
|
|
8
|
+
* o11y.rum.device.class from info. A field info lacks is left out.
|
|
9
|
+
*/
|
|
10
|
+
export function expoOptionsFrom(info, storage) {
|
|
11
|
+
const attrs = {
|
|
12
|
+
...(info.updateId === undefined ? {} : { [ATTR_RUM_APP_UPDATE_ID]: info.updateId }),
|
|
13
|
+
...(info.deviceClass === undefined ? {} : { [ATTR_RUM_DEVICE_CLASS]: info.deviceClass }),
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
storage,
|
|
17
|
+
...(info.serviceVersion === undefined ? {} : { serviceVersion: info.serviceVersion }),
|
|
18
|
+
...(Object.keys(attrs).length === 0 ? {} : { resourceAttributes: attrs }),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* options over expo: an option the app sets beats expo's, and resourceAttributes merge key by key
|
|
23
|
+
* with the app's winning. An option the app leaves undefined keeps expo's. Expo's values are
|
|
24
|
+
* defaults, so an attribute the app sets in env (OTEL_RESOURCE_ATTRIBUTES, unless hermetic) also
|
|
25
|
+
* beats expo's, service.version included, as it would beat a detected one.
|
|
26
|
+
*/
|
|
27
|
+
export function withExpoOptions(expo, options) {
|
|
28
|
+
const fromEnv = options.hermetic === true ? {} : parseResourceAttributes(options.env?.OTEL_RESOURCE_ATTRIBUTES);
|
|
29
|
+
const expoAttrs = Object.fromEntries(Object.entries(expo.resourceAttributes ?? {}).filter(([k]) => !(k in fromEnv)));
|
|
30
|
+
const own = Object.fromEntries(Object.entries(options).filter(([, v]) => v !== undefined));
|
|
31
|
+
const attrs = { ...expoAttrs, ...options.resourceAttributes };
|
|
32
|
+
return {
|
|
33
|
+
...(expo.storage === undefined ? {} : { storage: expo.storage }),
|
|
34
|
+
...(expo.serviceVersion === undefined || "service.version" in fromEnv
|
|
35
|
+
? {}
|
|
36
|
+
: { serviceVersion: expo.serviceVersion }),
|
|
37
|
+
...own,
|
|
38
|
+
...(Object.keys(attrs).length === 0 ? {} : { resourceAttributes: attrs }),
|
|
39
|
+
};
|
|
40
|
+
}
|
package/dist/expo.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libo11y React Native platform for Expo apps: setupReactNative with the retry queue in a
|
|
3
|
+
* directory of the app's documents (expo-file-system), the app version (expo-application), the ID
|
|
4
|
+
* of the over-the-air update the app runs (expo-updates) and the device class (expo-device). An
|
|
5
|
+
* app that imports this entry installs those four modules; they are optional peer dependencies of
|
|
6
|
+
* the package, and the main entry imports none of them.
|
|
7
|
+
*/
|
|
8
|
+
import type { KeyValueStorage } from "./contracts/storage.ts";
|
|
9
|
+
import { type ExpoAppInfo } from "./expo/app-info.ts";
|
|
10
|
+
import { type ExpoOptions } from "./expo/options.ts";
|
|
11
|
+
import type { ReactNativeSetupOptions, ReactNativeTelemetry } from "./options.ts";
|
|
12
|
+
export type { KeyValueStorage } from "./contracts/storage.ts";
|
|
13
|
+
export type { ReactNativeConsentOptions, ReactNativeInstrumentationOptions, ReactNativeSetupOptions, ReactNativeTelemetry, ReactNativeTransportOptions, } from "./options.ts";
|
|
14
|
+
export type { ExpoAppInfo, ExpoOptions };
|
|
15
|
+
/**
|
|
16
|
+
* A KeyValueStorage with one file per key in the libo11y directory of the app's documents
|
|
17
|
+
* (Paths.document), which the operating system keeps across launches and updates. The directory
|
|
18
|
+
* is created on the first write, not before. getAllKeys lists only the files of that directory.
|
|
19
|
+
*/
|
|
20
|
+
export declare function expoFileStorage(): KeyValueStorage;
|
|
21
|
+
/**
|
|
22
|
+
* The app version from expo-application, the update ID from expo-updates only when the app runs an
|
|
23
|
+
* over-the-air update (absent for the embedded bundle, in development and with updates disabled),
|
|
24
|
+
* and the device class from expo-device.
|
|
25
|
+
*/
|
|
26
|
+
export declare function expoAppInfo(): ExpoAppInfo;
|
|
27
|
+
/**
|
|
28
|
+
* The options Expo fills in: storage is expoFileStorage(), serviceVersion the app version, and
|
|
29
|
+
* resourceAttributes carry o11y.rum.app.update_id and o11y.rum.device.class from expoAppInfo().
|
|
30
|
+
*/
|
|
31
|
+
export declare function expoOptions(): ExpoOptions;
|
|
32
|
+
/**
|
|
33
|
+
* setupReactNative with expoOptions() under the app's own options: an option the app sets wins,
|
|
34
|
+
* and resourceAttributes merge key by key with the app's winning. So a batch saved just before
|
|
35
|
+
* the app stops goes out on the next launch, and the gateway finds the source maps of the update
|
|
36
|
+
* the app runs.
|
|
37
|
+
*/
|
|
38
|
+
export declare function setupExpo(options?: ReactNativeSetupOptions): Promise<ReactNativeTelemetry>;
|
package/dist/expo.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libo11y React Native platform for Expo apps: setupReactNative with the retry queue in a
|
|
3
|
+
* directory of the app's documents (expo-file-system), the app version (expo-application), the ID
|
|
4
|
+
* of the over-the-air update the app runs (expo-updates) and the device class (expo-device). An
|
|
5
|
+
* app that imports this entry installs those four modules; they are optional peer dependencies of
|
|
6
|
+
* the package, and the main entry imports none of them.
|
|
7
|
+
*/
|
|
8
|
+
/// <reference path="./expo/modules.d.ts" />
|
|
9
|
+
import * as Application from "expo-application";
|
|
10
|
+
import * as Device from "expo-device";
|
|
11
|
+
import { Directory, File, Paths } from "expo-file-system";
|
|
12
|
+
import * as Updates from "expo-updates";
|
|
13
|
+
import { expoAppInfoFrom } from "./expo/app-info.js";
|
|
14
|
+
import { expoFileStorageWith } from "./expo/file-storage.js";
|
|
15
|
+
import { expoOptionsFrom, withExpoOptions } from "./expo/options.js";
|
|
16
|
+
import { setupReactNative } from "./index.js";
|
|
17
|
+
/** The directory under the app's documents that expoFileStorage keeps its files in. */
|
|
18
|
+
const DIRECTORY = "libo11y";
|
|
19
|
+
/**
|
|
20
|
+
* A KeyValueStorage with one file per key in the libo11y directory of the app's documents
|
|
21
|
+
* (Paths.document), which the operating system keeps across launches and updates. The directory
|
|
22
|
+
* is created on the first write, not before. getAllKeys lists only the files of that directory.
|
|
23
|
+
*/
|
|
24
|
+
export function expoFileStorage() {
|
|
25
|
+
const dir = new Directory(Paths.document, DIRECTORY);
|
|
26
|
+
return expoFileStorageWith({
|
|
27
|
+
directoryExists: () => dir.exists,
|
|
28
|
+
createDirectory: () => dir.create({ intermediates: true, idempotent: true }),
|
|
29
|
+
fileNames: () => dir.list().flatMap((e) => (e instanceof File ? [e.name] : [])),
|
|
30
|
+
file: (name) => new File(dir, name),
|
|
31
|
+
replace: (from, to) => new File(dir, from).move(new File(dir, to), { overwrite: true }),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The app version from expo-application, the update ID from expo-updates only when the app runs an
|
|
36
|
+
* over-the-air update (absent for the embedded bundle, in development and with updates disabled),
|
|
37
|
+
* and the device class from expo-device.
|
|
38
|
+
*/
|
|
39
|
+
export function expoAppInfo() {
|
|
40
|
+
return expoAppInfoFrom({ application: Application, updates: Updates, device: Device });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The options Expo fills in: storage is expoFileStorage(), serviceVersion the app version, and
|
|
44
|
+
* resourceAttributes carry o11y.rum.app.update_id and o11y.rum.device.class from expoAppInfo().
|
|
45
|
+
*/
|
|
46
|
+
export function expoOptions() {
|
|
47
|
+
return expoOptionsFrom(expoAppInfo(), expoFileStorage());
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* setupReactNative with expoOptions() under the app's own options: an option the app sets wins,
|
|
51
|
+
* and resourceAttributes merge key by key with the app's winning. So a batch saved just before
|
|
52
|
+
* the app stops goes out on the next launch, and the gateway finds the source maps of the update
|
|
53
|
+
* the app runs.
|
|
54
|
+
*/
|
|
55
|
+
export function setupExpo(options = {}) {
|
|
56
|
+
return setupReactNative(withExpoOptions(expoOptions(), options));
|
|
57
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libo11y React Native platform: setup over the OpenTelemetry JS providers on Hermes, the mobile
|
|
3
|
+
* resource, an OTLP/HTTP transport whose retry queue saves every batch on device storage before it
|
|
4
|
+
* is sent, flushing when the app goes to the background, the consent gate, and fetch and XHR spans
|
|
5
|
+
* with origin-scoped propagation. No RUM features: those come with @libo11y/rum-react-native.
|
|
6
|
+
*/
|
|
7
|
+
export { type LifecycleOptions, type Logger, O11yError, type O11yErrorCode, type SignalReport, type Telemetry, type TelemetryOptions, } from "@libo11y/core";
|
|
8
|
+
export type { Consent, ConsentState, UrlMatcher } from "@libo11y/web";
|
|
9
|
+
import type { ReactNativeSetupOptions, ReactNativeTelemetry } from "./options.ts";
|
|
10
|
+
export type { KeyValueStorage } from "./contracts/storage.ts";
|
|
11
|
+
export type { ReactNativeConsentOptions, ReactNativeInstrumentationOptions, ReactNativeSetupOptions, ReactNativeTelemetry, ReactNativeTransportOptions, } from "./options.ts";
|
|
12
|
+
export { memoryStorage } from "./storage/memory.ts";
|
|
13
|
+
export { VERSION } from "./version.ts";
|
|
14
|
+
/**
|
|
15
|
+
* Builds React Native telemetry and installs it globally, as the server's and the web's setup do:
|
|
16
|
+
* the providers, the propagator and the context manager, the fetch and XMLHttpRequest patches as
|
|
17
|
+
* options.instrumentations says, flushing when the app goes to the background (AppState) and
|
|
18
|
+
* draining the retry queue when it comes back. Nothing is recorded, stored or sent until consent
|
|
19
|
+
* is granted, unless consent.initial is "granted". Every batch is saved in options.storage before it
|
|
20
|
+
* is sent, so a batch exported just before the app stops goes out on the next launch. Every
|
|
21
|
+
* endpoint must be an absolute http or https URL. Calling it twice without a shutdown rejects with
|
|
22
|
+
* ALREADY_SETUP.
|
|
23
|
+
*/
|
|
24
|
+
export declare function setupReactNative(options?: ReactNativeSetupOptions): Promise<ReactNativeTelemetry>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libo11y React Native platform: setup over the OpenTelemetry JS providers on Hermes, the mobile
|
|
3
|
+
* resource, an OTLP/HTTP transport whose retry queue saves every batch on device storage before it
|
|
4
|
+
* is sent, flushing when the app goes to the background, the consent gate, and fetch and XHR spans
|
|
5
|
+
* with origin-scoped propagation. No RUM features: those come with @libo11y/rum-react-native.
|
|
6
|
+
*/
|
|
7
|
+
export { O11yError, } from "@libo11y/core";
|
|
8
|
+
import { defaultReactNativeDeps } from "./env.js";
|
|
9
|
+
import { setupReactNativeWith } from "./setup.js";
|
|
10
|
+
export { memoryStorage } from "./storage/memory.js";
|
|
11
|
+
export { VERSION } from "./version.js";
|
|
12
|
+
/**
|
|
13
|
+
* Builds React Native telemetry and installs it globally, as the server's and the web's setup do:
|
|
14
|
+
* the providers, the propagator and the context manager, the fetch and XMLHttpRequest patches as
|
|
15
|
+
* options.instrumentations says, flushing when the app goes to the background (AppState) and
|
|
16
|
+
* draining the retry queue when it comes back. Nothing is recorded, stored or sent until consent
|
|
17
|
+
* is granted, unless consent.initial is "granted". Every batch is saved in options.storage before it
|
|
18
|
+
* is sent, so a batch exported just before the app stops goes out on the next launch. Every
|
|
19
|
+
* endpoint must be an absolute http or https URL. Calling it twice without a shutdown rejects with
|
|
20
|
+
* ALREADY_SETUP.
|
|
21
|
+
*/
|
|
22
|
+
export function setupReactNative(options = {}) {
|
|
23
|
+
return setupReactNativeWith(options, defaultReactNativeDeps(), true);
|
|
24
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch and XHR client spans on React Native, with the web instrumentation from
|
|
3
|
+
* @libo11y/web/portable under the scope @libo11y/react-native. The global fetch and
|
|
4
|
+
* XMLHttpRequest are patched separately, because Expo replaces fetch with expo/fetch, which uses
|
|
5
|
+
* no XHR, while bare React Native's fetch is whatwg-fetch over XHR. While a traced fetch call runs
|
|
6
|
+
* synchronously (whatwg-fetch opens and sends its XHR inside the call), the XHR patch records
|
|
7
|
+
* nothing, so one request is one span either way. traceFetch wraps a fetch the patch cannot reach,
|
|
8
|
+
* such as expo/fetch imported directly; a traced fetch that calls another traced fetch, such as an
|
|
9
|
+
* app's wrapper over the patched global, records only the outer call.
|
|
10
|
+
*/
|
|
11
|
+
import { type HttpInstrumentationOptions } from "@libo11y/web/portable";
|
|
12
|
+
/** The instrumentation scope of the React Native HTTP spans and duration histogram. */
|
|
13
|
+
export declare const RN_SCOPE = "@libo11y/react-native";
|
|
14
|
+
/** The web HTTP options, plus which globals to patch. */
|
|
15
|
+
export interface NetworkInstrumentationOptions extends HttpInstrumentationOptions {
|
|
16
|
+
/** Patch the global fetch. Default true. */
|
|
17
|
+
readonly fetch?: boolean;
|
|
18
|
+
/** Patch globalThis.XMLHttpRequest. Default true. */
|
|
19
|
+
readonly xhr?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** The installed instrumentation. */
|
|
22
|
+
export interface NetworkInstrumentation {
|
|
23
|
+
/** Restores the globals that still hold the patches; every wrapper then passes calls straight through. */
|
|
24
|
+
uninstall(): void;
|
|
25
|
+
/** fetchImpl with a client span per request; a wrapped fetch is returned as it is. */
|
|
26
|
+
traceFetch<F extends typeof fetch>(fetchImpl: F): F;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Patches g.fetch (g defaults to globalThis) and globalThis.XMLHttpRequest as opts says, and
|
|
30
|
+
* returns the uninstall and traceFetch.
|
|
31
|
+
*/
|
|
32
|
+
export declare function installNetworkInstrumentation(opts: NetworkInstrumentationOptions, g?: {
|
|
33
|
+
fetch?: typeof fetch;
|
|
34
|
+
}): NetworkInstrumentation;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch and XHR client spans on React Native, with the web instrumentation from
|
|
3
|
+
* @libo11y/web/portable under the scope @libo11y/react-native. The global fetch and
|
|
4
|
+
* XMLHttpRequest are patched separately, because Expo replaces fetch with expo/fetch, which uses
|
|
5
|
+
* no XHR, while bare React Native's fetch is whatwg-fetch over XHR. While a traced fetch call runs
|
|
6
|
+
* synchronously (whatwg-fetch opens and sends its XHR inside the call), the XHR patch records
|
|
7
|
+
* nothing, so one request is one span either way. traceFetch wraps a fetch the patch cannot reach,
|
|
8
|
+
* such as expo/fetch imported directly; a traced fetch that calls another traced fetch, such as an
|
|
9
|
+
* app's wrapper over the patched global, records only the outer call.
|
|
10
|
+
*/
|
|
11
|
+
import { installXhrInstrumentationWith, instrumentFetchWith, } from "@libo11y/web/portable";
|
|
12
|
+
/** The instrumentation scope of the React Native HTTP spans and duration histogram. */
|
|
13
|
+
export const RN_SCOPE = "@libo11y/react-native";
|
|
14
|
+
/**
|
|
15
|
+
* Patches g.fetch (g defaults to globalThis) and globalThis.XMLHttpRequest as opts says, and
|
|
16
|
+
* returns the uninstall and traceFetch.
|
|
17
|
+
*/
|
|
18
|
+
export function installNetworkInstrumentation(opts, g = globalThis) {
|
|
19
|
+
// How many traced fetch calls are running synchronously right now.
|
|
20
|
+
let depth = 0;
|
|
21
|
+
let done = false;
|
|
22
|
+
const xhrInternals = { scope: RN_SCOPE, skip: () => depth > 0 };
|
|
23
|
+
const wrappers = new WeakMap();
|
|
24
|
+
const wrapped = new WeakSet();
|
|
25
|
+
const traceFetch = (f) => {
|
|
26
|
+
if (wrapped.has(f))
|
|
27
|
+
return f;
|
|
28
|
+
const known = wrappers.get(f);
|
|
29
|
+
if (known !== undefined)
|
|
30
|
+
return known;
|
|
31
|
+
const traced = instrumentFetchWith(f, opts, { scope: RN_SCOPE });
|
|
32
|
+
const out = ((input, init) => {
|
|
33
|
+
if (done || depth > 0)
|
|
34
|
+
return f(input, init);
|
|
35
|
+
depth++;
|
|
36
|
+
try {
|
|
37
|
+
return traced(input, init);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
depth--;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
wrappers.set(f, out);
|
|
44
|
+
wrapped.add(out);
|
|
45
|
+
return out;
|
|
46
|
+
};
|
|
47
|
+
const undo = [];
|
|
48
|
+
if (opts.fetch !== false && typeof g.fetch === "function") {
|
|
49
|
+
const previous = g.fetch;
|
|
50
|
+
const patched = traceFetch(previous);
|
|
51
|
+
g.fetch = patched;
|
|
52
|
+
undo.push(() => {
|
|
53
|
+
if (g.fetch === patched)
|
|
54
|
+
g.fetch = previous;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (opts.xhr !== false)
|
|
58
|
+
undo.push(installXhrInstrumentationWith(opts, xhrInternals));
|
|
59
|
+
return {
|
|
60
|
+
uninstall: () => {
|
|
61
|
+
if (done)
|
|
62
|
+
return;
|
|
63
|
+
done = true;
|
|
64
|
+
for (const u of undo.reverse())
|
|
65
|
+
u();
|
|
66
|
+
},
|
|
67
|
+
traceFetch,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @libo11y/react-native/internal: the platform's building blocks, for @libo11y/rum-react-native.
|
|
3
|
+
* For libo11y packages only: it follows the package version and carries no compatibility promise.
|
|
4
|
+
* Nothing reachable from here imports react-native or Expo (boundary.test.ts enforces that), so
|
|
5
|
+
* node tests can load it; the binding takes the real env from ./env.
|
|
6
|
+
*/
|
|
7
|
+
export type { AppLifecycle, AppStateLike, ReactNativeEnv } from "./contracts/env.ts";
|
|
8
|
+
export type { KeyValueStorage } from "./contracts/storage.ts";
|
|
9
|
+
export { deviceClassOf } from "./device-class.ts";
|
|
10
|
+
export { type ExpoAppInfo, type ExpoModulesLike, expoAppInfoFrom } from "./expo/app-info.ts";
|
|
11
|
+
export { expoFileStorageWith, type FileLike, type FileSystemLike } from "./expo/file-storage.ts";
|
|
12
|
+
export { type ExpoOptions, expoOptionsFrom, withExpoOptions } from "./expo/options.ts";
|
|
13
|
+
export { installNetworkInstrumentation, type NetworkInstrumentation, type NetworkInstrumentationOptions, RN_SCOPE, } from "./instrumentation/network.ts";
|
|
14
|
+
export { appStateLifecycle } from "./lifecycle.ts";
|
|
15
|
+
export type { ReactNativeConsentOptions, ReactNativeInstrumentationOptions, ReactNativeSetupOptions, ReactNativeTelemetry, ReactNativeTransportOptions, } from "./options.ts";
|
|
16
|
+
export { MOBILE_ENV_DEFAULTS, type ReactNativePlatformOptions, reactNativePlatform } from "./platform.ts";
|
|
17
|
+
export { uuidCrypto } from "./random.ts";
|
|
18
|
+
export { detectReactNativeResource, type ReactNativeResourceOptions } from "./resource.ts";
|
|
19
|
+
export { type ReactNativeDeps, setupReactNativeWith } from "./setup.ts";
|
|
20
|
+
export { memoryStorage } from "./storage/memory.ts";
|
|
21
|
+
export { openStorageQueue, type StorageQueueOptions } from "./storage/queue.ts";
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @libo11y/react-native/internal: the platform's building blocks, for @libo11y/rum-react-native.
|
|
3
|
+
* For libo11y packages only: it follows the package version and carries no compatibility promise.
|
|
4
|
+
* Nothing reachable from here imports react-native or Expo (boundary.test.ts enforces that), so
|
|
5
|
+
* node tests can load it; the binding takes the real env from ./env.
|
|
6
|
+
*/
|
|
7
|
+
export { deviceClassOf } from "./device-class.js";
|
|
8
|
+
export { expoAppInfoFrom } from "./expo/app-info.js";
|
|
9
|
+
export { expoFileStorageWith } from "./expo/file-storage.js";
|
|
10
|
+
export { expoOptionsFrom, withExpoOptions } from "./expo/options.js";
|
|
11
|
+
export { installNetworkInstrumentation, RN_SCOPE, } from "./instrumentation/network.js";
|
|
12
|
+
export { appStateLifecycle } from "./lifecycle.js";
|
|
13
|
+
export { MOBILE_ENV_DEFAULTS, reactNativePlatform } from "./platform.js";
|
|
14
|
+
export { uuidCrypto } from "./random.js";
|
|
15
|
+
export { detectReactNativeResource } from "./resource.js";
|
|
16
|
+
export { setupReactNativeWith } from "./setup.js";
|
|
17
|
+
export { memoryStorage } from "./storage/memory.js";
|
|
18
|
+
export { openStorageQueue } from "./storage/queue.js";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app lifecycle from AppState: background, and inactive on iOS (the app switcher, an incoming
|
|
3
|
+
* call, Control Center), hide the app; active, or any other state, shows it. Listeners run in
|
|
4
|
+
* registration order, once per change between visible and hidden. AppState is listened to only
|
|
5
|
+
* while someone subscribes, so the lifecycle needs no close.
|
|
6
|
+
*/
|
|
7
|
+
import { diag } from "@opentelemetry/api";
|
|
8
|
+
const HIDDEN = new Set(["background", "inactive"]);
|
|
9
|
+
const isVisible = (state) => !HIDDEN.has(state ?? "active");
|
|
10
|
+
/** An AppLifecycle over appState. */
|
|
11
|
+
export function appStateLifecycle(appState) {
|
|
12
|
+
const listeners = [];
|
|
13
|
+
let sub;
|
|
14
|
+
// What the listeners last heard, so repeated states and active to active call nothing.
|
|
15
|
+
let notified = isVisible(appState.currentState);
|
|
16
|
+
const onChange = (state) => {
|
|
17
|
+
const next = isVisible(state);
|
|
18
|
+
if (next === notified)
|
|
19
|
+
return;
|
|
20
|
+
notified = next;
|
|
21
|
+
for (const l of [...listeners]) {
|
|
22
|
+
try {
|
|
23
|
+
l(next);
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
diag.warn("libo11y: react-native: a lifecycle listener threw", err);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
visible: () => isVisible(appState.currentState),
|
|
32
|
+
subscribe(l) {
|
|
33
|
+
if (sub === undefined) {
|
|
34
|
+
notified = isVisible(appState.currentState);
|
|
35
|
+
sub = appState.addEventListener("change", onChange);
|
|
36
|
+
}
|
|
37
|
+
listeners.push(l);
|
|
38
|
+
let active = true;
|
|
39
|
+
return () => {
|
|
40
|
+
if (!active)
|
|
41
|
+
return;
|
|
42
|
+
active = false;
|
|
43
|
+
listeners.splice(listeners.indexOf(l), 1);
|
|
44
|
+
if (listeners.length === 0) {
|
|
45
|
+
sub?.remove();
|
|
46
|
+
sub = undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public options of @libo11y/react-native and its telemetry handle.
|
|
3
|
+
*/
|
|
4
|
+
import type { Telemetry, TelemetryOptions } from "@libo11y/core";
|
|
5
|
+
import type { Consent, ConsentState, UrlMatcher } from "@libo11y/web";
|
|
6
|
+
import type { ContextManager } from "@opentelemetry/api";
|
|
7
|
+
import type { KeyValueStorage } from "./contracts/storage.ts";
|
|
8
|
+
/**
|
|
9
|
+
* Consent on React Native: an initial state, and no browser privacy signals, because a React
|
|
10
|
+
* Native runtime has none.
|
|
11
|
+
*/
|
|
12
|
+
export interface ReactNativeConsentOptions {
|
|
13
|
+
/** Default "pending": nothing is recorded, stored or sent until consent.grant(). */
|
|
14
|
+
readonly initial?: ConsentState;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* How the OTLP/HTTP transport sends and retries. Every batch is saved in the retry queue before it
|
|
18
|
+
* is sent, so a batch exported just before the app is suspended or stopped goes out on the next
|
|
19
|
+
* launch.
|
|
20
|
+
*/
|
|
21
|
+
export interface ReactNativeTransportOptions {
|
|
22
|
+
/** The most bytes the retry queue holds; the oldest batches go first. Default 2097152 (2 MiB). */
|
|
23
|
+
readonly maxQueueBytes?: number;
|
|
24
|
+
/** The most batches the retry queue holds. Default 500. */
|
|
25
|
+
readonly maxQueueEntries?: number;
|
|
26
|
+
/** Queued batches older than this are dropped. Default 86400000 (24 hours). */
|
|
27
|
+
readonly maxQueueAgeMillis?: number;
|
|
28
|
+
/** The timeout of one request. Default 10000. */
|
|
29
|
+
readonly timeoutMillis?: number;
|
|
30
|
+
/** The first retry delay, doubled per attempt with full jitter. Default 1000. */
|
|
31
|
+
readonly initialBackoffMillis?: number;
|
|
32
|
+
/** The largest retry delay. Default 300000. */
|
|
33
|
+
readonly maxBackoffMillis?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Which requests get client spans.
|
|
37
|
+
*/
|
|
38
|
+
export interface ReactNativeInstrumentationOptions {
|
|
39
|
+
/** Patch the global fetch. Default true. */
|
|
40
|
+
readonly fetch?: boolean;
|
|
41
|
+
/** Patch the global XMLHttpRequest. Default true. */
|
|
42
|
+
readonly xhr?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The origins that receive traceparent, tracestate and baggage, such as
|
|
45
|
+
* "https://api.example.com". Default none, because an app has no origin of its own.
|
|
46
|
+
*/
|
|
47
|
+
readonly propagateTo?: readonly UrlMatcher[];
|
|
48
|
+
/** URLs that get no span. The OTLP endpoints never do. */
|
|
49
|
+
readonly ignoreUrls?: readonly UrlMatcher[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The options of setupReactNative: core's TelemetryOptions plus where and how to export.
|
|
53
|
+
*/
|
|
54
|
+
export interface ReactNativeSetupOptions extends TelemetryOptions {
|
|
55
|
+
/**
|
|
56
|
+
* The OTLP/HTTP base URL, absolute, such as "https://app.example.com/_o11y". Signals post to
|
|
57
|
+
* `<endpoint>/v1/traces`, `/v1/metrics` and `/v1/logs`. An app has no page to resolve a relative
|
|
58
|
+
* URL against, so every endpoint must be an absolute http or https URL.
|
|
59
|
+
*/
|
|
60
|
+
readonly endpoint?: string;
|
|
61
|
+
/** The full traces URL, used as is. Beats endpoint. */
|
|
62
|
+
readonly tracesEndpoint?: string;
|
|
63
|
+
/** The full metrics URL, used as is. Beats endpoint. */
|
|
64
|
+
readonly metricsEndpoint?: string;
|
|
65
|
+
/** The full logs URL, used as is. Beats endpoint. */
|
|
66
|
+
readonly logsEndpoint?: string;
|
|
67
|
+
/** Headers for every export request. */
|
|
68
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
69
|
+
/**
|
|
70
|
+
* OTEL_* variables, read as the server reads process.env, because an app has no process
|
|
71
|
+
* environment. Options beat them. Ignored when hermetic is true. Default: none.
|
|
72
|
+
*/
|
|
73
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
74
|
+
/** The consent the telemetry starts with. */
|
|
75
|
+
readonly consent?: ReactNativeConsentOptions;
|
|
76
|
+
/**
|
|
77
|
+
* Where the retry queue lives. Default memoryStorage(), which is lost when the app stops.
|
|
78
|
+
* AsyncStorage satisfies KeyValueStorage as it is.
|
|
79
|
+
*/
|
|
80
|
+
readonly storage?: KeyValueStorage;
|
|
81
|
+
/** How batches are sent, queued and retried. */
|
|
82
|
+
readonly transport?: ReactNativeTransportOptions;
|
|
83
|
+
/** Which requests get client spans, and which origins receive trace context. */
|
|
84
|
+
readonly instrumentations?: ReactNativeInstrumentationOptions;
|
|
85
|
+
/**
|
|
86
|
+
* The context manager. Default upstream's StackContextManager, which carries context through
|
|
87
|
+
* synchronous code only, not across await.
|
|
88
|
+
*/
|
|
89
|
+
readonly contextManager?: () => ContextManager;
|
|
90
|
+
/**
|
|
91
|
+
* Random bytes for IDs, such as expo-crypto's getRandomValues. Default crypto.getRandomValues
|
|
92
|
+
* where the runtime has it, else Math.random. These IDs are not secrets.
|
|
93
|
+
*/
|
|
94
|
+
readonly getRandomValues?: (array: Uint8Array) => Uint8Array;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The handle setupReactNative resolves with: core's Telemetry with the consent and traceFetch.
|
|
98
|
+
*/
|
|
99
|
+
export interface ReactNativeTelemetry extends Telemetry {
|
|
100
|
+
/** The application's handle on consent: grant, revoke, state and subscribe. */
|
|
101
|
+
readonly consent: Consent;
|
|
102
|
+
/**
|
|
103
|
+
* Wraps a fetch the global patch does not reach, such as expo/fetch imported directly, so its
|
|
104
|
+
* requests get spans and traceparent like the global fetch's. A fetch wrapped already, the
|
|
105
|
+
* patched global fetch included, is returned unchanged. Without the global install
|
|
106
|
+
* (setupReactNative installs it), fetchImpl is returned as it is.
|
|
107
|
+
*/
|
|
108
|
+
traceFetch<F extends typeof fetch>(fetchImpl: F): F;
|
|
109
|
+
}
|
package/dist/options.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The React Native Platform core builds on. */
|
|
2
|
+
import { type Platform, type RecordingGate } from "@libo11y/core";
|
|
3
|
+
import { type createExporterSet, type Delivery, type SignalEndpoints } from "@libo11y/web/portable";
|
|
4
|
+
import type { ReactNativeEnv } from "./contracts/env.ts";
|
|
5
|
+
import type { ReactNativeSetupOptions } from "./options.ts";
|
|
6
|
+
/**
|
|
7
|
+
* OTEL_* defaults suited to a phone, under the options' env: span and log batches every 10 seconds
|
|
8
|
+
* in batches of at most 128, queues of 512, metrics every 60 seconds. Fewer, larger requests save
|
|
9
|
+
* the radio and the JavaScript thread.
|
|
10
|
+
*/
|
|
11
|
+
export declare const MOBILE_ENV_DEFAULTS: Readonly<Record<string, string>>;
|
|
12
|
+
/** Options for reactNativePlatform. */
|
|
13
|
+
export interface ReactNativePlatformOptions {
|
|
14
|
+
readonly exportOptions: ReactNativeSetupOptions;
|
|
15
|
+
/** The resolved endpoints. */
|
|
16
|
+
readonly endpoints: SignalEndpoints;
|
|
17
|
+
/** The delivery the exporters use. */
|
|
18
|
+
readonly delivery: Delivery;
|
|
19
|
+
/** The consent gate, passed to core as Platform.gate. */
|
|
20
|
+
readonly gate: RecordingGate;
|
|
21
|
+
readonly env: ReactNativeEnv;
|
|
22
|
+
readonly createExporterSet: typeof createExporterSet;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A React Native Platform, named "react-native": env from browserEnv over MOBILE_ENV_DEFAULTS,
|
|
26
|
+
* detectReactNativeResource, the options' context manager (default upstream's
|
|
27
|
+
* StackContextManager), the exporter set over the delivery with the gate as its metric consent
|
|
28
|
+
* boundary, console lines through the console
|
|
29
|
+
* methods captured when the platform is built (so captureConsole cannot recurse), core's own SHA-1
|
|
30
|
+
* (Hermes has no Web Crypto), UUIDs from uuidCrypto, the global console for captureConsole, and
|
|
31
|
+
* the gate. No runtime metrics.
|
|
32
|
+
*/
|
|
33
|
+
export declare function reactNativePlatform(o: ReactNativePlatformOptions): Platform;
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** The React Native Platform core builds on. */
|
|
2
|
+
import { sha1 } from "@libo11y/core";
|
|
3
|
+
import { browserEnv, createContextManager, randomUuid, } from "@libo11y/web/portable";
|
|
4
|
+
import { uuidCrypto } from "./random.js";
|
|
5
|
+
import { detectReactNativeResource } from "./resource.js";
|
|
6
|
+
/**
|
|
7
|
+
* OTEL_* defaults suited to a phone, under the options' env: span and log batches every 10 seconds
|
|
8
|
+
* in batches of at most 128, queues of 512, metrics every 60 seconds. Fewer, larger requests save
|
|
9
|
+
* the radio and the JavaScript thread.
|
|
10
|
+
*/
|
|
11
|
+
export const MOBILE_ENV_DEFAULTS = {
|
|
12
|
+
OTEL_BSP_SCHEDULE_DELAY: "10000",
|
|
13
|
+
OTEL_BLRP_SCHEDULE_DELAY: "10000",
|
|
14
|
+
OTEL_BSP_MAX_QUEUE_SIZE: "512",
|
|
15
|
+
OTEL_BLRP_MAX_QUEUE_SIZE: "512",
|
|
16
|
+
OTEL_BSP_MAX_EXPORT_BATCH_SIZE: "128",
|
|
17
|
+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE: "128",
|
|
18
|
+
OTEL_METRIC_EXPORT_INTERVAL: "60000",
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A React Native Platform, named "react-native": env from browserEnv over MOBILE_ENV_DEFAULTS,
|
|
22
|
+
* detectReactNativeResource, the options' context manager (default upstream's
|
|
23
|
+
* StackContextManager), the exporter set over the delivery with the gate as its metric consent
|
|
24
|
+
* boundary, console lines through the console
|
|
25
|
+
* methods captured when the platform is built (so captureConsole cannot recurse), core's own SHA-1
|
|
26
|
+
* (Hermes has no Web Crypto), UUIDs from uuidCrypto, the global console for captureConsole, and
|
|
27
|
+
* the gate. No runtime metrics.
|
|
28
|
+
*/
|
|
29
|
+
export function reactNativePlatform(o) {
|
|
30
|
+
const vars = browserEnv(o.exportOptions, o.endpoints, MOBILE_ENV_DEFAULTS);
|
|
31
|
+
const random = uuidCrypto(o.exportOptions.getRandomValues);
|
|
32
|
+
// Bound now, before captureConsole patches the console, so a sink line never re-enters capture.
|
|
33
|
+
const log = console.log.bind(console);
|
|
34
|
+
const warn = console.warn.bind(console);
|
|
35
|
+
return {
|
|
36
|
+
name: "react-native",
|
|
37
|
+
env: () => vars,
|
|
38
|
+
detectResource: (hermetic) => detectReactNativeResource({ env: o.env, hermetic, vars }),
|
|
39
|
+
contextManager: o.exportOptions.contextManager ?? createContextManager,
|
|
40
|
+
buildExporters: (req) => o.createExporterSet(req, o.delivery, o.endpoints, { gate: o.gate }),
|
|
41
|
+
writeConsoleLine: (line) => log(line),
|
|
42
|
+
writeDiagnosticLine: (line) => warn(line),
|
|
43
|
+
sha1: async (data) => sha1(data),
|
|
44
|
+
randomUUID: () => randomUuid(random),
|
|
45
|
+
console: globalThis.console,
|
|
46
|
+
gate: o.gate,
|
|
47
|
+
};
|
|
48
|
+
}
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The random source React Native IDs come from. */
|
|
2
|
+
import type { UuidCrypto } from "@libo11y/web/portable";
|
|
3
|
+
/**
|
|
4
|
+
* A source for randomUuid: the app's getRandomValues (such as expo-crypto's), else the runtime's
|
|
5
|
+
* crypto when it has getRandomValues, else an empty source, from which randomUuid uses Math.random.
|
|
6
|
+
* Hermes has no Web Crypto. These IDs are not secrets.
|
|
7
|
+
*/
|
|
8
|
+
export declare function uuidCrypto(getRandomValues?: (array: Uint8Array) => Uint8Array): Partial<UuidCrypto>;
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A source for randomUuid: the app's getRandomValues (such as expo-crypto's), else the runtime's
|
|
3
|
+
* crypto when it has getRandomValues, else an empty source, from which randomUuid uses Math.random.
|
|
4
|
+
* Hermes has no Web Crypto. These IDs are not secrets.
|
|
5
|
+
*/
|
|
6
|
+
export function uuidCrypto(getRandomValues) {
|
|
7
|
+
if (getRandomValues !== undefined) {
|
|
8
|
+
return {
|
|
9
|
+
getRandomValues: (a) => {
|
|
10
|
+
getRandomValues(a);
|
|
11
|
+
return a;
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const c = globalThis.crypto;
|
|
16
|
+
return typeof c?.getRandomValues === "function" ? c : {};
|
|
17
|
+
}
|