Expo Router
了解如何使用 Sentry 的 Expo Router instrumentation。
Sentry 的 React Native SDK 包含了对 Expo Router 的 instrumentation。这使您可以查看导航转换的性能以及在导航过程中发生的错误。本页面将指导您完成 instrumentation 的设置并根据需要进行配置。
以下代码片段展示了如何初始化 instrumentation。
app/_layout.tsx
Copied
import React from "react";
import { Slot, useNavigationContainerRef } from "expo-router";
import Constants, { ExecutionEnvironment } from "expo-constants";
import * as Sentry from "@sentry/react-native";
const navigationIntegration = Sentry.reactNavigationIntegration({
enableTimeToInitialDisplay:
Constants.executionEnvironment === ExecutionEnvironment.StoreClient, // Only in native builds, not in Expo Go.
});
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
// We recommend adjusting this value in production.
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
integrations: [navigationIntegration],
enableNativeFramesTracking:
Constants.executionEnvironment === ExecutionEnvironment.StoreClient, // Only in native builds, not in Expo Go.
});
function RootLayout() {
const ref = useNavigationContainerRef();
React.useEffect(() => {
if (ref) {
navigationIntegration.registerNavigationContainer(ref);
}
}, [ref]);
return <Slot />;
}
export default Sentry.wrap(RootLayout);
您可以通过向构造函数传递一个选项对象来配置 instrumentation:
Copied
Sentry.reactNavigationIntegration({
enableTimeToInitialDisplay: true, // default: false
routeChangeTimeoutMs: 1000, // default: 1000
ignoreEmptyBackNavigationTransactions: true, // default: true
});
此选项指定在路由更改发起后,instrumentation 等待路由挂载的时间。如果在此时间内路由未挂载,事务将被丢弃。默认值是 1_000
毫秒。
此选项启用自动测量每个路由的首次显示时间。了解更多请参阅 首次显示时间。默认值是 false
。
此选项确保来自已见过且没有任何跨度的路由的事务不会被采样。这减少了大量杂乱数据,使得大多数返回导航事务现在被忽略。默认值是 true
。
- 慢帧和冻结帧、首次显示时间和完全显示时间仅在原生构建中可用,不在 Expo Go 中提供。