Skip to content

Commit 13fafca

Browse files
authored
Merge pull request #299 from IFRCGo/feature/sentry-integration
Add Sentry and Google Analytics
2 parents 2d52bfa + 9e0d7c6 commit 13fafca

File tree

7 files changed

+159
-16
lines changed

7 files changed

+159
-16
lines changed

env.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ export default defineConfig({
66
APP_ENVIRONMENT: Schema.enum(['development', 'testing', 'staging', 'production'] as const),
77
APP_MAPBOX_ACCESS_TOKEN: Schema.string(),
88
APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }),
9-
APP_SENTRY_DSN: Schema.string.optional(),
10-
APP_SENTRY_NORMALIZE_DEPTH: Schema.number.optional(),
11-
APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(),
9+
APP_RISK_ADMIN_URL: Schema.string({ format: 'url', protocol: true }),
1210
APP_SHOW_ENV_BANNER: Schema.boolean.optional(),
1311
APP_TINY_API_KEY: Schema.string(),
1412
APP_TITLE: Schema.string(),
13+
APP_SENTRY_DSN: Schema.string.optional(),
14+
APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(),
15+
APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE: Schema.number.optional(),
16+
APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE: Schema.number.optional(),
17+
APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(),
1518
})

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"@ifrc-go/icons": "^1.2.0",
3333
"@mapbox/mapbox-gl-draw": "^1.2.0",
34+
"@sentry/react": "^7.81.1",
3435
"@tinymce/tinymce-react": "^4.3.0",
3536
"@togglecorp/fujs": "^2.0.0",
3637
"@togglecorp/re-map": "^0.2.0-beta-6",
@@ -95,6 +96,7 @@
9596
"vite": "^4.3.2",
9697
"vite-plugin-checker": "^0.6.0",
9798
"vite-plugin-compression2": "^0.9.1",
99+
"vite-plugin-radar": "^0.9.1",
98100
"vite-plugin-svgr": "^3.2.0",
99101
"vite-plugin-webfont-dl": "^3.7.4",
100102
"vite-tsconfig-paths": "^4.2.0",

src/App/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'react-router-dom';
1111
import mapboxgl from 'mapbox-gl';
1212
import { isDefined, unique } from '@togglecorp/fujs';
13+
import * as Sentry from '@sentry/react';
1314

1415
import UserContext, { UserAuth, UserContextProps } from '#contexts/user';
1516
import AlertContext, { AlertParams, AlertContextProps } from '#contexts/alert';
@@ -44,11 +45,11 @@ const requestContextValue = {
4445
transformResponse: processGoResponse,
4546
transformError: processGoError,
4647
};
47-
48-
const router = createBrowserRouter(unwrappedRoutes);
48+
const sentryCreateBrowserRouter = Sentry.wrapCreateBrowserRouter(createBrowserRouter);
49+
const router = sentryCreateBrowserRouter(unwrappedRoutes);
4950
mapboxgl.accessToken = mbtoken;
5051

51-
function App() {
52+
function Application() {
5253
// ALERTS
5354

5455
const [alerts, setAlerts] = useState<AlertParams[]>([]);
@@ -242,4 +243,5 @@ function App() {
242243
);
243244
}
244245

246+
const App = Sentry.withProfiler(Application);
245247
export default App;

src/config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ const {
66
APP_RISK_API_ENDPOINT,
77
APP_TINY_API_KEY,
88
APP_SHOW_ENV_BANNER,
9-
APP_SENTRY_DSN,
10-
APP_SENTRY_TRACES_SAMPLE_RATE,
11-
APP_SENTRY_NORMALIZE_DEPTH,
129
APP_TITLE,
1310
APP_COMMIT_HASH,
1411
APP_VERSION,
12+
APP_SENTRY_DSN,
13+
APP_SENTRY_TRACES_SAMPLE_RATE,
14+
APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE,
15+
APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE,
1516
} = import.meta.env;
1617

1718
export const environment = APP_ENVIRONMENT;
@@ -27,5 +28,6 @@ export const showEnvBanner = APP_SHOW_ENV_BANNER;
2728
export const riskApi = APP_RISK_API_ENDPOINT;
2829
export const tinyApiKey = APP_TINY_API_KEY;
2930
export const sentryAppDsn = APP_SENTRY_DSN;
30-
export const sentryTraceSampleRate = APP_SENTRY_TRACES_SAMPLE_RATE;
31-
export const sentryNormalizeDepth = APP_SENTRY_NORMALIZE_DEPTH;
31+
export const sentryTracesSampleRate = APP_SENTRY_TRACES_SAMPLE_RATE;
32+
export const sentryReplaysSessionSampleRate = APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE;
33+
export const sentryReplaysOnErrorSampleRate = APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE;

src/index.tsx

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,66 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
3-
import { isNotDefined } from '@togglecorp/fujs';
3+
import { isNotDefined, isDefined } from '@togglecorp/fujs';
4+
import * as Sentry from '@sentry/react';
5+
import {
6+
useLocation,
7+
useNavigationType,
8+
createRoutesFromChildren,
9+
matchRoutes,
10+
} from 'react-router-dom';
11+
import {
12+
appTitle,
13+
appVersion,
14+
appCommitHash,
15+
api,
16+
environment,
17+
sentryAppDsn,
18+
sentryReplaysOnErrorSampleRate,
19+
sentryReplaysSessionSampleRate,
20+
sentryTracesSampleRate,
21+
} from '#config';
422

523
import 'mapbox-gl/dist/mapbox-gl.css';
624
import './index.css';
725

826
import App from './App/index.tsx';
927

28+
if (isDefined(sentryAppDsn)) {
29+
Sentry.init({
30+
dsn: sentryAppDsn,
31+
release: `${appTitle}@${appVersion}+${appCommitHash}`,
32+
environment,
33+
integrations: [
34+
new Sentry.BrowserTracing({
35+
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
36+
React.useEffect,
37+
useLocation,
38+
useNavigationType,
39+
createRoutesFromChildren,
40+
matchRoutes,
41+
),
42+
}),
43+
new Sentry.Replay(),
44+
],
45+
46+
// Set tracesSampleRate to 1.0 to capture 100%
47+
// of transactions for performance monitoring.
48+
tracesSampleRate: Number(sentryTracesSampleRate),
49+
50+
// Set `tracePropagationTargets` to control for which URLs distributed
51+
// tracing should be enabled
52+
tracePropagationTargets: [
53+
api,
54+
// riskApi, TODO: let's add this once sentry is configured for risk server
55+
],
56+
57+
// Capture Replay for (sentryReplaysSessionSampleRate)% of all sessions,
58+
// plus for (sentryReplaysOnErrorSampleRate)% of sessions with an error
59+
replaysSessionSampleRate: Number(sentryReplaysSessionSampleRate),
60+
replaysOnErrorSampleRate: Number(sentryReplaysOnErrorSampleRate),
61+
});
62+
}
63+
1064
const webappRootId = 'webapp-root';
1165
const webappRootElement = document.getElementById(webappRootId);
1266

vite.config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from 'vite';
1+
import { defineConfig, loadEnv } from 'vite';
22
import tsconfigPaths from 'vite-tsconfig-paths';
33
import webfontDownload from 'vite-plugin-webfont-dl';
44
import reactSwc from '@vitejs/plugin-react-swc';
@@ -7,7 +7,8 @@ import { compression } from 'vite-plugin-compression2';
77
import checker from 'vite-plugin-checker';
88
import { visualizer } from 'rollup-plugin-visualizer';
99
import { ValidateEnv as validateEnv } from '@julr/vite-plugin-validate-env';
10-
import svgr from "vite-plugin-svgr";
10+
import { VitePluginRadar } from 'vite-plugin-radar'
11+
import svgr from 'vite-plugin-svgr';
1112

1213
import envConfig from './env';
1314

@@ -16,10 +17,12 @@ const commitHash = execSync('git rev-parse --short HEAD').toString();
1617

1718
export default defineConfig(({ mode }) => {
1819
const isProd = mode === 'production';
20+
const env = loadEnv(mode, process.cwd(), '')
21+
1922
return {
2023
define: {
2124
'import.meta.env.APP_COMMIT_HASH': JSON.stringify(commitHash),
22-
'import.meta.env.APP_VERSION': JSON.stringify(process.env.npm_package_version),
25+
'import.meta.env.APP_VERSION': JSON.stringify(env.npm_package_version),
2326
},
2427
plugins: [
2528
isProd ? checker({
@@ -38,6 +41,11 @@ export default defineConfig(({ mode }) => {
3841
validateEnv(envConfig),
3942
isProd ? compression() : undefined,
4043
isProd ? visualizer({ sourcemap: true }) : undefined,
44+
VitePluginRadar({
45+
analytics: {
46+
id: env.APP_GOOGLE_ANALYTICS_ID,
47+
},
48+
})
4149
],
4250
css: {
4351
devSourcemap: isProd,

yarn.lock

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,66 @@
877877
estree-walker "^2.0.2"
878878
picomatch "^2.3.1"
879879

880+
"@sentry-internal/[email protected]":
881+
version "7.81.1"
882+
resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.81.1.tgz#1180365cd8a9e18cb0f92e1ea970161840ec0e2e"
883+
integrity sha512-E5xm27xrLXL10knH2EWDQsQYh5nb4SxxZzJ3sJwDGG9XGKzBdlp20UUhKqx00wixooVX9uCj3e4Jg8SvNB1hKg==
884+
dependencies:
885+
"@sentry/core" "7.81.1"
886+
"@sentry/types" "7.81.1"
887+
"@sentry/utils" "7.81.1"
888+
889+
890+
version "7.81.1"
891+
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.81.1.tgz#5ee6ae3679ee80f444d2e8c5662430e7a734ae50"
892+
integrity sha512-DNtS7bZEnFPKVoGazKs5wHoWC0FwsOFOOMNeDvEfouUqKKbjO7+RDHbr7H6Bo83zX4qmZWRBf8V+3n3YPIiJFw==
893+
dependencies:
894+
"@sentry-internal/tracing" "7.81.1"
895+
"@sentry/core" "7.81.1"
896+
"@sentry/replay" "7.81.1"
897+
"@sentry/types" "7.81.1"
898+
"@sentry/utils" "7.81.1"
899+
900+
901+
version "7.81.1"
902+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.81.1.tgz#082fd9122bf9a488c8e05b1754724ddbc2d5cf30"
903+
integrity sha512-tU37yAmckOGCw/moWKSwekSCWWJP15O6luIq+u7wal22hE88F3Vc5Avo8SeF3upnPR+4ejaOFH+BJTr6bgrs6Q==
904+
dependencies:
905+
"@sentry/types" "7.81.1"
906+
"@sentry/utils" "7.81.1"
907+
908+
"@sentry/react@^7.81.1":
909+
version "7.81.1"
910+
resolved "https://registry.yarnpkg.com/@sentry/react/-/react-7.81.1.tgz#6a94e8e373a5bf27330cea2eb1a603ae0eb0b8ba"
911+
integrity sha512-kk0plP/mf8KgVLOiImIpp1liYysmh3Un8uXcVAToomSuHZPGanelFAdP0XhY+0HlWU9KIfxTjhMte1iSwQ8pYw==
912+
dependencies:
913+
"@sentry/browser" "7.81.1"
914+
"@sentry/types" "7.81.1"
915+
"@sentry/utils" "7.81.1"
916+
hoist-non-react-statics "^3.3.2"
917+
918+
919+
version "7.81.1"
920+
resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.81.1.tgz#a656d55e2a00b34e42be6eeb79018d21efc223af"
921+
integrity sha512-4ueT0C4bYjngN/9p0fEYH10dTMLovHyk9HxJ6zSTgePvGVexhg+cSEHXisoBDwHeRZVnbIvsVM0NA7rmEDXJJw==
922+
dependencies:
923+
"@sentry-internal/tracing" "7.81.1"
924+
"@sentry/core" "7.81.1"
925+
"@sentry/types" "7.81.1"
926+
"@sentry/utils" "7.81.1"
927+
928+
929+
version "7.81.1"
930+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.81.1.tgz#2b2551fc291e1089651fd574a68f7c4175878bd5"
931+
integrity sha512-dvJvGyctiaPMIQqa46k56Re5IODWMDxiHJ1UjBs/WYDLrmWFPGrEbyJ8w8CYLhYA+7qqrCyIZmHbWSTRIxstHw==
932+
933+
934+
version "7.81.1"
935+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.81.1.tgz#42f3e77baf90205cec1f8599eb8445a6918030bd"
936+
integrity sha512-gq+MDXIirHKxNZ+c9/lVvCXd6y2zaZANujwlFggRH2u9SRiPaIXVilLpvMm4uJqmqBMEcY81ArujExtHvkbCqg==
937+
dependencies:
938+
"@sentry/types" "7.81.1"
939+
880940
"@svgr/babel-plugin-add-jsx-attribute@^7.0.0":
881941
version "7.0.0"
882942
resolved "https://registry.yarnpkg.com/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-7.0.0.tgz#80856c1b7a3b7422d232f6e079f0beb90c4a13e9"
@@ -4268,6 +4328,13 @@ [email protected]:
42684328
resolved "https://registry.yarnpkg.com/hat/-/hat-0.0.3.tgz#bb014a9e64b3788aed8005917413d4ff3d502d8a"
42694329
integrity sha512-zpImx2GoKXy42fVDSEad2BPKuSQdLcqsCYa48K3zHSzM/ugWuYjLDr8IXxpVuL7uCLHw56eaiLxCRthhOzf5ug==
42704330

4331+
hoist-non-react-statics@^3.3.2:
4332+
version "3.3.2"
4333+
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
4334+
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
4335+
dependencies:
4336+
react-is "^16.7.0"
4337+
42714338
home-or-tmp@^2.0.0:
42724339
version "2.0.0"
42734340
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
@@ -6209,7 +6276,7 @@ react-focus-on@^3.8.1:
62096276
use-callback-ref "^1.3.0"
62106277
use-sidecar "^1.1.2"
62116278

6212-
react-is@^16.13.1:
6279+
react-is@^16.13.1, react-is@^16.7.0:
62136280
version "16.13.1"
62146281
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
62156282
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
@@ -7692,6 +7759,11 @@ vite-plugin-compression2@^0.9.1:
76927759
dependencies:
76937760
"@rollup/pluginutils" "^5.0.2"
76947761

7762+
vite-plugin-radar@^0.9.1:
7763+
version "0.9.1"
7764+
resolved "https://registry.yarnpkg.com/vite-plugin-radar/-/vite-plugin-radar-0.9.1.tgz#1ee8866cb2f2275f3eb56bfe49fea82bbdd673d2"
7765+
integrity sha512-stnb+LxeEKobcesXW2JA0OdCaBRgR/zZwN6ACXZf1gF9MNR689aiK5UlgTVmrpUnEcPucO9U0M0WHnuM6NXPsA==
7766+
76957767
vite-plugin-svgr@^3.2.0:
76967768
version "3.2.0"
76977769
resolved "https://registry.yarnpkg.com/vite-plugin-svgr/-/vite-plugin-svgr-3.2.0.tgz#920375aaf6635091c9ac8e467825f92d32544476"

0 commit comments

Comments
 (0)