@@ -17,11 +17,12 @@ import {
1717import { logger } from "@sentry/utils" ;
1818
1919import { NativeAppStartResponse } from "../definitions" ;
20- import { StallTracking } from "../integrations " ;
20+ import { ReactNativeOptions } from "../options " ;
2121import { RoutingInstrumentationInstance } from "../tracing/routingInstrumentation" ;
2222import { NATIVE } from "../wrapper" ;
2323import { NativeFramesInstrumentation } from "./nativeframes" ;
24- import { adjustTransactionDuration } from "./utils" ;
24+ import { StallTrackingInstrumentation } from "./stalltracking" ;
25+ import { adjustTransactionDuration , isNearToNow } from "./utils" ;
2526
2627export type BeforeNavigate = (
2728 context : TransactionContext
@@ -70,19 +71,6 @@ export interface ReactNativeTracingOptions
7071 * @returns A (potentially) modified context object, with `sampled = false` if the transaction should be dropped.
7172 */
7273 beforeNavigate : BeforeNavigate ;
73-
74- /**
75- * Track the app start time by adding measurements to the first route transaction. If there is no routing instrumentation
76- * an app start transaction will be started.
77- *
78- * Default: true
79- */
80- enableAppStartTracking : boolean ;
81-
82- /**
83- * Track slow/frozen frames from the native layer and adds them as measurements to all transactions.
84- */
85- enableNativeFramesTracking : boolean ;
8674}
8775
8876const defaultReactNativeTracingOptions : ReactNativeTracingOptions = {
@@ -91,8 +79,6 @@ const defaultReactNativeTracingOptions: ReactNativeTracingOptions = {
9179 maxTransactionDuration : 600 ,
9280 ignoreEmptyBackNavigationTransactions : true ,
9381 beforeNavigate : ( context ) => context ,
94- enableAppStartTracking : true ,
95- enableNativeFramesTracking : true ,
9682} ;
9783
9884/**
@@ -112,10 +98,12 @@ export class ReactNativeTracing implements Integration {
11298 public options : ReactNativeTracingOptions ;
11399
114100 public nativeFramesInstrumentation ?: NativeFramesInstrumentation ;
101+ public stallTrackingInstrumentation ?: StallTrackingInstrumentation ;
115102
116103 private _getCurrentHub ?: ( ) => Hub ;
117104 private _awaitingAppStartData ?: NativeAppStartResponse ;
118105 private _appStartFinishTimestamp ?: number ;
106+ private _sentryOptions ?: ReactNativeOptions ;
119107
120108 public constructor ( options : Partial < ReactNativeTracingOptions > = { } ) {
121109 this . options = {
@@ -140,14 +128,14 @@ export class ReactNativeTracing implements Integration {
140128 // @ts -ignore TODO
141129 shouldCreateSpanForRequest,
142130 routingInstrumentation,
143- enableNativeFramesTracking,
144131 } = this . options ;
145132
133+ this . _sentryOptions = getCurrentHub ( ) ?. getClient ( ) ?. getOptions ( ) ;
146134 this . _getCurrentHub = getCurrentHub ;
147135
148136 void this . _instrumentAppStart ( ) ;
149137
150- if ( enableNativeFramesTracking ) {
138+ if ( this . _sentryOptions ?. enableAutoPerformanceTracking ) {
151139 this . nativeFramesInstrumentation = new NativeFramesInstrumentation (
152140 addGlobalEventProcessor ,
153141 ( ) => {
@@ -160,6 +148,7 @@ export class ReactNativeTracing implements Integration {
160148 return false ;
161149 }
162150 ) ;
151+ this . stallTrackingInstrumentation = new StallTrackingInstrumentation ( ) ;
163152 } else {
164153 NATIVE . disableNativeFramesTracking ( ) ;
165154 }
@@ -187,14 +176,25 @@ export class ReactNativeTracing implements Integration {
187176 * To be called on a transaction start. Can have async methods
188177 */
189178 public onTransactionStart ( transaction : Transaction ) : void {
190- this . nativeFramesInstrumentation ?. onTransactionStart ( transaction ) ;
179+ if ( isNearToNow ( transaction . startTimestamp ) ) {
180+ // Only if this method is called at or within margin of error to the start timestamp.
181+ this . nativeFramesInstrumentation ?. onTransactionStart ( transaction ) ;
182+ this . stallTrackingInstrumentation ?. onTransactionStart ( transaction ) ;
183+ }
191184 }
192185
193186 /**
194187 * To be called on a transaction finish. Cannot have async methods.
195188 */
196- public onTransactionFinish ( transaction : Transaction ) : void {
189+ public onTransactionFinish (
190+ transaction : Transaction ,
191+ endTimestamp ?: number
192+ ) : void {
197193 this . nativeFramesInstrumentation ?. onTransactionFinish ( transaction ) ;
194+ this . stallTrackingInstrumentation ?. onTransactionFinish (
195+ transaction ,
196+ endTimestamp
197+ ) ;
198198 }
199199
200200 /**
@@ -209,7 +209,10 @@ export class ReactNativeTracing implements Integration {
209209 * Starts a route transaction if there isn't routing instrumentation.
210210 */
211211 private async _instrumentAppStart ( ) : Promise < void > {
212- if ( ! this . options . enableAppStartTracking || ! NATIVE . enableNative ) {
212+ if (
213+ ! this . _sentryOptions ?. enableAutoPerformanceTracking ||
214+ ! NATIVE . enableNative
215+ ) {
213216 return ;
214217 }
215218
@@ -316,12 +319,17 @@ export class ReactNativeTracing implements Integration {
316319 `[ReactNativeTracing] Starting ${ context . op } transaction "${ context . name } " on scope`
317320 ) ;
318321
319- idleTransaction . registerBeforeFinishCallback ( ( transaction ) => {
320- this . onTransactionFinish ( transaction ) ;
321- } ) ;
322+ idleTransaction . registerBeforeFinishCallback (
323+ ( transaction , endTimestamp ) => {
324+ this . onTransactionFinish ( transaction , endTimestamp ) ;
325+ }
326+ ) ;
322327
323328 idleTransaction . registerBeforeFinishCallback ( ( transaction ) => {
324- if ( this . options . enableAppStartTracking && this . _awaitingAppStartData ) {
329+ if (
330+ this . _sentryOptions ?. enableAutoPerformanceTracking &&
331+ this . _awaitingAppStartData
332+ ) {
325333 transaction . startTimestamp =
326334 this . _awaitingAppStartData . appStartTime / 1000 ;
327335 transaction . op = "ui.load" ;
@@ -332,24 +340,6 @@ export class ReactNativeTracing implements Integration {
332340 }
333341 } ) ;
334342
335- const stallTracking = this . _getCurrentHub ( ) . getIntegration ( StallTracking ) ;
336-
337- if ( stallTracking ) {
338- const stallTrackingFinish = stallTracking . registerTransactionStart (
339- idleTransaction
340- ) ;
341-
342- idleTransaction . registerBeforeFinishCallback (
343- ( transaction , endTimestamp ) => {
344- const stallMeasurements = stallTrackingFinish ( endTimestamp ) ;
345-
346- if ( stallMeasurements ) {
347- transaction . setMeasurements ( stallMeasurements ) ;
348- }
349- }
350- ) ;
351- }
352-
353343 idleTransaction . registerBeforeFinishCallback (
354344 ( transaction , endTimestamp ) => {
355345 adjustTransactionDuration (
0 commit comments