Skip to content

Commit 1e2b329

Browse files
feat(monitoring): add Sentry error tracking integration
- Configure Sentry SDK for Next.js with edge and server support - Add global error boundary for client-side error handling - Enable source maps and session replay features
1 parent d116515 commit 1e2b329

File tree

9 files changed

+1845
-72
lines changed

9 files changed

+1845
-72
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ tests/e2e/.auth/*.json
7373
# Git worktrees (Crystal)
7474
/worktrees/
7575
/worktree-*/
76+
77+
# Sentry Config File
78+
.env.sentry-build-plugin

app/global-error.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use client'
2+
3+
import * as Sentry from '@sentry/nextjs'
4+
import NextError from 'next/error'
5+
import { memo, useEffect } from 'react'
6+
7+
/**
8+
* Global Error Boundary
9+
*
10+
* Catches unhandled errors at the root level and reports to Sentry.
11+
* This component wraps the entire application and provides fallback UI.
12+
*/
13+
const GlobalError = memo(function GlobalError({
14+
error,
15+
}: {
16+
error: Error & { digest?: string }
17+
}) {
18+
useEffect(() => {
19+
Sentry.captureException(error)
20+
}, [error])
21+
22+
return (
23+
<html lang="en">
24+
<body>
25+
{/* `NextError` is the default Next.js error page component. Its type
26+
definition requires a `statusCode` prop. However, since the App Router
27+
does not expose status codes for errors, we simply pass 0 to render a
28+
generic error message. */}
29+
<NextError statusCode={0} />
30+
</body>
31+
</html>
32+
)
33+
})
34+
35+
export default GlobalError

instrumentation-client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This file configures the initialization of Sentry on the client.
2+
// The added config here will be used whenever a users loads a page in their browser.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
Sentry.init({
8+
dsn: 'https://06b1775946774ab1527986b339ea85ed@o1245861.ingest.us.sentry.io/4510597804261376',
9+
10+
// Add optional integrations for additional features
11+
integrations: [Sentry.replayIntegration()],
12+
13+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
14+
tracesSampleRate: 1,
15+
// Enable logs to be sent to Sentry
16+
enableLogs: true,
17+
18+
// Define how likely Replay events are sampled.
19+
// This sets the sample rate to be 10%. You may want this to be 100% while
20+
// in development and sample at a lower rate in production
21+
replaysSessionSampleRate: 0.1,
22+
23+
// Define how likely Replay events are sampled when an error occurs.
24+
replaysOnErrorSampleRate: 1.0,
25+
26+
// Enable sending user PII (Personally Identifiable Information)
27+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/options/#sendDefaultPii
28+
sendDefaultPii: true,
29+
})
30+
31+
export const onRouterTransitionStart = Sentry.captureRouterTransitionStart

instrumentation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from '@sentry/nextjs'
2+
3+
export async function register() {
4+
if (process.env.NEXT_RUNTIME === 'nodejs') {
5+
await import('./sentry.server.config')
6+
}
7+
8+
if (process.env.NEXT_RUNTIME === 'edge') {
9+
await import('./sentry.edge.config')
10+
}
11+
}
12+
13+
export const onRequestError = Sentry.captureRequestError

next.config.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withSentryConfig } from '@sentry/nextjs'
12
import { codeInspectorPlugin } from 'code-inspector-plugin'
23
import type { NextConfig } from 'next'
34

@@ -63,4 +64,40 @@ const nextConfig: NextConfig = {
6364
},
6465
}
6566

66-
export default nextConfig
67+
export default withSentryConfig(nextConfig, {
68+
// For all available options, see:
69+
// https://www.npmjs.com/package/@sentry/webpack-plugin#options
70+
71+
org: 'laststance',
72+
73+
project: 'gitbix',
74+
75+
// Only print logs for uploading source maps in CI
76+
silent: !process.env.CI,
77+
78+
// For all available options, see:
79+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
80+
81+
// Upload a larger set of source maps for prettier stack traces (increases build time)
82+
widenClientFileUpload: true,
83+
84+
// Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
85+
// This can increase your server load as well as your hosting bill.
86+
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
87+
// side errors will fail.
88+
tunnelRoute: '/monitoring',
89+
90+
webpack: {
91+
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
92+
// See the following for more information:
93+
// https://docs.sentry.io/product/crons/
94+
// https://vercel.com/docs/cron-jobs
95+
automaticVercelMonitors: true,
96+
97+
// Tree-shaking options for reducing bundle size
98+
treeshake: {
99+
// Automatically tree-shake Sentry logger statements to reduce bundle size
100+
removeDebugLogging: true,
101+
},
102+
},
103+
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@radix-ui/react-slot": "^1.2.4",
5151
"@radix-ui/react-visually-hidden": "^1.2.4",
5252
"@reduxjs/toolkit": "^2.11.2",
53+
"@sentry/nextjs": "^10.32.1",
5354
"@supabase/ssr": "^0.8.0",
5455
"@supabase/supabase-js": "^2.89.0",
5556
"@t3-oss/env-nextjs": "^0.13.10",

0 commit comments

Comments
 (0)