getsentry / sentry-react-native

Official Sentry SDK for React Native
https://sentry.io
MIT License
1.56k stars 332 forks source link

Capture start up crashes by default (auto init native SDKs before JS) #3608

Open krystofwoldrich opened 7 months ago

krystofwoldrich commented 7 months ago

Description

Currently, start-up crashes that happen before the RN JS SDK starts are not recorded by default. This is due to the native SDKs being initialized by the RN JS SDK. Which starts when the JS Engine starts executing the JS bundle.

How to set up for start up crashes at the moment

Add SentryAndroid.init to MainApplication:

class MainApplication : Application(), ReactApplication {

    // reactNativeHost...

    override fun onCreate() {
        super.onCreate()
        SoLoader.init(this, false)
        SentryAndroid.init(this, options -> {
            options.setDsn("___DSN___");
        });
    }
}

Add SentrySDK to AppDelegate:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
    options.dsn = @"___DSN___";
  }];
  // rn init
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

With this approach our users have to manually mirror all the options from https://github.com/getsentry/sentry-react-native/blob/5329a1e953a679b75b8b0626e6947ff8216bdcac/ios/RNSentry.mm#L68 and https://github.com/getsentry/sentry-react-native/blob/5329a1e953a679b75b8b0626e6947ff8216bdcac/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java#L158 and their JS init to get the same experience as with autoNativeInitialization from JS.

How to set up for start up crashes in the future

The native SDKs are initialized by the JS layer for ease of use and the single source of configuration (the JS Sentry.init(options)).

The native SDKs require at minimum a DSN to send the start up crash event. Since we want to automatically init the native SDKs before the JS SDK, the DSN will have to be set at multiple places:

This complexity should be hidden from the users.

{
  "expo": {
    "plugins": [
      [
        "@sentry/react-native/expo",
        {
          "dsn": "___DSN___"
        }
      ]
    ]
  }
}
### Tasks
- [ ] Create RNSentry.init for Android
- [ ] Create RNSentry.init for Cocoa
- [ ] Add DSN mismatch warnings
- [ ] Add allowDsnMismatch option
- [ ] Update Expo plugin to support DSN option
- [ ] Update wizard add new native RNSentry inits
- [ ] Update native manual init docs to use RNSentry inits 
### Native RNSentry.init tasks
- [ ] Drop JS Errors https://github.com/getsentry/sentry-react-native/blob/942161665edbac984baf81bc4e1ec289ebb0e58c/ios/RNSentry.mm#L127
- [ ] Add event origin and environment https://github.com/getsentry/sentry-react-native/blob/942161665edbac984baf81bc4e1ec289ebb0e58c/ios/RNSentry.mm#L177
realkosty commented 4 months ago

@krystofwoldrich customer is asking:

do you have any timeline/estimate around when this issue could be fixed?

krystofwoldrich commented 4 months ago

@realkosty We don't have a timeline yet. But it's possible to capture the startup crashes when initializing the native SDKs manually. The How to set up for start up crashes at the moment part of the issue.