From 786db06d91724a9fb2a97479c1b0ea79614bc76d Mon Sep 17 00:00:00 2001 From: wonderlandpark Date: Fri, 7 May 2021 21:20:27 +0900 Subject: [PATCH] feat: migrated to @sentry/nextjs --- next.config.js | 5 +-- package.json | 1 + pages/_app.tsx | 9 ------ pages/_error.tsx | 53 ++---------------------------- sentry.client.config.js | 14 ++++++++ sentry.properties | 5 +++ sentry.server.config.js | 14 ++++++++ utils/RequestHandler.ts | 18 ----------- yarn.lock | 72 +++++++++++++++++++++++++++++++++++++++-- 9 files changed, 108 insertions(+), 83 deletions(-) create mode 100644 sentry.client.config.js create mode 100644 sentry.properties create mode 100644 sentry.server.config.js diff --git a/next.config.js b/next.config.js index 9099204..ec4d7bf 100644 --- a/next.config.js +++ b/next.config.js @@ -1,9 +1,10 @@ /* eslint-disable @typescript-eslint/no-var-requires */ // const { withSentryConfig } = require('@sentry/nextjs') +const { withSentryConfig } = require('@sentry/nextjs') const VERSION = require('./package.json').version -module.exports = { +module.exports = withSentryConfig({ env: { NEXT_PUBLIC_RELEASE_VERSION: VERSION }, @@ -13,4 +14,4 @@ module.exports = { experimental: { scrollRestoration: true } -} \ No newline at end of file +}, {}) \ No newline at end of file diff --git a/package.json b/package.json index 6e25bda..24ffa5f 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "@fortawesome/fontawesome-free": "5.15.3", "@hcaptcha/react-hcaptcha": "0.3.6", + "@sentry/nextjs": "6.3.5", "@sentry/node": "6.3.5", "@sentry/react": "6.3.5", "@sentry/tracing": "6.3.5", diff --git a/pages/_app.tsx b/pages/_app.tsx index 289afda..caae990 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -2,8 +2,6 @@ import Head from 'next/head' import App, { AppContext, AppProps } from 'next/app' import dynamic from 'next/dynamic' import { Router, useRouter } from 'next/router' -import * as Sentry from '@sentry/react' -import { Integrations } from '@sentry/tracing' import { useEffect, useState } from 'react' import { GlobalHotKeys } from 'react-hotkeys' import NProgress from 'nprogress' @@ -33,13 +31,6 @@ Router.events.on('routeChangeStart', NProgress.start) Router.events.on('routeChangeComplete', NProgress.done) Router.events.on('routeChangeError', NProgress.done) -Sentry.init({ - dsn: process.env.NEXT_PUBLIC_SENTRY_DSN, - integrations: [new Integrations.BrowserTracing()], - tracesSampleRate: 1.0, - enabled: process.env.NODE_ENV === 'production' -}) - const KoreanbotsApp = ({ Component, pageProps, err, cookie }: KoreanbotsProps): JSX.Element => { const [ shortcutModal, setShortcutModal ] = useState(false) const [ theme, setTheme ] = useState('system') diff --git a/pages/_error.tsx b/pages/_error.tsx index ef63515..2cca7d4 100644 --- a/pages/_error.tsx +++ b/pages/_error.tsx @@ -1,16 +1,12 @@ -/* https://github.com/vercel/next.js/blob/canary/examples/with-sentry/pages/_error.js */ - -import { NextPageContext } from 'next' +import { NextPage } from 'next' import Link from 'next/link' import dynamic from 'next/dynamic' -import NextErrorComponent, { ErrorProps } from 'next/error' -import * as Sentry from '@sentry/react' import { getRandom } from '@utils/Tools' import { ErrorMessage } from '@utils/Constants' const Container = dynamic(() => import('@components/Container')) -const MyError = () => { +const MyError: NextPage = () => { return
@@ -32,49 +28,4 @@ const MyError = () => {
} -MyError.getInitialProps = async ({ res, err, asPath, pathname, query, AppTree }:NextPageContext) => { - const errorInitialProps:CustomErrorProps = await NextErrorComponent.getInitialProps({ err, res, pathname, asPath, query, AppTree }) - - // Workaround for https://github.com/vercel/next.js/issues/8592, mark when - // getInitialProps has run - errorInitialProps.hasGetInitialPropsRun = true - - // Running on the server, the response object (`res`) is available. - // - // Next.js will pass an err on the server if a page's data fetching methods - // threw or returned a Promise that rejected - // - // Running on the client (browser), Next.js will provide an err if: - // - // - a page's `getInitialProps` threw or returned a Promise that rejected - // - an exception was thrown somewhere in the React lifecycle (render, - // componentDidMount, etc) that was caught by Next.js's React Error - // Boundary. Read more about what types of exceptions are caught by Error - // Boundaries: https://reactjs.org/docs/error-boundaries.html - - if (err) { - Sentry.captureException(err) - - // Flushing before returning is necessary if deploying to Vercel, see - // https://vercel.com/docs/platform/limits#streaming-responses - await Sentry.flush(2000) - - return errorInitialProps - } - - // If this point is reached, getInitialProps was called without any - // information about what the error might be. This is unexpected and may - // indicate a bug introduced in Next.js, so record it in Sentry - Sentry.captureException( - new Error(`_error.js getInitialProps missing data at path: ${asPath}`) - ) - await Sentry.flush(2000) - - return { err } -} - -interface CustomErrorProps extends ErrorProps { - hasGetInitialPropsRun?: boolean -} - export default MyError \ No newline at end of file diff --git a/sentry.client.config.js b/sentry.client.config.js new file mode 100644 index 0000000..1745373 --- /dev/null +++ b/sentry.client.config.js @@ -0,0 +1,14 @@ +// This file configures the intialization of Sentry on the browser. +// The config you add here will be used whenever a page is visited. +// https://docs.sentry.io/platforms/javascript/guides/nextjs/ + +import * as Sentry from '@sentry/nextjs' + +const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN + +Sentry.init({ + dsn: SENTRY_DSN, + // Note: if you want to override the automatic release value, do not set a + // `release` value here - use the environment variable `SENTRY_RELEASE`, so + // that it will also get attached to your source maps +}) diff --git a/sentry.properties b/sentry.properties new file mode 100644 index 0000000..7c52905 --- /dev/null +++ b/sentry.properties @@ -0,0 +1,5 @@ +defaults.url=https://sentry.io/ +defaults.org=koreanbots +defaults.project=client +auth.token=85b0389aa5db4388b8f09b1f1884ea5f964e65cdac83408ab7be303eec2b3305 +cli.executable=../../.npm/_npx/32022/lib/node_modules/@sentry/wizard/node_modules/@sentry/cli/bin/sentry-cli diff --git a/sentry.server.config.js b/sentry.server.config.js new file mode 100644 index 0000000..c49a92c --- /dev/null +++ b/sentry.server.config.js @@ -0,0 +1,14 @@ +// This file configures the initialization of Sentry on the server. +// The config you add here will be used whenever the server handles a request. +// https://docs.sentry.io/platforms/javascript/guides/nextjs/ + +import * as Sentry from '@sentry/nextjs' + +const SENTRY_DSN = process.env.SENTRY_DSN + +Sentry.init({ + dsn: SENTRY_DSN + // Note: if you want to override the automatic release value, do not set a + // `release` value here - use the environment variable `SENTRY_RELEASE`, so + // that it will also get attached to your source maps +}) diff --git a/utils/RequestHandler.ts b/utils/RequestHandler.ts index c9c30f2..102cf62 100644 --- a/utils/RequestHandler.ts +++ b/utils/RequestHandler.ts @@ -1,20 +1,9 @@ import { NextApiRequest, NextApiResponse } from 'next' -import * as Sentry from '@sentry/node' import nc from 'next-connect' import rateLimit from 'express-rate-limit' import ResponseWrapper from '@utils/ResponseWrapper' import { GlobalRatelimitIgnore } from '@utils/Constants' -import { get } from './Query' - -Sentry.init({ - dsn: process.env.SENTRY_DSN, - integrations: [ - new Sentry.Integrations.Http({ tracing: true }) - ], - tracesSampleRate: 1.0, - enabled: process.env.NODE_ENV === 'production' -}) const limiter = rateLimit({ windowMs: 60 * 1000, @@ -33,14 +22,7 @@ const RequestHandler = () => onNoMatch(_req, res) { return ResponseWrapper(res, { code: 405 }) }, - onError: async (err, req, res) => { - const user = await get.Authorization(req.cookies.token) - Sentry.captureException(err, { user: { id: user || null, ip_address: req.headers['x-forwarded-for'] as string || null } }) - return ResponseWrapper(res, { code: 500 }) - }, }) - .use(Sentry.Handlers.requestHandler()) - .use(Sentry.Handlers.tracingHandler()) .use(limiter) export default RequestHandler diff --git a/yarn.lock b/yarn.lock index 1d4a91b..aafb298 100644 --- a/yarn.lock +++ b/yarn.lock @@ -740,6 +740,18 @@ "@sentry/utils" "6.3.5" tslib "^1.9.3" +"@sentry/cli@^1.63.1": + version "1.64.1" + resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.64.1.tgz#632565d8f8b40ada51333ae38b7001ef35457a0b" + integrity sha512-G+TzOSG+58fG3f5uYvPXweK65f1sP/8MWSEuRmJE4P0JJTTXQI6WErvrqrhfR5F7UVvGzltEbpc8rvO7N3+88A== + dependencies: + https-proxy-agent "^5.0.0" + mkdirp "^0.5.5" + node-fetch "^2.6.0" + npmlog "^4.1.2" + progress "^2.0.3" + proxy-from-env "^1.1.0" + "@sentry/core@6.3.5": version "6.3.5" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.3.5.tgz#6b73de736eb9d0040be94cdbb06a744cd6b9172e" @@ -760,6 +772,16 @@ "@sentry/utils" "6.3.5" tslib "^1.9.3" +"@sentry/integrations@6.3.5": + version "6.3.5" + resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-6.3.5.tgz#3037e8f047c3b218d3d9bfacbb2089a174045baf" + integrity sha512-0+AN1JPz+JO/wSLW74mrCxdvxepwxe3u4swn8toPFuhNWiI+zpkZYr/Ic4EYDjLARn4Ouyba28uqESPDq8y0fw== + dependencies: + "@sentry/types" "6.3.5" + "@sentry/utils" "6.3.5" + localforage "^1.8.1" + tslib "^1.9.3" + "@sentry/minimal@6.3.5": version "6.3.5" resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.3.5.tgz#ef4894771243d01d81e91819400d2ecdcb34b411" @@ -769,6 +791,19 @@ "@sentry/types" "6.3.5" tslib "^1.9.3" +"@sentry/nextjs@6.3.5": + version "6.3.5" + resolved "https://registry.yarnpkg.com/@sentry/nextjs/-/nextjs-6.3.5.tgz#aeca7d6d752c9b9516a532b5eff767b6a0e9b5ba" + integrity sha512-ZJYoUIj+hkB+jmpGR8pzF/9afwYGUUPusAqf3MLtOshMOX+bwFw8fA1Osb5Wj5ZaY/ttFNmMnxPiJ2iiY3O8Zw== + dependencies: + "@sentry/core" "6.3.5" + "@sentry/integrations" "6.3.5" + "@sentry/node" "6.3.5" + "@sentry/react" "6.3.5" + "@sentry/utils" "6.3.5" + "@sentry/webpack-plugin" "1.15.0" + tslib "^1.9.3" + "@sentry/node@6.3.5": version "6.3.5" resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.3.5.tgz#d5cbf941d0a4caf7b8e644d71cc6b463eeda214e" @@ -820,6 +855,13 @@ "@sentry/types" "6.3.5" tslib "^1.9.3" +"@sentry/webpack-plugin@1.15.0": + version "1.15.0" + resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.15.0.tgz#c7f9eafbbace1929c3fb81bb720fc0d7e8b4f86d" + integrity sha512-KHVug+xI+KH/xCL7otWcRRszw0rt6i/BCH5F8+6/njz2gCBrYQOzdMvzWm4GeXZUuw5ekgycYaUhDs1/6enjuQ== + dependencies: + "@sentry/cli" "^1.63.1" + "@sinonjs/commons@^1.7.0": version "1.8.3" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d" @@ -3884,6 +3926,11 @@ ignore@^5.1.4: resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw== +immediate@~3.0.5: + version "3.0.6" + resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" + integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps= + import-fresh@^3.0.0, import-fresh@^3.2.1: version "3.3.0" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b" @@ -5016,6 +5063,13 @@ license-checker@^25.0.1: spdx-satisfies "^4.0.0" treeify "^1.1.0" +lie@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e" + integrity sha1-mkNrLMd0bKWd56QfpGmz77dr2H4= + dependencies: + immediate "~3.0.5" + lines-and-columns@^1.1.6: version "1.1.6" resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" @@ -5030,6 +5084,13 @@ loader-utils@1.2.3: emojis-list "^2.0.0" json5 "^1.0.1" +localforage@^1.8.1: + version "1.9.0" + resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1" + integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g== + dependencies: + lie "3.1.1" + locate-path@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e" @@ -5335,7 +5396,7 @@ mkdirp@1.x, mkdirp@^1.0.3: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== -mkdirp@^0.5.1: +mkdirp@^0.5.1, mkdirp@^0.5.5: version "0.5.5" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def" integrity sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ== @@ -5504,7 +5565,7 @@ node-emoji@1.10.0, node-emoji@^1.8.1: dependencies: lodash.toarray "^4.4.0" -node-fetch@2.6.1, node-fetch@^2.6.1: +node-fetch@2.6.1, node-fetch@^2.6.0, node-fetch@^2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== @@ -6526,7 +6587,7 @@ process@0.11.10, process@^0.11.10: resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI= -progress@^2.0.0: +progress@^2.0.0, progress@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== @@ -6553,6 +6614,11 @@ property-expr@^2.0.4: resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.4.tgz#37b925478e58965031bb612ec5b3260f8241e910" integrity sha512-sFPkHQjVKheDNnPvotjQmm3KD3uk1fWKUN7CrpdbwmUx3CrG3QiM8QpTSimvig5vTXmTvjz7+TDvXOI9+4rkcg== +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + psl@^1.1.28, psl@^1.1.33: version "1.8.0" resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"