Skip to content

Commit 3149baf

Browse files
committed
consolidate code
1 parent 32c0509 commit 3149baf

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

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

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import React from 'react';
1212
import ReactDOM from 'react-dom';
1313
import Reflux from 'reflux';
1414
import * as Router from 'react-router';
15-
import {createMemoryHistory} from 'history';
1615
import * as Sentry from '@sentry/browser';
1716
import {ExtraErrorData} from '@sentry/integrations';
1817
import {Integrations} from '@sentry/apm';
@@ -27,8 +26,7 @@ import ConfigStore from 'app/stores/configStore';
2726
import Main from 'app/main';
2827
import ajaxCsrfSetup from 'app/utils/ajaxCsrfSetup';
2928
import plugins from 'app/plugins';
30-
import routes from 'app/routes';
31-
import getRouteStringFromRoutes from 'app/utils/getRouteStringFromRoutes';
29+
import {normalizeTransactionName} from 'app/utils/apm';
3230

3331
function getSentryIntegrations(hasReplays: boolean = false) {
3432
const integrations = [
@@ -73,60 +71,12 @@ const tracesSampleRate = config ? config.apmSampling : 0;
7371
const hasReplays =
7472
window.__SENTRY__USER && window.__SENTRY__USER.isStaff && !!process.env.DISABLE_RR_WEB;
7573

76-
const appRoutes = Router.createRoutes(routes());
77-
const createLocation = createMemoryHistory().createLocation;
78-
7974
Sentry.init({
8075
...window.__SENTRY__OPTIONS,
8176
integrations: getSentryIntegrations(hasReplays),
8277
tracesSampleRate,
8378
async beforeSend(event) {
84-
if (event.type === 'transaction') {
85-
// For JavaScript transactions, translate the transaction name if it exists and doesn't start with /
86-
// using the app's react-router routes. If the transaction name doesn't exist, use the window.location.pathname
87-
// as the fallback.
88-
89-
let prevTransactionName = event.transaction;
90-
91-
if (typeof prevTransactionName === 'string') {
92-
if (prevTransactionName.startsWith('/')) {
93-
return event;
94-
}
95-
} else {
96-
prevTransactionName = window.location.pathname;
97-
}
98-
99-
const transactionName: string | undefined = await new Promise(function(resolve) {
100-
Router.match(
101-
{
102-
routes: appRoutes,
103-
location: createLocation(prevTransactionName),
104-
},
105-
(error, _redirectLocation, renderProps) => {
106-
if (error) {
107-
return resolve(undefined);
108-
}
109-
110-
const routePath = getRouteStringFromRoutes(renderProps.routes ?? []);
111-
return resolve(routePath);
112-
}
113-
);
114-
});
115-
116-
if (typeof transactionName === 'string' && transactionName.length) {
117-
event.transaction = transactionName;
118-
119-
if (event.tags) {
120-
event.tags['ui.route'] = transactionName;
121-
} else {
122-
event.tags = {
123-
'ui.route': transactionName,
124-
};
125-
}
126-
}
127-
}
128-
129-
return event;
79+
return normalizeTransactionName(event);
13080
},
13181
});
13282

src/sentry/static/sentry/app/utils/apm.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import * as Sentry from '@sentry/browser';
2+
import {createMemoryHistory} from 'history';
3+
import * as Router from 'react-router';
4+
5+
import routes from 'app/routes';
6+
import getRouteStringFromRoutes from 'app/utils/getRouteStringFromRoutes';
7+
8+
const appRoutes = Router.createRoutes(routes());
9+
const createLocation = createMemoryHistory().createLocation;
210

311
/**
412
* Sets the transaction name
@@ -9,3 +17,54 @@ export function setTransactionName(name: string) {
917
scope.setTag('ui.route', name);
1018
});
1119
}
20+
21+
export async function normalizeTransactionName(
22+
event: Sentry.Event
23+
): Promise<Sentry.Event> {
24+
if (event.type === 'transaction') {
25+
// For JavaScript transactions, translate the transaction name if it exists and doesn't start with /
26+
// using the app's react-router routes. If the transaction name doesn't exist, use the window.location.pathname
27+
// as the fallback.
28+
29+
let prevTransactionName = event.transaction;
30+
31+
if (typeof prevTransactionName === 'string') {
32+
if (prevTransactionName.startsWith('/')) {
33+
return event;
34+
}
35+
} else {
36+
prevTransactionName = window.location.pathname;
37+
}
38+
39+
const transactionName: string | undefined = await new Promise(function(resolve) {
40+
Router.match(
41+
{
42+
routes: appRoutes,
43+
location: createLocation(prevTransactionName),
44+
},
45+
(error, _redirectLocation, renderProps) => {
46+
if (error) {
47+
return resolve(undefined);
48+
}
49+
50+
const routePath = getRouteStringFromRoutes(renderProps.routes ?? []);
51+
return resolve(routePath);
52+
}
53+
);
54+
});
55+
56+
if (typeof transactionName === 'string' && transactionName.length) {
57+
event.transaction = transactionName;
58+
59+
if (event.tags) {
60+
event.tags['ui.route'] = transactionName;
61+
} else {
62+
event.tags = {
63+
'ui.route': transactionName,
64+
};
65+
}
66+
}
67+
}
68+
69+
return event;
70+
}

0 commit comments

Comments
 (0)