Skip to content

Commit 67c4264

Browse files
committed
fix(apm): Set the transaction name for JavaScript transactions before they are flushed
Set the transaction name for JavaScript transactions based on latest window.location object just before the transaction is flushed. The window.location object is translated based on the app react-router routes using the react-router utility functions.
1 parent 470956d commit 67c4264

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/sentry/static/sentry/app/bootstrap.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import ConfigStore from 'app/stores/configStore';
2626
import Main from 'app/main';
2727
import ajaxCsrfSetup from 'app/utils/ajaxCsrfSetup';
2828
import plugins from 'app/plugins';
29+
import routes from 'app/routes';
30+
import getRouteStringFromRoutes from 'app/utils/getRouteStringFromRoutes';
2931

3032
function getSentryIntegrations() {
3133
const integrations = [
@@ -70,10 +72,38 @@ const config = ConfigStore.getConfig();
7072

7173
const tracesSampleRate = config ? config.apmSampling : 0;
7274

75+
const appRoutes = Router.createRoutes(routes());
76+
7377
Sentry.init({
7478
...window.__SENTRY__OPTIONS,
7579
integrations: getSentryIntegrations(),
7680
tracesSampleRate,
81+
async beforeSend(event) {
82+
if (event.type === 'transaction') {
83+
// for JavaScript transactions, set the transaction name based on the current window.location
84+
// object against the application react-router routes
85+
86+
const transactionName: string | undefined = await new Promise(function(resolve) {
87+
Router.match(
88+
{routes: appRoutes, location: window.location},
89+
(error, _redirectLocation, renderProps) => {
90+
if (error) {
91+
return resolve(undefined);
92+
}
93+
94+
const routePath = getRouteStringFromRoutes(renderProps.routes ?? []);
95+
return resolve(routePath);
96+
}
97+
);
98+
});
99+
100+
if (typeof transactionName === 'string' && transactionName.length) {
101+
event.transaction = transactionName;
102+
}
103+
}
104+
105+
return event;
106+
},
77107
});
78108

79109
if (window.__SENTRY__USER) {

0 commit comments

Comments
 (0)