Skip to content

Commit

Permalink
ref(react): Add source to react-router-v3
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Jul 6, 2022
1 parent 1cfcb0d commit d4f71a3
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Primitive, Transaction, TransactionContext } from '@sentry/types';
import { Primitive, Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

import { Location, ReactRouterInstrumentation } from './types';
Expand Down Expand Up @@ -44,16 +44,24 @@ export function reactRouterV3Instrumentation(

// Have to use global.location because history.location might not be defined.
if (startTransactionOnPageLoad && global && global.location) {
normalizeTransactionName(routes, global.location as unknown as Location, match, (localName: string) => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
op: 'pageload',
tags: {
'routing.instrumentation': 'react-router-v3',
},
});
});
normalizeTransactionName(
routes,
global.location as unknown as Location,
match,
(localName: string, source: TransactionSource = 'url') => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
op: 'pageload',
tags: {
'routing.instrumentation': 'react-router-v3',
},
});
if (activeTransaction) {
activeTransaction.setMetadata({ source });
}
},
);
}

if (startTransactionOnLocationChange && history.listen) {
Expand All @@ -68,13 +76,16 @@ export function reactRouterV3Instrumentation(
if (prevName) {
tags.from = prevName;
}
normalizeTransactionName(routes, location, match, (localName: string) => {
normalizeTransactionName(routes, location, match, (localName: string, source: TransactionSource = 'url') => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
op: 'navigation',
tags,
});
if (activeTransaction) {
activeTransaction.setMetadata({ source });
}
});
}
});
Expand All @@ -89,7 +100,7 @@ function normalizeTransactionName(
appRoutes: Route[],
location: Location,
match: Match,
callback: (pathname: string) => void,
callback: (pathname: string, source?: TransactionSource) => void,
): void {
let name = location.pathname;
match(
Expand All @@ -108,7 +119,7 @@ function normalizeTransactionName(
}

name = routePath;
return callback(name);
return callback(name, 'route');
},
);
}
Expand Down

0 comments on commit d4f71a3

Please sign in to comment.