1
1
import * as Sentry from '@sentry/browser' ;
2
2
import * as Router from 'react-router' ;
3
3
import { createMemoryHistory } from 'history' ;
4
+ import set from 'lodash/set' ;
4
5
5
6
import getRouteStringFromRoutes from 'app/utils/getRouteStringFromRoutes' ;
6
7
@@ -20,49 +21,55 @@ export async function normalizeTransactionName(
20
21
appRoutes : Router . PlainRoute [ ] ,
21
22
event : Sentry . Event
22
23
) : Promise < Sentry . Event > {
23
- if ( event . type === 'transaction' ) {
24
- // For JavaScript transactions, translate the transaction name if it exists and doesn't start with /
25
- // using the app's react-router routes. If the transaction name doesn't exist, use the window.location.pathname
26
- // as the fallback.
24
+ if ( event . type !== 'transaction' ) {
25
+ return event ;
26
+ }
27
27
28
- let prevTransactionName = event . transaction ;
28
+ // For JavaScript transactions, translate the transaction name if it exists and doesn't start with /
29
+ // using the app's react-router routes. If the transaction name doesn't exist, use the window.location.pathname
30
+ // as the fallback.
29
31
30
- if ( typeof prevTransactionName === 'string' ) {
31
- if ( prevTransactionName . startsWith ( '/' ) ) {
32
- return event ;
33
- }
34
- } else {
35
- prevTransactionName = window . location . pathname ;
32
+ let prevTransactionName = event . transaction ;
33
+
34
+ if ( typeof prevTransactionName === 'string' ) {
35
+ if ( prevTransactionName . startsWith ( '/' ) ) {
36
+ return event ;
36
37
}
37
38
38
- const transactionName : string | undefined = await new Promise ( function ( resolve ) {
39
- Router . match (
40
- {
41
- routes : appRoutes ,
42
- location : createLocation ( prevTransactionName ) ,
43
- } ,
44
- ( error , _redirectLocation , renderProps ) => {
45
- if ( error ) {
46
- return resolve ( undefined ) ;
47
- }
48
-
49
- const routePath = getRouteStringFromRoutes ( renderProps . routes ?? [ ] ) ;
50
- return resolve ( routePath ) ;
39
+ set ( event , [ 'tags' , 'transaction.rename.source' ] , 'existing transaction name' ) ;
40
+ } else {
41
+ set ( event , [ 'tags' , 'transaction.rename.source' ] , 'window.location.pathname' ) ;
42
+
43
+ prevTransactionName = window . location . pathname ;
44
+ }
45
+
46
+ const transactionName : string | undefined = await new Promise ( function ( resolve ) {
47
+ Router . match (
48
+ {
49
+ routes : appRoutes ,
50
+ location : createLocation ( prevTransactionName ) ,
51
+ } ,
52
+ ( error , _redirectLocation , renderProps ) => {
53
+ if ( error ) {
54
+ set ( event , [ 'tags' , 'transaction.rename.react-router-match' ] , 'error' ) ;
55
+ return resolve ( undefined ) ;
51
56
}
52
- ) ;
53
- } ) ;
54
-
55
- if ( typeof transactionName === 'string' && transactionName . length ) {
56
- event . transaction = transactionName ;
57
-
58
- if ( event . tags ) {
59
- event . tags [ 'ui.route' ] = transactionName ;
60
- } else {
61
- event . tags = {
62
- 'ui.route' : transactionName ,
63
- } ;
57
+
58
+ set ( event , [ 'tags' , 'transaction.rename.react-router-match' ] , 'success' ) ;
59
+
60
+ const routePath = getRouteStringFromRoutes ( renderProps . routes ?? [ ] ) ;
61
+ return resolve ( routePath ) ;
64
62
}
65
- }
63
+ ) ;
64
+ } ) ;
65
+
66
+ if ( typeof transactionName === 'string' && transactionName . length ) {
67
+ event . transaction = transactionName ;
68
+
69
+ set ( event , [ 'tags' , 'transaction.rename.before' ] , prevTransactionName ) ;
70
+ set ( event , [ 'tags' , 'transaction.rename.after' ] , transactionName ) ;
71
+
72
+ set ( event , [ 'tags' , 'ui.route' ] , transactionName ) ;
66
73
}
67
74
68
75
return event ;
0 commit comments