sentry-react-native icon indicating copy to clipboard operation
sentry-react-native copied to clipboard

Add method to control native tracing

Open wbinarytree opened this issue 3 years ago • 4 comments

Currently when set a tracesSampleRate or tracesSampler. It's not sync into native during initNativeSdk. However if I set autoInitializeNativeSdk to false. The frame data from RNSentryModule will be lost because of native sdk isn't initialized via RNSentryModule. The only thing I can do for that is using patch-package to have a tracesSampleRate or tracesSampler to have both. I'm not sure this is expected feature or unexpected bug. I leave it as feature request here.

wbinarytree avatar Jul 12 '22 04:07 wbinarytree

Sorry for the delay on replying. We have disabled passing down these values from JS into Native because that would result in double-reporting transactions. And the transactions from Native are not necessarly actionable from your end if you're writing JS.

What are you trying to accomplish by setting this flag?

bruno-garcia avatar Jul 19 '22 19:07 bruno-garcia

I can understand that tracesSampleRate/tracesSampler can not be transmit to native especially for tracesSampler. However when I disable autoInitializeNativeSdk. I would assume I can still have frame tracing enabled. Which is the case for iOS but not for Android. Here is my work around by use patch-package

in RNSentryModule:

public class RNSentryModule extends ReactContextBaseJavaModule {
  //.. other methods
     
   // same code from initNativeSdk 
   @ReactMethod
    public void enablePerformance() {
                      if (androidXAvailable) {
                    frameMetricsAggregator = new FrameMetricsAggregator();
                    final Activity currentActivity = getCurrentActivity();

                    if (frameMetricsAggregator != null && currentActivity != null) {
                        try {
                            frameMetricsAggregator.add(currentActivity);
                        } catch (Throwable ignored) {
                            // throws ConcurrentModification when calling addOnFrameMetricsAvailableListener
                            // this is a best effort since we can't reproduce it
                            logger.warning("Error adding Activity to frameMetricsAggregator.");
                        }
                    }
                } else {
                    logger.warning("androidx.core' isn't available as a dependency.");
                }
    }
  //.. other methods
}

in my App.js:

      Sentry.init({});
      if (Platform.OS === "android") {
        NativeModules.RNSentry.enablePerformance();
      }

wbinarytree avatar Jul 20 '22 04:07 wbinarytree

If you want to enable performance on the Native SDKs, you can do with https://docs.sentry.io/platforms/react-native/manual-setup/native-init/ Maybe we should init the frameMetricsAggregator variable even if autoInitializeNativeSdk is disabled, but enableAutoPerformanceTracking is enabled, agreed, otherwise it's more boilerplate for the final user.

marandaneto avatar Jul 20 '22 08:07 marandaneto

@marandaneto Yeah but that would not add frame data into my JavaScript transaction. In order to have both tracesSampleRate on native and react-native. I need to disable JS auto init native SDK. Init native SDK myself. then use the alternative I provide about to enable frame rate data in react-native.

Maybe we should init the frameMetricsAggregator variable even if autoInitializeNativeSdk is disabled

That's exactly what I'm looking for. frameMetricsAggregator probably shouldn't be part of nativeInit.

wbinarytree avatar Jul 20 '22 12:07 wbinarytree

Relates to https://github.com/getsentry/sentry-cocoa/issues/2262

marandaneto avatar Oct 05 '22 10:10 marandaneto