From f47de136cb870e8b2a162ad5498260ccea565ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Wold=C5=99ich?= <31292499+krystofwoldrich@users.noreply.github.com> Date: Fri, 22 Mar 2024 15:49:28 +0100 Subject: [PATCH] fix(nativeframes): Do not enable NativeFramesTracking when native is not available (#3705) --- CHANGELOG.md | 4 ++ samples/expo/app/_layout.tsx | 1 - src/js/tracing/reactnativetracing.ts | 55 ++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eac7274b14..56e215db03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Add `getDefaultConfig` option to `getSentryExpoConfig` ([#3690](https://github.com/getsentry/sentry-react-native/pull/3690)) +### Fixes + +- Do not enable NativeFramesTracking when native is not available ([#3705](https://github.com/getsentry/sentry-react-native/pull/3705)) + ### Dependencies - Bump CLI from v2.30.0 to v2.30.2 ([#3678](https://github.com/getsentry/sentry-react-native/pull/3678)) diff --git a/samples/expo/app/_layout.tsx b/samples/expo/app/_layout.tsx index e0cbc9c7d3..6fac65744f 100644 --- a/samples/expo/app/_layout.tsx +++ b/samples/expo/app/_layout.tsx @@ -54,7 +54,6 @@ process.env.EXPO_SKIP_DURING_EXPORT !== 'true' && Sentry.init({ Sentry.metrics.metricsAggregatorIntegration(), new Sentry.ReactNativeTracing({ routingInstrumentation, - enableNativeFramesTracking: !isExpoGo(), // Only in native builds, not in Expo Go. }), ); return integrations.filter(i => i.name !== 'Dedupe'); diff --git a/src/js/tracing/reactnativetracing.ts b/src/js/tracing/reactnativetracing.ts index e3c4a64cf9..397cc1ee50 100644 --- a/src/js/tracing/reactnativetracing.ts +++ b/src/js/tracing/reactnativetracing.ts @@ -187,7 +187,10 @@ export class ReactNativeTracing implements Integration { /** * Registers routing and request instrumentation. */ - public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { + public async setupOnce( + addGlobalEventProcessor: (callback: EventProcessor) => void, + getCurrentHub: () => Hub, + ): Promise { const hub = getCurrentHub(); const client = hub.getClient(); const clientOptions = client && client.getOptions(); @@ -203,7 +206,6 @@ export class ReactNativeTracing implements Integration { tracePropagationTargets: thisOptionsTracePropagationTargets, routingInstrumentation, enableAppStartTracking, - enableNativeFramesTracking, enableStallTracking, } = this.options; @@ -240,20 +242,7 @@ export class ReactNativeTracing implements Integration { }); } - if (enableNativeFramesTracking) { - NATIVE.enableNativeFramesTracking(); - this.nativeFramesInstrumentation = new NativeFramesInstrumentation(addGlobalEventProcessor, () => { - const self = getCurrentHub().getIntegration(ReactNativeTracing); - - if (self) { - return !!self.nativeFramesInstrumentation; - } - - return false; - }); - } else { - NATIVE.disableNativeFramesTracking(); - } + this._enableNativeFramesTracking(addGlobalEventProcessor); if (enableStallTracking) { this.stallTrackingInstrumentation = new StallTrackingInstrumentation(); @@ -366,6 +355,40 @@ export class ReactNativeTracing implements Integration { return this._inflightInteractionTransaction; } + /** + * Enables or disables native frames tracking based on the `enableNativeFramesTracking` option. + */ + private _enableNativeFramesTracking(addGlobalEventProcessor: (callback: EventProcessor) => void): void { + if (this.options.enableNativeFramesTracking && !NATIVE.enableNative) { + // Do not enable native frames tracking if native is not available. + logger.warn( + '[ReactNativeTracing] NativeFramesTracking is not available on the Web, Expo Go and other platforms without native modules.', + ); + return; + } + + if (!this.options.enableNativeFramesTracking && NATIVE.enableNative) { + // Disable native frames tracking when native available and option is false. + NATIVE.disableNativeFramesTracking(); + return; + } + + if (!this.options.enableNativeFramesTracking) { + return; + } + + NATIVE.enableNativeFramesTracking(); + this.nativeFramesInstrumentation = new NativeFramesInstrumentation(addGlobalEventProcessor, () => { + const self = getCurrentHub().getIntegration(ReactNativeTracing); + + if (self) { + return !!self.nativeFramesInstrumentation; + } + + return false; + }); + } + /** * Sets the current view name into the app context. * @param event Le event.