Unverified Commit fff39a30 authored by Joel's avatar Joel Committed by GitHub

feat: use react sentry to support pass config via runtime (#350)

parent a11f36ca
...@@ -13,5 +13,3 @@ NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api ...@@ -13,5 +13,3 @@ NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api
# SENTRY # SENTRY
NEXT_PUBLIC_SENTRY_DSN= NEXT_PUBLIC_SENTRY_DSN=
NEXT_PUBLIC_SENTRY_ORG=
NEXT_PUBLIC_SENTRY_PROJECT=
\ No newline at end of file
...@@ -3,6 +3,7 @@ import type { FC } from 'react' ...@@ -3,6 +3,7 @@ import type { FC } from 'react'
import React, { useEffect, useRef, useState } from 'react' import React, { useEffect, useRef, useState } from 'react'
import { usePathname, useRouter, useSelectedLayoutSegments } from 'next/navigation' import { usePathname, useRouter, useSelectedLayoutSegments } from 'next/navigation'
import useSWR, { SWRConfig } from 'swr' import useSWR, { SWRConfig } from 'swr'
import * as Sentry from '@sentry/react'
import Header from '../components/header' import Header from '../components/header'
import { fetchAppList } from '@/service/apps' import { fetchAppList } from '@/service/apps'
import { fetchDatasets } from '@/service/datasets' import { fetchDatasets } from '@/service/datasets'
...@@ -12,11 +13,29 @@ import { AppContextProvider } from '@/context/app-context' ...@@ -12,11 +13,29 @@ import { AppContextProvider } from '@/context/app-context'
import DatasetsContext from '@/context/datasets-context' import DatasetsContext from '@/context/datasets-context'
import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common' import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
const isDevelopment = process.env.NODE_ENV === 'development'
export type ICommonLayoutProps = { export type ICommonLayoutProps = {
children: React.ReactNode children: React.ReactNode
} }
const CommonLayout: FC<ICommonLayoutProps> = ({ children }) => { const CommonLayout: FC<ICommonLayoutProps> = ({ children }) => {
useEffect(() => {
const SENTRY_DSN = document?.body?.getAttribute('data-public-sentry-dsn')
if (!isDevelopment && SENTRY_DSN) {
Sentry.init({
dsn: SENTRY_DSN,
integrations: [
new Sentry.BrowserTracing({
}),
new Sentry.Replay(),
],
tracesSampleRate: 0.1,
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
})
}
}, [])
const router = useRouter() const router = useRouter()
const pathname = usePathname() const pathname = usePathname()
const segments = useSelectedLayoutSegments() const segments = useSelectedLayoutSegments()
......
...@@ -83,7 +83,8 @@ const Header: FC<IHeaderProps> = ({ ...@@ -83,7 +83,8 @@ const Header: FC<IHeaderProps> = ({
'sticky top-0 left-0 right-0 z-20 flex bg-gray-100 grow-0 shrink-0 basis-auto h-14', 'sticky top-0 left-0 right-0 z-20 flex bg-gray-100 grow-0 shrink-0 basis-auto h-14',
s.header, s.header,
isBordered ? 'border-b border-gray-200' : '', isBordered ? 'border-b border-gray-200' : '',
)}> )}
>
<div className={classNames( <div className={classNames(
s[`header-${langeniusVersionInfo.current_env}`], s[`header-${langeniusVersionInfo.current_env}`],
'flex flex-1 items-center justify-between px-4', 'flex flex-1 items-center justify-between px-4',
......
...@@ -21,6 +21,7 @@ const LocaleLayout = ({ ...@@ -21,6 +21,7 @@ const LocaleLayout = ({
data-api-prefix={process.env.NEXT_PUBLIC_API_PREFIX} data-api-prefix={process.env.NEXT_PUBLIC_API_PREFIX}
data-pubic-api-prefix={process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX} data-pubic-api-prefix={process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX}
data-public-edition={process.env.NEXT_PUBLIC_EDITION} data-public-edition={process.env.NEXT_PUBLIC_EDITION}
data-public-sentry-dsn={process.env.NEXT_PUBLIC_SENTRY_DSN}
> >
{/* @ts-expect-error Async Server Component */} {/* @ts-expect-error Async Server Component */}
<I18nServer locale={locale}>{children}</I18nServer> <I18nServer locale={locale}>{children}</I18nServer>
......
...@@ -6,6 +6,7 @@ export NEXT_PUBLIC_DEPLOY_ENV=${DEPLOY_ENV} ...@@ -6,6 +6,7 @@ export NEXT_PUBLIC_DEPLOY_ENV=${DEPLOY_ENV}
export NEXT_PUBLIC_EDITION=${EDITION} export NEXT_PUBLIC_EDITION=${EDITION}
export NEXT_PUBLIC_API_PREFIX=${CONSOLE_URL}/console/api export NEXT_PUBLIC_API_PREFIX=${CONSOLE_URL}/console/api
export NEXT_PUBLIC_PUBLIC_API_PREFIX=${APP_URL}/api export NEXT_PUBLIC_PUBLIC_API_PREFIX=${APP_URL}/api
export NEXT_PUBLIC_SENTRY_DSN=${SENTRY_DSN}
/usr/local/bin/pm2 -v /usr/local/bin/pm2 -v
/usr/local/bin/pm2-runtime start /app/web/pm2.json /usr/local/bin/pm2-runtime start /app/web/pm2.json
const { withSentryConfig } = require('@sentry/nextjs')
const isDevelopment = process.env.NODE_ENV === 'development'
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN
const SENTRY_ORG = process.env.NEXT_PUBLIC_SENTRY_ORG
const SENTRY_PROJECT = process.env.NEXT_PUBLIC_SENTRY_PROJECT
const isHideSentry = isDevelopment || !SENTRY_DSN || !SENTRY_ORG || !SENTRY_PROJECT
const withMDX = require('@next/mdx')({ const withMDX = require('@next/mdx')({
extension: /\.mdx?$/, extension: /\.mdx?$/,
options: { options: {
...@@ -46,25 +38,6 @@ const nextConfig = { ...@@ -46,25 +38,6 @@ const nextConfig = {
}, },
] ]
}, },
...(isHideSentry
? {}
: {
sentry: {
hideSourceMaps: true,
},
}),
}
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup
const sentryWebpackPluginOptions = {
org: SENTRY_ORG,
project: SENTRY_PROJECT,
silent: true, // Suppresses all logs
sourcemaps: {
assets: './**',
ignore: ['./node_modules/**'],
},
// https://github.com/getsentry/sentry-webpack-plugin#options.
} }
module.exports = isHideSentry ? withMDX(nextConfig) : withMDX(withSentryConfig(nextConfig, sentryWebpackPluginOptions)) module.exports = withMDX(nextConfig)
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
"@mdx-js/loader": "^2.3.0", "@mdx-js/loader": "^2.3.0",
"@mdx-js/react": "^2.3.0", "@mdx-js/react": "^2.3.0",
"@next/mdx": "^13.2.4", "@next/mdx": "^13.2.4",
"@sentry/nextjs": "^7.53.1", "@sentry/react": "^7.54.0",
"@sentry/utils": "^7.54.0", "@sentry/utils": "^7.54.0",
"@tailwindcss/typography": "^0.5.9",
"@tailwindcss/line-clamp": "^0.4.2", "@tailwindcss/line-clamp": "^0.4.2",
"@tailwindcss/typography": "^0.5.9",
"@types/crypto-js": "^4.1.1", "@types/crypto-js": "^4.1.1",
"@types/lodash-es": "^4.17.7", "@types/lodash-es": "^4.17.7",
"@types/node": "18.15.0", "@types/node": "18.15.0",
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
"@types/react-syntax-highlighter": "^15.5.6", "@types/react-syntax-highlighter": "^15.5.6",
"@types/react-window": "^1.8.5", "@types/react-window": "^1.8.5",
"@types/react-window-infinite-loader": "^1.0.6", "@types/react-window-infinite-loader": "^1.0.6",
"autoprefixer": "^10.4.14",
"ahooks": "^3.7.5", "ahooks": "^3.7.5",
"autoprefixer": "^10.4.14",
"classnames": "^2.3.2", "classnames": "^2.3.2",
"copy-to-clipboard": "^3.3.3", "copy-to-clipboard": "^3.3.3",
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
......
import * as Sentry from '@sentry/nextjs'
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// Replay may only be enabled for the client-side
integrations: [new Sentry.Replay()],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 0.1,
// Capture Replay for 10% of all sessions,
// plus for 100% of sessions with an error
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
// ...
// 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
})
import * as Sentry from '@sentry/nextjs'
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 0.1,
// ...
// 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
})
import * as Sentry from '@sentry/nextjs'
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 0.1,
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment