-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
342 lines (312 loc) · 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import React, { Component, useEffect } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import 'react-loader-spinner/dist/loader/css/react-spinner-loader.css';
import * as Sentry from '@sentry/react';
import { createBrowserHistory } from 'history';
import {
Routes,
Route,
BrowserRouter,
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
} from 'react-router-dom';
import { crasher } from './utils/errors';
import {
determineBackendType,
determineBackendUrl,
} from './utils/backendrouter';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import logger from 'redux-logger';
import rootReducer from './reducers';
import ScrollToTop from './components/ScrollToTop';
import Footer from './components/Footer';
import Nav from './components/Nav';
import About from './components/About';
import Cart from './components/Cart';
import Checkout from './components/Checkout';
import Complete from './components/Complete';
import CompleteError from './components/CompleteError';
import Employee from './components/Employee';
import Home from './components/Home';
import NotFound from './components/NotFound';
import Product from './components/Product';
import Products from './components/Products';
import ProductsJoin from './components/ProductsJoin';
import Nplusone from './components/nplusone';
const tracingOrigins = [
'localhost',
'empowerplant.io',
'run.app',
'appspot.com',
/^\//,
];
const history = createBrowserHistory();
let ENVIRONMENT;
if (window.location.hostname === 'localhost') {
ENVIRONMENT = 'test';
} else {
// App Engine
ENVIRONMENT = 'production';
}
let BACKEND_URL;
let FRONTEND_SLOWDOWN;
const DSN = process.env.REACT_APP_DSN;
const RELEASE = process.env.REACT_APP_RELEASE;
console.log('ENVIRONMENT', ENVIRONMENT);
console.log('RELEASE', RELEASE);
Sentry.init({
dsn: DSN,
release: RELEASE,
environment: ENVIRONMENT,
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
replaysSessionSampleRate: 1.0,
debug: true,
integrations: [
new Sentry.metrics.MetricsAggregator(),
new Sentry.BrowserProfilingIntegration(),
new Sentry.BrowserTracing({
tracingOrigins: tracingOrigins,
tracePropagationTargets: tracingOrigins,
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
useEffect,
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes
),
beforeNavigate: (context) => {
const { name, op } = context;
const { source } = context.metadata;
if (source === 'url' && (name === '/' || name === '/checkout')) {
context.metadata.source = 'route';
}
return {
...context,
// How to parameterize a transaction if not using a Routing library
// name: window.location.pathname.replace(/\/employee.*/,'/employee/:id')
};
},
_experiments: {
// This enables tracing on user interactions like clicks
// --> 2/13/24 disabling experimental interactions feature
// because it may be preventing navigation transactions
// from being captured
enableInteractions: false,
// This enables profiling of route transactions in react
onStartRouteTransaction: Sentry.onProfilingStartRouteTransaction,
},
}),
new Sentry.Replay({
// Additional configuration goes in here
// replaysSessionSampleRate and replaysOnErrorSampleRate is now a top-level SDK option
blockAllMedia: false,
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#network-details
networkDetailAllowUrls: ['/checkout', '/products'],
}),
],
beforeSend(event, hint) {
// Parse from tags because src/index.js already set it there. Once there are React route changes, it is no longer in the URL bar
let se;
Sentry.withScope(function (scope) {
se = scope._tags.se;
});
if (se) {
if (se.startsWith('prod-tda-')) {
// Release Health
event.fingerprint = ['{{ default }}', se, RELEASE];
} else {
// SE Testing
event.fingerprint = ['{{ default }}', se];
}
}
if (event.exception) {
sessionStorage.setItem('lastErrorEventId', event.event_id);
}
return event;
},
});
// TODO is this best placement?
const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes);
const sentryReduxEnhancer = Sentry.createReduxEnhancer({});
const store = createStore(
rootReducer,
compose(applyMiddleware(logger), sentryReduxEnhancer)
);
class App extends Component {
constructor() {
super();
this.state = {
cart: {
items: [],
quantities: {},
total: 0,
},
products: {
response: [],
},
};
let queryParams = new URLSearchParams(history.location.search);
// Set desired backend
let backendTypeParam = queryParams.get('backend');
const backendType = determineBackendType(backendTypeParam);
BACKEND_URL = determineBackendUrl(backendType, ENVIRONMENT);
console.log(`> backendType: ${backendType} | backendUrl: ${BACKEND_URL}`);
// These also get passed via request headers (see window.fetch below)
Sentry.configureScope((scope) => {
const customerType = [
'medium-plan',
'large-plan',
'small-plan',
'enterprise',
][Math.floor(Math.random() * 4)];
scope.setTag('customerType', customerType);
if (queryParams.get('se')) {
// Route components (navigation changes) will now have 'se' tag on scope
console.log('> src/index.js se', queryParams.get('se'));
scope.setTag('se', queryParams.get('se'));
// for use in Checkout.js when deciding whether to pre-fill form
// lasts for as long as the tab is open
sessionStorage.setItem('se', queryParams.get('se'));
}
if (queryParams.get('frontendSlowdown') === 'true') {
console.log('> frontend-only slowdown: true');
FRONTEND_SLOWDOWN = true;
scope.setTag('frontendSlowdown', true);
} else {
console.log('> frontend + backend slowdown');
scope.setTag('frontendSlowdown', false);
}
if (queryParams.get('userFeedback')) {
sessionStorage.setItem('userFeedback', queryParams.get('userFeedback'));
} else {
sessionStorage.setItem('userFeedback', 'false');
}
sessionStorage.removeItem('lastErrorEventId');
scope.setTag('backendType', backendType);
let email = null;
if (queryParams.get('userEmail')) {
email = queryParams.get('userEmail');
} else {
// making fewer emails so event and user counts for an Issue are not the same
let array = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
];
let a = array[Math.floor(Math.random() * array.length)];
let b = array[Math.floor(Math.random() * array.length)];
let c = array[Math.floor(Math.random() * array.length)];
email = a + b + c + '@gmail.com';
}
scope.setUser({ email: email });
});
// Automatically append `se`, `customerType` and `userEmail` query params to all requests
// (except for requests to Sentry)
const nativeFetch = window.fetch;
window.fetch = function (...args) {
let url = args[0];
// When TDA is run in 'mock' mode inside Docker mini-relay will be ingesting on port 9989, see:
// https://github.com/sentry-demos/empower/blob/79bed0b78fb3d40dff30411ef26c31dc7d4838dc/mini-relay/Dockerfile#L9
let ignore_match = url.match(
/^http[s]:\/\/([^.]+\.ingest\.sentry\.io\/|localhost:9989|127.0.0.1:9989).*/
);
if (!ignore_match) {
Sentry.withScope(function (scope) {
let se, customerType, email;
[se, customerType] = [scope._tags.se, scope._tags.customerType];
email = scope._user.email;
args[1].headers = { ...args[1].headers, se, customerType, email };
});
}
return nativeFetch.apply(window, args);
};
// Crasher parses query params sent by /tests for triggering crashes for Release Health
crasher();
}
render() {
return (
<Provider store={store}>
<BrowserRouter history={history}>
<ScrollToTop />
<Nav frontendSlowdown={FRONTEND_SLOWDOWN} />
<div id="body-container">
<SentryRoutes>
<Route
path="/"
element={
<Home
backend={BACKEND_URL}
frontendSlowdown={FRONTEND_SLOWDOWN}
/>
}
></Route>
<Route
path="/about"
element={<About backend={BACKEND_URL} history={history} />}
></Route>
<Route path="/cart" element={<Cart />} />
<Route
path="/checkout"
element={<Checkout backend={BACKEND_URL} history={history} />}
></Route>
<Route path="/complete" element={<Complete />} />
<Route path="/error" element={<CompleteError />} />
<Route path="/employee/:id" element={<Employee />}></Route>
<Route path="/product/:id" element={<Product />}></Route>
<Route
path="/products"
element={<Products backend={BACKEND_URL} />}
></Route>
<Route
path="/products-fes" // fes = frontend slowdown (only frontend)
element={
<Products backend={BACKEND_URL} frontendSlowdown={true} />
}
></Route>
<Route
path="/nplusone"
element={<Nplusone backend={BACKEND_URL} />}
/>
<Route
path="/products-join"
element={<ProductsJoin backend={BACKEND_URL} />}
></Route>
<Route path="*" element={<NotFound />} />
</SentryRoutes>
</div>
<Footer />
</BrowserRouter>
</Provider>
);
}
}
// React-router in use here https://reactrouter.com/web/guides/quick-start
ReactDOM.render(<App />, document.getElementById('root'));