1
1
import * as Sentry from '@sentry/browser' ;
2
+ import * as Router from 'react-router' ;
3
+ import { createMemoryHistory } from 'history' ;
4
+ import set from 'lodash/set' ;
5
+
6
+ import getRouteStringFromRoutes from 'app/utils/getRouteStringFromRoutes' ;
7
+
8
+ const createLocation = createMemoryHistory ( ) . createLocation ;
2
9
3
10
/**
4
11
* Sets the transaction name
@@ -9,3 +16,61 @@ export function setTransactionName(name: string) {
9
16
scope . setTag ( 'ui.route' , name ) ;
10
17
} ) ;
11
18
}
19
+
20
+ export async function normalizeTransactionName (
21
+ appRoutes : Router . PlainRoute [ ] ,
22
+ event : Sentry . Event
23
+ ) : Promise < Sentry . Event > {
24
+ if ( event . type !== 'transaction' ) {
25
+ return event ;
26
+ }
27
+
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.
31
+
32
+ let prevTransactionName = event . transaction ;
33
+
34
+ if ( typeof prevTransactionName === 'string' ) {
35
+ if ( prevTransactionName . startsWith ( '/' ) ) {
36
+ return event ;
37
+ }
38
+
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 ) ;
56
+ }
57
+
58
+ set ( event , [ 'tags' , 'transaction.rename.react-router-match' ] , 'success' ) ;
59
+
60
+ const routePath = getRouteStringFromRoutes ( renderProps . routes ?? [ ] ) ;
61
+ return resolve ( routePath ) ;
62
+ }
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 ) ;
73
+ }
74
+
75
+ return event ;
76
+ }
0 commit comments