@@ -12,11 +12,11 @@ import {
12
12
getRootSpan ,
13
13
spanToJSON ,
14
14
} from '@sentry/core' ;
15
- import type { Integration , Span , StartSpanOptions , TransactionSource } from '@sentry/types' ;
15
+ import type { Client , Integration , Span , TransactionSource } from '@sentry/types' ;
16
16
import hoistNonReactStatics from 'hoist-non-react-statics' ;
17
17
import * as React from 'react' ;
18
18
19
- import type { Action , Location , ReactRouterInstrumentation } from './types' ;
19
+ import type { Action , Location } from './types' ;
20
20
21
21
// We need to disable eslint no-explict-any because any is required for the
22
22
// react-router typings.
@@ -63,21 +63,15 @@ export function reactRouterV4BrowserTracingIntegration(
63
63
afterAllSetup ( client ) {
64
64
integration . afterAllSetup ( client ) ;
65
65
66
- const startPageloadCallback = ( startSpanOptions : StartSpanOptions ) : undefined => {
67
- startBrowserTracingPageLoadSpan ( client , startSpanOptions ) ;
68
- return undefined ;
69
- } ;
70
-
71
- const startNavigationCallback = ( startSpanOptions : StartSpanOptions ) : undefined => {
72
- startBrowserTracingNavigationSpan ( client , startSpanOptions ) ;
73
- return undefined ;
74
- } ;
75
-
76
- const instrumentation = createReactRouterInstrumentation ( history , 'reactrouter_v4' , routes , matchPath ) ;
77
-
78
- // Now instrument page load & navigation with correct settings
79
- instrumentation ( startPageloadCallback , instrumentPageLoad , false ) ;
80
- instrumentation ( startNavigationCallback , false , instrumentNavigation ) ;
66
+ instrumentReactRouter (
67
+ client ,
68
+ instrumentPageLoad ,
69
+ instrumentNavigation ,
70
+ history ,
71
+ 'reactrouter_v4' ,
72
+ routes ,
73
+ matchPath ,
74
+ ) ;
81
75
} ,
82
76
} ;
83
77
}
@@ -95,38 +89,35 @@ export function reactRouterV5BrowserTracingIntegration(
95
89
instrumentNavigation : false ,
96
90
} ) ;
97
91
98
- const { history, routes, matchPath } = options ;
92
+ const { history, routes, matchPath, instrumentPageLoad = true , instrumentNavigation = true } = options ;
99
93
100
94
return {
101
95
...integration ,
102
96
afterAllSetup ( client ) {
103
97
integration . afterAllSetup ( client ) ;
104
98
105
- const startPageloadCallback = ( startSpanOptions : StartSpanOptions ) : undefined => {
106
- startBrowserTracingPageLoadSpan ( client , startSpanOptions ) ;
107
- return undefined ;
108
- } ;
109
-
110
- const startNavigationCallback = ( startSpanOptions : StartSpanOptions ) : undefined => {
111
- startBrowserTracingNavigationSpan ( client , startSpanOptions ) ;
112
- return undefined ;
113
- } ;
114
-
115
- const instrumentation = createReactRouterInstrumentation ( history , 'reactrouter_v5' , routes , matchPath ) ;
116
-
117
- // Now instrument page load & navigation with correct settings
118
- instrumentation ( startPageloadCallback , options . instrumentPageLoad , false ) ;
119
- instrumentation ( startNavigationCallback , false , options . instrumentNavigation ) ;
99
+ instrumentReactRouter (
100
+ client ,
101
+ instrumentPageLoad ,
102
+ instrumentNavigation ,
103
+ history ,
104
+ 'reactrouter_v5' ,
105
+ routes ,
106
+ matchPath ,
107
+ ) ;
120
108
} ,
121
109
} ;
122
110
}
123
111
124
- function createReactRouterInstrumentation (
112
+ function instrumentReactRouter (
113
+ client : Client ,
114
+ instrumentPageLoad : boolean ,
115
+ instrumentNavigation : boolean ,
125
116
history : RouterHistory ,
126
117
instrumentationName : string ,
127
118
allRoutes : RouteConfig [ ] = [ ] ,
128
119
matchPath ?: MatchPath ,
129
- ) : ReactRouterInstrumentation {
120
+ ) : void {
130
121
function getInitPathName ( ) : string | undefined {
131
122
if ( history && history . location ) {
132
123
return history . location . pathname ;
@@ -161,12 +152,11 @@ function createReactRouterInstrumentation(
161
152
return [ pathname , 'url' ] ;
162
153
}
163
154
164
- return ( customStartTransaction , startTransactionOnPageLoad = true , startTransactionOnLocationChange = true ) : void => {
155
+ if ( instrumentPageLoad ) {
165
156
const initPathName = getInitPathName ( ) ;
166
-
167
- if ( startTransactionOnPageLoad && initPathName ) {
157
+ if ( initPathName ) {
168
158
const [ name , source ] = normalizeTransactionName ( initPathName ) ;
169
- customStartTransaction ( {
159
+ startBrowserTracingPageLoadSpan ( client , {
170
160
name,
171
161
attributes : {
172
162
[ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'pageload' ,
@@ -175,23 +165,23 @@ function createReactRouterInstrumentation(
175
165
} ,
176
166
} ) ;
177
167
}
168
+ }
178
169
179
- if ( startTransactionOnLocationChange && history . listen ) {
180
- history . listen ( ( location , action ) => {
181
- if ( action && ( action === 'PUSH' || action === 'POP' ) ) {
182
- const [ name , source ] = normalizeTransactionName ( location . pathname ) ;
183
- customStartTransaction ( {
184
- name,
185
- attributes : {
186
- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
187
- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : `auto.navigation.react.${ instrumentationName } ` ,
188
- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : source ,
189
- } ,
190
- } ) ;
191
- }
192
- } ) ;
193
- }
194
- } ;
170
+ if ( instrumentNavigation && history . listen ) {
171
+ history . listen ( ( location , action ) => {
172
+ if ( action && ( action === 'PUSH' || action === 'POP' ) ) {
173
+ const [ name , source ] = normalizeTransactionName ( location . pathname ) ;
174
+ startBrowserTracingNavigationSpan ( client , {
175
+ name,
176
+ attributes : {
177
+ [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
178
+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : `auto.navigation.react.${ instrumentationName } ` ,
179
+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : source ,
180
+ } ,
181
+ } ) ;
182
+ }
183
+ } ) ;
184
+ }
195
185
}
196
186
197
187
/**
0 commit comments