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

[Android] TimeToInitialDisplay doesn't track Tab screens from react-navigation

Open serzmerz opened this issue 1 year ago • 9 comments

OS:

  • [ ] Windows
  • [x] MacOS
  • [ ] Linux

Platform:

  • [ ] iOS
  • [x] Android

SDK:

  • [x] @sentry/react-native (>= 1.0.0)
  • [ ] react-native-sentry (<= 0.43.2)

SDK version: 5.22.2

react-native version: 0.73.6

Are you using Expo?

  • [ ] Yes
  • [x] No

Are you using sentry.io or on-premise?

  • [ ] sentry.io (SaaS)
  • [x] on-premise

If you are using sentry.io, please post a link to your issue so we can take a look:

[Link to issue]

Configuration:

(@sentry/react-native)

export const routingInstrumentation = new NativeSentry.ReactNavigationInstrumentation({
  enableTimeToInitialDisplay: true
});

Sentry.init({
  dsn: Config.get("REACT_APP_SENTRY_DSN"),
  environment: Config.get("ENV"),
  ignoreErrors,
  integrations: [
    new NativeSentry.ReactNativeTracing({
      routingInstrumentation
    })
  ],
  tracesSampleRate: Config.get("ENV") === "prod" && !__DEV__ ? 0.2 : 1,
  _experiments: {
    profilesSampleRate: Config.get("ENV") === "prod" && !__DEV__ ? 0 : 1
  }
});

I have the following issue:

On Android, when enableTimeToInitialDisplay flag is active, sentry doesn't capture initial time on screens from Tab navigator (react-navigation). It is happening in @sentry/react-native/android/src/main/java/io/sentry/react/RNSentryReactFragmentLifecycleTracer.java because:

  1. Tab screen has Class name com.swmansion.rnscreens.ScreenFragment, not com.swmansion.rnscreens.ScreenStackFragment and it doesnt subscribe to eventDispatcher.
  2. Even if I remove this condition, EventDispatcher doesn't report com.swmansion.rnscreens.events.ScreenAppearEvent event, since this event(transitionEnd) only available for Stack screens.

My navigation setup is next: Root stack navigator and one of the screens is TabNavigator

<RootStackNavigator.Navigator screenOptions={rootScreenOptions}>
        {routes.map(({ name, component, options }) => (
          <RootStackNavigator.Screen
            key={name}
            name={name}
            component={component}
            options={options}
          />
        ))}
      </RootStackNavigator.Navigator>
      
      
      
Tabs rendered as a screen inside root stack navigator:

<Tab.Navigator sceneContainerStyle={sceneContainerStyle} screenOptions={tabBarOptions}>
        {tabs.map(({ component, options, listeners, name }) => {
          return (
            <Tab.Screen
              key={name}
              name={name}
              component={component}
              options={options}
              listeners={listeners}
            />
          );
        })}
      </Tab.Navigator>     

Steps to reproduce:

  • Launch the app
  • Tab navigator is active screen
  • Navigate between tabs with tab bar

Actual result:

On android you will get deadline_exceeded for ui.load.initial_display span. (Note: first default screen will be reported correctly, since router fire ScreenAppearEvent for this stack screen). It works as expected on ios with both tab and stack screens.

Screenshot 2024-05-09 at 17 33 30

Expected result:

ui.load.initial_display span captured correctly for tab screens for Android

Next steps:

Have you considered any other options how to trigger navigation end for tab screens? Maybe you know any workarounds? I can help investigate or provide demo if it will be helpful. Tabs usually is the most heaviest screens, so we would like to track performance of them, especially for android(which is more slower platform for our app)

serzmerz avatar May 09 '24 15:05 serzmerz

Hi @serzmerz, thank you for the detailed information and the code snipped,

we will investigate it.

Currently you can work around this by using the manual TTID instrumentation in the tabs.

<Sentry.TimeToInitialDisplay record={true} />

https://docs.sentry.io/platforms/react-native/performance/instrumentation/time-to-display/#time-to-initial-display-overwrite

krystofwoldrich avatar May 10 '24 11:05 krystofwoldrich

Hello @krystofwoldrich, manual TTID instrumentation also doesn't work

serzmerz avatar May 10 '24 12:05 serzmerz

this happening because event will not be emitted from native side for android for tabs here https://github.com/getsentry/sentry-react-native/blob/main/src/js/tracing/reactnavigation.ts#L236 and transaction will be cancelled by timeout

serzmerz avatar May 10 '24 12:05 serzmerz

@serzmerz Thank you for the details. If I understand this correctly the native events (https://github.com/getsentry/sentry-react-native/blob/main/src/js/tracing/reactnavigation.ts?rgh-link-date=2024-05-10T12%3A31%3A17Z#L236) are also not emitting for the manual instrumentation?

krystofwoldrich avatar May 15 '24 13:05 krystofwoldrich

yes, for some reason is not emitted with manual instrumentation as well

serzmerz avatar May 15 '24 14:05 serzmerz

Could you confirm does it happen both in emulator and on device?

krystofwoldrich avatar May 15 '24 14:05 krystofwoldrich

Good catch, I checked on real device and manual instrumentation works, thanks.

DEBUG Sentry Logger [debug]: [TimeToDisplay] onDrawNextFrame: {"newFrameTimestampInSeconds":1715846467.9390001,"type":"initialDisplay" DEBUG Sentry Logger [debug]: [TimeToDisplay] Found existing ui.load.initial_display span. DEBUG Sentry Logger [debug]: [TimeToDisplay] MessagesTab initial display span updated with end timestamp.

serzmerz avatar May 16 '24 08:05 serzmerz

Also, I'm checking TimeToFullDisplay and it doesn't work on Tab on real device, only TimeToInitialDisplay works. To make it work I need to have first: <Sentry.TimeToInitialDisplay record={true} /> And : <Sentry.TimeToFullDisplay record={true} />

With that I managed to record both: initial and full display on the tab

serzmerz avatar May 16 '24 08:05 serzmerz

Thank you for the confirmation.

krystofwoldrich avatar May 17 '24 09:05 krystofwoldrich

The tab screens will be recorded using a fallback implemented in https://github.com/getsentry/sentry-react-native/pull/4042 (https://github.com/getsentry/sentry-react-native/releases/tag/5.35.0) and https://github.com/getsentry/sentry-react-native/pull/4189 for the SDK v6.

krystofwoldrich avatar Nov 18 '24 10:11 krystofwoldrich