Skip to content

Commit b321f68

Browse files
committed
ref: Use shouldSendTransaction for both instrumentations
1 parent f5080ce commit b321f68

File tree

3 files changed

+61
-66
lines changed

3 files changed

+61
-66
lines changed

sample/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {SENTRY_INTERNAL_DSN} from './dsn';
2121

2222
const reactNavigationV5Instrumentation = new Sentry.Tracing.ReactNavigationV5Instrumentation(
2323
{
24-
shouldAttachTransaction: (route) => {
24+
shouldSendTransaction: (route, previousRoute) => {
2525
if (route.name === 'ManualTracker') {
2626
return false;
2727
}

src/js/tracing/reactnavigationv4.ts

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ import { logger } from "@sentry/utils";
33

44
import { RoutingInstrumentation } from "./router";
55

6-
interface NavigationRoute {
6+
interface NavigationRouteV4 {
77
routeName: string;
88
key: string;
99
params?: Record<any, any>;
1010
}
1111

12-
interface NavigationState {
12+
interface NavigationStateV4 {
1313
index: number;
1414
key: string;
1515
isTransitioning: boolean;
1616
routeName?: string;
17-
routes: (NavigationRoute | NavigationState)[];
17+
routes: (NavigationRouteV4 | NavigationStateV4)[];
1818
}
1919

2020
interface AppContainerInstance {
2121
_navigation: {
22-
state: NavigationState;
22+
state: NavigationStateV4;
2323
router: {
2424
getStateForAction: (
2525
action: any,
26-
state: NavigationState
27-
) => NavigationState;
26+
state: NavigationStateV4
27+
) => NavigationStateV4;
2828
};
2929
};
3030
}
@@ -34,19 +34,19 @@ interface AppContainerRef {
3434
}
3535

3636
type ReactNavigationV4ShouldAttachTransaction = (
37-
prevRoute: NavigationRoute | null,
38-
newRoute: NavigationRoute
37+
route: NavigationRouteV4,
38+
previousRoute: NavigationRouteV4 | null
3939
) => boolean;
4040

4141
interface ReactNavigationV4InstrumentationOptions {
42-
shouldAttachTransaction: ReactNavigationV4ShouldAttachTransaction;
42+
shouldSendTransaction: ReactNavigationV4ShouldAttachTransaction;
4343
}
4444

4545
const defaultShouldAttachTransaction: ReactNavigationV4ShouldAttachTransaction = () =>
4646
true;
4747

4848
const DEFAULT_OPTIONS: ReactNavigationV4InstrumentationOptions = {
49-
shouldAttachTransaction: defaultShouldAttachTransaction,
49+
shouldSendTransaction: defaultShouldAttachTransaction,
5050
};
5151

5252
/**
@@ -60,7 +60,7 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
6060

6161
private _options: ReactNavigationV4InstrumentationOptions;
6262

63-
private _prevRoute: NavigationRoute | null = null;
63+
private _prevRoute: NavigationRouteV4 | null = null;
6464

6565
constructor(options: Partial<ReactNavigationV4InstrumentationOptions>) {
6666
super();
@@ -103,17 +103,9 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
103103
*/
104104
private _handleInitialState(): void {
105105
if (this._appContainerRef.current) {
106-
const navigationState = this._appContainerRef.current._navigation.state;
106+
const state = this._appContainerRef.current._navigation.state;
107107

108-
const currentRoute = this._getCurrentRouteFromState(navigationState);
109-
110-
if (
111-
this._options.shouldAttachTransaction(this._prevRoute, currentRoute)
112-
) {
113-
this.onRouteWillChange(this._getTransactionContext(currentRoute));
114-
}
115-
116-
this._prevRoute = currentRoute;
108+
this._onStateChange(state);
117109
}
118110
}
119111

@@ -130,35 +122,41 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
130122
action,
131123
state
132124
) => {
133-
const newNavigationState = originalGetStateForAction(action, state);
134-
const currentRoute = this._getCurrentRouteFromState(newNavigationState);
135-
136-
// If the route is a different key, this is so we ignore actions that pertain to the same screen.
137-
if (!this._prevRoute || currentRoute.key !== this._prevRoute.key) {
138-
const context = this._getTransactionContext(currentRoute);
139-
140-
if (
141-
this._options.shouldAttachTransaction(this._prevRoute, currentRoute)
142-
) {
143-
this.onRouteWillChange(context);
144-
} else {
145-
logger.log(
146-
`[ReactNavigationV4Instrumentation] Will not send transaction "${context.name}" due to shouldAttachTransaction.`
147-
);
148-
}
149-
150-
this._prevRoute = currentRoute;
151-
}
152-
153-
return newNavigationState;
125+
const newState = originalGetStateForAction(action, state);
126+
127+
this._onStateChange(newState);
128+
129+
return newState;
154130
};
155131
}
156132
}
157133

158134
/**
159-
* Gets the transaction context for a `NavigationRoute`
135+
* To be called on navigation state changes and creates the transaction.
136+
*/
137+
private _onStateChange(state: NavigationStateV4): void {
138+
const currentRoute = this._getCurrentRouteFromState(state);
139+
140+
// If the route is a different key, this is so we ignore actions that pertain to the same screen.
141+
if (!this._prevRoute || currentRoute.key !== this._prevRoute.key) {
142+
const context = this._getTransactionContext(currentRoute);
143+
144+
if (this._options.shouldSendTransaction(currentRoute, this._prevRoute)) {
145+
this.onRouteWillChange(context);
146+
} else {
147+
logger.log(
148+
`[ReactNavigationV4Instrumentation] Will not send transaction "${context.name}" due to shouldSendTransaction.`
149+
);
150+
}
151+
152+
this._prevRoute = currentRoute;
153+
}
154+
}
155+
156+
/**
157+
* Gets the transaction context for a `NavigationRouteV4`
160158
*/
161-
private _getTransactionContext(route: NavigationRoute): TransactionContext {
159+
private _getTransactionContext(route: NavigationRouteV4): TransactionContext {
162160
return {
163161
name: route.routeName,
164162
op: "navigation",
@@ -174,7 +172,9 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
174172
/**
175173
* Gets the current route given a navigation state
176174
*/
177-
private _getCurrentRouteFromState(state: NavigationState): NavigationRoute {
175+
private _getCurrentRouteFromState(
176+
state: NavigationStateV4
177+
): NavigationRouteV4 {
178178
const parentRoute = state.routes[state.index];
179179

180180
if (
@@ -186,7 +186,7 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
186186
return this._getCurrentRouteFromState(parentRoute);
187187
}
188188

189-
return parentRoute as NavigationRoute;
189+
return parentRoute as NavigationRouteV4;
190190
}
191191
}
192192

src/js/tracing/reactnavigationv5.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface NavigationContainer {
1414
}
1515

1616
interface ReactNavigationInstrumentationOptions {
17-
shouldAttachTransaction(
17+
shouldSendTransaction?(
1818
route: NavigationRoute,
1919
previousRoute?: NavigationRoute
2020
): boolean;
@@ -24,17 +24,6 @@ type NavigationContainerRef = {
2424
current: NavigationContainer | null;
2525
};
2626

27-
const defaultShouldAttachTransaction = (
28-
route: NavigationRoute,
29-
previousRoute?: NavigationRoute
30-
): boolean => {
31-
return !previousRoute || previousRoute.key !== route.key;
32-
};
33-
34-
const DEFAULT_OPTIONS: ReactNavigationInstrumentationOptions = {
35-
shouldAttachTransaction: defaultShouldAttachTransaction,
36-
};
37-
3827
const STATE_CHANGE_TIMEOUT_DURATION = 200;
3928

4029
/**
@@ -61,10 +50,7 @@ export class ReactNavigationV5Instrumentation extends RoutingInstrumentation {
6150
constructor(_options: Partial<ReactNavigationInstrumentationOptions> = {}) {
6251
super();
6352

64-
this._options = {
65-
...DEFAULT_OPTIONS,
66-
..._options,
67-
};
53+
this._options = _options;
6854
}
6955

7056
/**
@@ -115,23 +101,32 @@ export class ReactNavigationV5Instrumentation extends RoutingInstrumentation {
115101
*/
116102
private _onStateChange(): void {
117103
// Use the getCurrentRoute method to be accurate.
104+
const previousRoute = this._latestRoute;
118105
const route = this._navigationContainerRef?.current?.getCurrentRoute();
119106

120107
if (route) {
121-
if (this._latestTransaction) {
108+
if (
109+
this._latestTransaction &&
110+
(!previousRoute || previousRoute.key !== route.key)
111+
) {
122112
this._latestTransaction.setName(route.name);
123113
this._latestTransaction.setTag("routing.route.key", route.key);
124114
this._latestTransaction.setData("routing.params", route.params);
125115

126-
if (this._options.shouldAttachTransaction(route, this._latestRoute)) {
116+
const willSendTransaction =
117+
typeof this._options.shouldSendTransaction === "function"
118+
? this._options.shouldSendTransaction(route, previousRoute)
119+
: true;
120+
121+
if (willSendTransaction) {
127122
// Clear the timeout so the transaction does not get cancelled.
128123
if (typeof this._stateChangeTimeout !== "undefined") {
129124
clearTimeout(this._stateChangeTimeout);
130125
this._stateChangeTimeout = undefined;
131126
}
132127
} else {
133128
logger.log(
134-
`[ReactNavigationV5Instrumentation] Will not send transaction "${this._latestTransaction.name}" due to shouldAttachTransaction.`
129+
`[ReactNavigationV5Instrumentation] Will not send transaction "${this._latestTransaction.name}" due to shouldSendTransaction.`
135130
);
136131
}
137132
}

0 commit comments

Comments
 (0)