1
头图

foreword

Introduction to sentry

Sentry is an exception monitoring platform out of the country that supports most languages and frameworks on the market.

For react-native:

  • Can automatically capture including native errors and js errors.
  • The reported information includes model, system version number, app version number, console.log information, error stack information, abnormal source code location, commit record, etc.
  • Features include error message kanban, monitoring and warning, performance monitoring, etc.

advantage:

  • free
  • Rich reporting information

shortcoming:

  • need scientific internet
  • No Chinese version yet

Access steps

Create project

Log in

Register and log in to your sentry account.

image-20220827164007747.png

Create an organization

Click and enter the organization name and confirm. I created an organization called test.

image-20220827164657285.png

Create project

Select react-native, you can modify the project name and organization, I will not change it here, click Create Project.

image-20220827164957373.png

Configure sentry

Install

 npm install --save @sentry/react-native
# or
yarn add @sentry/react-native

Modify native configuration

 npx @sentry/wizard -i reactNative -p ios android
# or
yarn sentry-wizard -i reactNative -p ios android

# 上面命令完成后记得更新ios依赖
npx pod-install

At this time, the command line will open the browser (if not open, manually click the link that appears on the command line), and then you select a project, here select test / react-native project. Represents the react-native project under the test organization.

image-20220827171710966.png

After the selection is completed, four files will be automatically configured (sentry version is 4.2.4 at the time of writing this article, and new versions may be different in the future).

Modify the content
iOS and android

There is one more sentry.properties file.

image-20220827172548429.png

sentry.properties the content inside is the same:

 defaults.url=https://sentry.io/
defaults.org=test-5yn
defaults.project=react-native
auth.token=4824f7eb99fc06e55d01ad794dd9743fdxxxxxxxxxxx
  • url: official website address
  • org: organization name
  • project: project name
  • token: authentication token
Note, do not try to add comments to the sentry.properties file, otherwise an error will be reported when uploading the sourcemap, and the url parameter will be used when uploading.
android

android also modified the android/app/build.gradle file.

image-20220827173022807.png

iOS

ios modified the ios/rn_demo.xcodeproj/project.pbxproj file.

A build phase task is added to it. The content of the task is probably to upload some content to the sentry platform when packaging, so as to locate the abnormality. It is estimated that it is to locate native exceptions.

image-20220827173305551.png

image-20220827173336695.png

Then I changed the original packaging task of react-native:

image-20220827174355932.png

Format the content of the script here:

  • original script

     set -e
    
    WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
    REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"
    
    /bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"
  • modified script

     export SENTRY_PROPERTIES=sentry.properties
    export EXTRA_PACKAGER_ARGS="--sourcemap-output $DERIVED_FILE_DIR/main.jsbundle.map"
    set -e
    
    WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
    ../node_modules/@sentry/cli/bin/sentry-cli react-native xcode REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"
    
    /bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"

Probably the change is to add a sentry cli command, and then upload the sourcemap to locate the js abnormal location.

Initialize SDK

 import * as Sentry from '@sentry/react-native';

Sentry.init({
  dsn: 'https://04c9544cd0104f97ad0a9c2599f9cd49@o1379748.ingest.sentry.io/6692866', // 这里替换成你项目的dsn
  tracesSampleRate: 1.0, // 上传异常的比例,1.0代表百分之百上传
});

wrap your app

Modified App.tsx :

 export default Sentry.wrap(App);

verify

js error

 throw new Error("My first Sentry error!");

View in the background:

image-20220829122118266.png

native error

 Sentry.nativeCrash();

View in the background:

image-20220829122358237.png

Stepping on the pit record

error: No such file or directory (os error 2)

The new version rn will have the following problems, the version > 0.69.

image-20220829083729565

solution

You need to manually modify the packaging script and copy the following code to the place shown in the picture:

 set -e
export SENTRY_PROPERTIES=sentry.properties
export EXTRA_PACKAGER_ARGS="--sourcemap-output $DERIVED_FILE_DIR/main.jsbundle.map"
WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"
SENTRY_CLI_PATH="../node_modules/@sentry/cli/bin/sentry-cli"
/bin/sh -c "$WITH_ENVIRONMENT \"$SENTRY_CLI_PATH react-native xcode $REACT_NATIVE_XCODE\""

image-20220829110921080.png

env: node: No such file or directory

If you use nvm to manage node, the following problems will occur at runtime:

image-20220829111930253

solution

Create a soft connection to a node:

 ln -s $(which node) /usr/local/bin/node

postscript

For the problem of being walled, you can consider privatized deployment. For details, you can check: https://github.com/getsentry/self-hosted .

Reference: sentry official documentation

wcly
153 声望2 粉丝

用已知解决未知