as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

Sentry

Sentry

Sentry是一个错误跟踪和性能监控平台,可帮助您解决问题并持续了解您的应用。@amazon-devices/sentry__react-native库在Vega平台上实现了适用于Sentry的React Native SDK

在此阶段期间,某些Sentry功能尚未为Vega设置。您可以在Vega开发者论坛上提问或报告问题。

安装

  1. package.json文件中添加JavaScript库依赖项。

     dependencies: {
         "@sentry/react-native": "npm:@amazon-devices/sentry__react-native@~1.0.10"
     }
    
  2. 使用npm install命令安装依赖项。

示例

在应用的顶层初始化Sentry。

要初始化Sentry,请使用Sentry.init({...})方法。唯一必需的参数是dsn,这是Data Source Name(数据源名称)的缩写。从Sentry项目的设置页面中找到dsn


import * as Sentry from '@sentry/react-native';
import React from 'react';
import {View, Button, Text} from 'react-native';

// 在组件之外的顶层位置初始化Sentry 
Sentry.init({
  // 将环境变量SENTRY_DSN设置为项目的dsn 
  // 或者在此处内联DSN
  dsn: process.env.SENTRY_DSN,
});

export function App() {
  const captureMessage = () => {
    // 直接向Sentry发送消息
    Sentry.captureMessage('SentryAppDemo message');
  };

  const captureException = () => {
    // 捕获异常并将其发送到Sentry
    Sentry.captureException(new Error('SentryAppDemo error'));
  };

  return (
    <View
      style={{
        backgroundColor: '#181a1b',
        padding: 32,
        flex: 1,
        flexDirection: 'column',
        gap: 16,
      }}>
      <Text style={{fontSize: 32, color: 'white'}}>Sentry App Demo</Text>
      <View style={{width: '25%'}}>
        <Button onPress={captureMessage} title="捕获消息" />
      </View>
      <View style={{width: '25%'}}>
        <Button onPress={captureException} title="捕获异常" />
      </View>
    </View>
  );
}

已复制到剪贴板。

初始化Sentry后,Sentry自动发现并报告所有未捕获的异常。您无需手动发送事件。

您还可以使用Sentry.setContext()Sentry.setUser() 向Sentry事件添加额外信息,如下所示。


import * as Sentry from '@sentry/react-native';
import React from 'react';
import {View, Button, Text} from 'react-native';

// 在组件之外的顶层位置初始化Sentry 
Sentry.init({
  // 将环境变量SENTRY_DSN设置为项目的dsn 
  // 或者在此处内联DSN
  dsn: 'process.env.SENTRY_DSN',
});

export function App() {
  const setContext = () => {
    // 向Sentry事件添加自定义数据 
    Sentry.setContext('ExampleContext', {
      category: 'ExampleCategory',
      message: 'ExampleMessage',
      level: 'info',
    });
  };

  const throwError = () => {
    // 抛出未捕获错误(Sentry会为我们报告此问题)
    throw new Error('SentryAppDemo uncaught error');
  };

  return (
    <View
      style={{
        backgroundColor: '#181a1b',
        padding: 32,
        flex: 1,
        flexDirection: 'column',
        gap: 16,
      }}>
      <Text style={{fontSize: 32, color: 'white'}}>Sentry App Demo</Text>
      <View style={{width: '25%'}}>
        <Button onPress={setContext} title="设置上下文" />
      </View>
      <View style={{width: '25%'}}>
        <Button onPress={throwError} title="引发错误" />
      </View>
    </View>
  );
}

已复制到剪贴板。

API参考

  • Sentry.init(options: ReactNativeOptions) - 初始化Sentry。调用它后,所有错误都将由Sentry报告。如果您的应用在调用Sentry.init() 之前崩溃或引发未捕获的异常,错误不会被Sentry捕获。
  • Sentry.nativeCrash() - 此方法会触发原生崩溃,经过测试和验证,可在Vega设备和模拟器上运行。
  • Sentry.setUser(user: User) - 设置将包含在Sentry事件中的用户数据(例如usernameemail)。
  • Sentry.setContext(contextKey: string, contextData: Object) - 设置要在传递的contextKey下添加的自定义数据,这些数据将包含在Sentry事件中。
  • Sentry.flush() - 等待所有Sentry事件发送。
  • Sentry.captureMessage(message: string) - 捕获消息事件并将其发送给Sentry。
  • Sentry.captureException(exception: Error) - 捕获异常事件并将其发送给Sentry。

其他配置

向Sentry错误添加可读堆栈跟踪

构建应用后,您需要向Sentry告知应用的版本名称、版本和版本号,并发送JavaScript包的源映射和调试文件。这可确保Sentry捕获事件并使用可读的堆栈轨迹正确映射信息。

构建应用后,文件夹结构可能如下所示:

├── sentrydemoapp
│   ├── build
│   │   ├── lib
│   │   │   ├── rn-bundles
│   │   │   │   ├── Debug # 使用调试版本的路径
│   │   │   │   ├── Release # 使用发布版本的路径
│   │   ├── x86_64-release
│   │   │   ├── debug # 使用适用于x86计算机模拟器的应用路径
│   │   ├── armv7-release
│   │   │   ├── debug #使用适用于Fire TV Stick的应用路径
│   │   ├── aarch64-release
│   │   │   ├── debug # 使用适用于M系列计算机模拟器的应用路径
  1. 在Sentry初始化中,创建一个发布版本和一个分发版本。您需要定义$SENTRY_RELEASE_VERSION(例如my-kepler-project@2.3.12)和$SENTRY_DIST_NUMBER(例如52)。有关更多信息,请参阅Sentry官方文档(仅提供英文版)。
     Sentry.init({
       release: process.env.SENTRY_RELEASE_VERSION,
       dist: process.env.SENTRY_DIST_NUMBER,
     });
    
  2. 将Sentry Metro Serializer添加到metro.config.js并重建应用

     ...
     const {createSentryMetroSerializer} = require('@sentry/react-native/dist/js/tools/sentryMetroSerializer');
     ...
    
     const sentryMetroSerializer = createSentryMetroSerializer()
    
     const config = {
       serializer: {
         customSerializer(entryPoint, preModules, graph, options) {
           return sentryMetroSerializer(entryPoint, preModules, graph, options)
         },
       },
     };
    
     module.exports = mergeConfig(getDefaultConfig(__dirname), config);
    
  3. 安装sentry-cli。有关更多信息,请参阅Sentry官方文档(仅提供英文版)。
     curl -sL https://sentry.io/get-cli/ | sh
    
  4. 创建新的Sentry发布版本。您需要传递之前定义的$SENTRY_RELEASE_VERSION。有关更多信息,请参阅Sentry官方文档(仅提供英文版)。
     sentry-cli releases new $SENTRY_RELEASE_VERSION
    
  5. 上传用于JavaScript符号化的源映射。请注意,尚不支持sentry-wizard。有关更多信息,请参阅Sentry官方文档(仅提供英文版)。
    # 在Vega应用目录中
     # BUILD_TYPE要么是调试版,要么是发布版。
     # 使用调试ID时,--release和--dist标记是可选的
     sentry-cli sourcemaps upload "build/lib/rn-bundles/$BUILD_TYPE" --release "$SENTRY_RELEASE_VERSION" --dist "$SENTRY_DIST_NUMBER"
    

将信息发送给Sentry后,您将收到有关应用中错误发生位置的精确信息。

实现详情

已知问题

  • Sentry离线支持在Vega上不起作用。

已知限制

API 当前的行为
Sentry.addBreadcrumb() 不执行任何操作。
Sentry.init(options: ReactNativeOptions) 不支持某些选项。请参阅下方的ReactNativeOptions表。

Sentry.init() 缺少选项。

Sentry.init选项 当前的行为
options.enableNativeCrashHandling 目前无法禁用。
options.sessionTrackingIntervalMillis 不支持
options.enableNdk NDK仅能在Android系统上运行。
options.enableNdkScopeSync NDK仅能在Android系统上运行。
options.attachThreads 仅限Android的选项。
options.sendDefaultPii 仅限Android的选项。
options.enableAutoPerformanceTracing 性能监控不起作用。
options.enableWatchdogTerminationTracking 仅限iOS的选项。
options.enableAppHangTracking 仅限iOS的选项。
options.appHangTimeoutInterval 仅限iOS的选项。
options.attachScreenshot 不支持
options.attachViewHierarchy 不支持

支持的版本

程序包版本 基于 @amazon-devices/react-native-kepler版本
1.0.0 5.15.2 2.x.x

其他资源

有关其他库的信息,请参阅支持的第三方库和服务


Last updated: 2025年9月30日