RUM 配置¶
RUM 初始化配置¶
let rumConfig: FTRUMConfig = {
androidAppId: Config.ANDROID_APP_ID,
iOSAppId: Config.IOS_APP_ID,
enableAutoTrackUserAction: true,
enableAutoTrackError: true,
enableNativeUserAction: true,
enableNativeUserView: false,
enableNativeUserResource: true,
errorMonitorType: ErrorMonitorType.all,
deviceMonitorType: DeviceMetricsMonitorType.all,
detectFrequency: DetectFrequency.rare,
};
await FTReactNativeRUM.setConfig(rumConfig);
| 字段 | 类型 | 必须 | 说明 |
|---|---|---|---|
| androidAppId | string | 是 | app_id,应用访问监测控制台申请 |
| iOSAppId | string | 是 | app_id,应用访问监测控制台申请 |
| sampleRate | number | 否 | 采样率,取值范围 [0,1],0 表示不采集,1 表示全采集,默认值为 1。作用域为同一 session_id 下所有 View、Action、LongTask、Error 数据 |
| sessionOnErrorSampleRate | number | 否 | 设置错误采集率,当会话未被 sampleRate 采样时,若会话期间发生错误,可以采集到错误前 1 分钟范围的数据,取值范围 [0,1],0 表示不采集,1 表示全采集,默认值为 0。作用域为同一 session_id 下所有 View、Action、LongTask、Error 数据。SDK 0.3.14 以上支持 |
| enableAutoTrackUserAction | boolean | 否 | 是否自动采集 React Native 组件点击事件,开启后可配合 accessibilityLabel 设置 actionName |
| enableAutoTrackError | boolean | 否 | 是否自动采集 React Native Error,默认 false |
| enableNativeUserAction | boolean | 否 | 是否进行 Native Action 追踪,原生 Button 点击事件、应用启动事件,默认 false |
| enableNativeUserView | boolean | 否 | 是否进行 Native View 自动追踪,纯 React Native 应用建议关闭,默认 false |
| enableNativeUserResource | boolean | 否 | 是否开启 Native Resource 自动追踪。由于 React Native 的网络请求在 iOS、Android 端使用系统 API 实现,开启后,所有 resource 数据能够一并采集。默认 false |
| errorMonitorType | enum ErrorMonitorType | 否 | 设置辅助监控信息,添加附加监控数据到 RUM Error 数据中,默认不开启 |
| deviceMonitorType | enum DeviceMetricsMonitorType | 否 | 在 View 周期中添加监控数据,默认不开启 |
| detectFrequency | enum DetectFrequency | 否 | 视图性能监控采样周期,默认为 DetectFrequency.normal |
| enableResourceHostIP | boolean | 否 | 是否采集请求目标域名地址的 IP。作用域:只影响 enableNativeUserResource = true 的默认采集。iOS:>= iOS 13 支持;Android:同一 OkhttpClient 存在 IP 缓存机制 |
| globalContext | object | 否 | 添加自定义标签,用于用户监测数据源区分。如果需要使用追踪功能,则参数 key 为 track_id,value 为任意数值。添加规则注意事项请查阅此处 |
| enableTrackNativeCrash | boolean | 否 | 是否开启 Android Java Crash 和 OC/C/C++ 崩溃监测,默认 false |
| enableTrackNativeAppANR | boolean | 否 | 是否开启 Native ANR 监测,默认 false |
| enableTrackNativeFreeze | boolean | 否 | 是否采集 Native Freeze,默认 false |
| nativeFreezeDurationMs | number | 否 | 设置采集 Native Freeze 卡顿阈值,取值范围 [100,),单位毫秒。iOS 默认 250ms,Android 默认 1000ms |
| rumDiscardStrategy | string | 否 | 丢弃策略:FTRUMCacheDiscard.discard 丢弃新数据(默认)、FTRUMCacheDiscard.discardOldest 丢弃旧数据 |
| rumCacheLimitCount | number | 否 | 本地缓存最大 RUM 条目数量限制 [10_000,),默认 100_000 |
| enableTraceWebView | boolean | 否 | 设置开启采集 WebView 数据,默认 true。SDK 0.3.16 以上支持 |
| allowWebViewHost | Array |
否 | 设置允许数据追踪的 WebView host 地址,null 时全采集,默认为 null。SDK 0.3.16 以上支持 |
| iosCrashMonitoringType | enum IOSCrashMonitoringType | 否 | 配置 SDK 崩溃监控的类型范围,默认为 IOSCrashMonitoringType.highCompatibility。SDK 0.3.16 以上支持 |
RUM 用户数据追踪¶
FTRUMConfig 可通过 enableAutoTrackUserAction、enableAutoTrackError、enableNativeUserAction、enableNativeUserView、enableNativeUserResource 等配置实现自动采集;如果需要手动采集,也可使用以下 API。
View¶
在 SDK 初始化 RUM 配置 时可配置 enableNativeUserView 开启自动采集 Native View。React Native View 由于 React Native 提供了广泛的库来创建屏幕导航,所以默认情况下只支持手动采集;如果需要自动采集 React Native View,可继续阅读下方“自动采集 React Native View”。
使用方法¶
/**
* view 加载时长。
*/
onCreateView(viewName: string, loadTime: number): Promise<void>;
/**
* view 开始。
*/
startView(viewName: string, property?: object): Promise<void>;
/**
* view 结束。
*/
stopView(property?: object): Promise<void>;
使用示例¶
import { FTReactNativeRUM } from '@cloudcare/react-native-mobile';
FTReactNativeRUM.onCreateView('viewName', duration);
FTReactNativeRUM.startView('viewName', { 'custom.foo': 'something' });
FTReactNativeRUM.stopView({ 'custom.foo': 'something' });
自动采集 React Native View¶
如果您在 React Native 中使用 react-native-navigation、react-navigation 或 Expo Router 导航组件,可以参考下面方式进行 React Native View 的自动采集。
react-native-navigation¶
将 example 中 FTRumReactNavigationTracking.tsx 文件添加到您的工程;
调用 FTRumReactNativeNavigationTracking.startTracking() 方法,开启采集。
import { FTRumReactNativeNavigationTracking } from './FTRumReactNativeNavigationTracking';
function startReactNativeNavigation() {
FTRumReactNativeNavigationTracking.startTracking();
registerScreens();
Navigation.events().registerAppLaunchedListener(async () => {
await Navigation.setRoot({
root: {
stack: {
children: [{ component: { name: 'Home' } }],
},
},
});
});
}
react-navigation¶
将 example 中 FTRumReactNavigationTracking.tsx 文件添加到您的工程;
- 方法一:
如果您使用 createNativeStackNavigator(); 创建原生导航堆栈,建议采用添加 screenListeners 方式开启采集,这样可以统计到页面的加载时长,具体使用如下:
import { FTRumReactNavigationTracking } from './FTRumReactNavigationTracking';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
<Stack.Navigator
screenListeners={FTRumReactNavigationTracking.StackListener}
initialRouteName="Home"
>
<Stack.Screen name="Home" component={Home} options={{ headerShown: false }} />
<Stack.Screen name="Mine" component={Mine} options={{ title: 'Mine' }} />
</Stack.Navigator>;
- 方法二:
如果没有使用 createNativeStackNavigator();,需要在 NavigationContainer 组件中添加自动采集方法,如下:
注意:此方法无法采集页面加载时长
import { FTRumReactNavigationTracking } from './FTRumReactNavigationTracking';
import type { NavigationContainerRef } from '@react-navigation/native';
const navigationRef: React.RefObject<
NavigationContainerRef<ReactNavigation.RootParamList>
> = React.createRef();
<NavigationContainer
ref={navigationRef}
onReady={() => {
FTRumReactNavigationTracking.startTrackingViews(navigationRef.current);
}}
>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={Home} options={{ headerShown: false }} />
<Stack.Screen name="Mine" component={Mine} options={{ title: 'Mine' }} />
</Stack.Navigator>
</NavigationContainer>;
具体使用示例可以参考 example。
Expo Router¶
如果您使用的是 Expo Router,在 app/_layout.js 文件中添加如下方法进行数据采集。
import { useEffect } from 'react';
import { useSegments, usePathname, Slot } from 'expo-router';
import { FTReactNativeRUM } from '@cloudcare/react-native-mobile';
export default function Layout() {
const pathname = usePathname();
const segments = useSegments();
const viewKey = segments.join('/');
useEffect(() => {
FTReactNativeRUM.startView(viewKey);
}, [viewKey, pathname]);
return <Slot />;
}
Action¶
使用方法¶
/**
* 启动 RUM Action。
*/
startAction(actionName: string, actionType: string, property?: object): Promise<void>;
/**
* 添加 Action 事件。
*/
addAction(actionName: string, actionType: string, property?: object): Promise<void>;
使用示例¶
import { FTReactNativeRUM } from '@cloudcare/react-native-mobile';
FTReactNativeRUM.startAction('actionName', 'actionType', { 'custom.foo': 'something' });
FTReactNativeRUM.addAction('actionName', 'actionType', { 'custom.foo': 'something' });
Error¶
使用方法¶
/**
* 异常捕获与日志收集。
*/
addError(stack: string, message: string, property?: object): Promise<void>;
/**
* 指定错误类型的异常捕获与日志收集。
*/
addErrorWithType(type: string, stack: string, message: string, property?: object): Promise<void>;
使用示例¶
import { FTReactNativeRUM } from '@cloudcare/react-native-mobile';
FTReactNativeRUM.addError('error stack', 'error message', { 'custom.foo': 'something' });
FTReactNativeRUM.addErrorWithType('custom_error', 'error stack', 'error message', {
'custom.foo': 'something',
});
Resource¶
使用方法¶
/**
* 开始资源请求。
*/
startResource(key: string, property?: object): Promise<void>;
/**
* 结束资源请求。
*/
stopResource(key: string, property?: object): Promise<void>;
/**
* 发送资源数据指标。
*/
addResource(key: string, resource: FTRUMResource, metrics?: FTRUMResourceMetrics): Promise<void>;
使用示例¶
import { FTReactNativeRUM } from '@cloudcare/react-native-mobile';
async function getHttp(url: string) {
const key = Utils.getUUID();
FTReactNativeRUM.startResource(key);
const fetchOptions = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
let res: Response;
try {
res = await fetch(url, fetchOptions);
} finally {
const resource: FTRUMResource = {
url: url,
httpMethod: fetchOptions.method,
requestHeader: fetchOptions.headers,
};
if (res) {
resource.responseHeader = res.headers;
resource.resourceStatus = res.status;
resource.responseBody = await res.text();
}
FTReactNativeRUM.stopResource(key);
FTReactNativeRUM.addResource(key, resource);
}
}
更多 Action 自定义规则说明,请阅读 数据采集自定义规则。