Observability
How the kit reports errors, traces, session replays, and logs to Sentry, how to turn it on, and what to change for production.
The kit uses Sentry for error tracking, performance traces, session replay, and logs. It's wired into every Next.js runtime and stays silent until you set a DSN, so you can ignore it until you need it.
How it's wired
Next.js runs code in three places, and each gets its own Sentry setup, the layout Sentry's Next.js guide uses:
| File | Runs in | What it does |
|---|---|---|
src/instrumentation-client.ts | The browser | Errors, traces, session replay, and console logs from the client |
sentry.server.config.ts | Node.js (server) | Errors, traces, and console logs from the server |
sentry.edge.config.ts | Edge runtime | The same for edge code |
src/instrumentation.ts | Server start | Loads the server or edge config in register() and exports onRequestError |
src/app/global-error.tsx | The browser | Reports errors that reach the root error boundary with Sentry.captureException |
next.config.ts | Build | withSentryConfig uploads source maps so stack traces show your original code |
onRequestError is set to Sentry.captureRequestError, which captures errors
from Server Components, middleware, and proxies, so server errors are reported
without wrapping each route.
What's captured
- Errors from the browser, the server, and edge code, with stack traces.
- Performance traces, sampled at
NEXT_PUBLIC_SENTRY_TRACES_SAMPLE_RATE(a share from0to1; unset means1in development and0.1in production). - Session replay, in the browser only: 10% of normal sessions and 100% of
sessions that hit an error (
replaysSessionSampleRateandreplaysOnErrorSampleRateinsrc/instrumentation-client.ts). - Logs:
console.*calls at or aboveNEXT_PUBLIC_SENTRY_LOG_LEVEL(defaultwarn, soconsole.warnandconsole.error) are sent to Sentry throughconsoleLoggingIntegration.
Turn it on
- Create a Next.js project in Sentry and copy its DSN from Settings → Projects → your project → Client Keys.
- Set the variables in
.env(all are listed in Configuration):
NEXT_PUBLIC_SENTRY_DSN=https://...ingest.sentry.io/...
NEXT_PUBLIC_SENTRY_ENVIRONMENT=development
NEXT_PUBLIC_SENTRY_LOG_LEVEL=warn- For readable stack traces in production, also set
SENTRY_ORG,SENTRY_PROJECT, andSENTRY_AUTH_TOKENwhere the app is built.withSentryConfiguses them to upload source maps duringnext build. Keep the auth token secret: it belongs in your CI or hosting settings, not in the repository. - Restart the dev server, then trigger an error, for example by temporarily throwing in a page, and check that it appears in Sentry.
Without NEXT_PUBLIC_SENTRY_DSN, Sentry.init() still runs but sends
nothing. That's the default, so a fresh clone reports nothing until you set
it.
Before you go to production
- Check the trace sample rate. When
NEXT_PUBLIC_SENTRY_TRACES_SAMPLE_RATEis unset or empty, the kit traces every request in development and 10% (0.1) in production, the starting point Sentry's guide uses. Tracing every request in production can use up your Sentry quota, so only raise it on purpose, and adjust it to your traffic. - Set the environment. Use
NEXT_PUBLIC_SENTRY_ENVIRONMENT=production(orstaging) so issues from each deployment are separated. - Set the variables at build time. The
NEXT_PUBLIC_SENTRY_*values are built into the browser code, so they must be set whennext buildruns. In Docker they're build arguments; see Docker. - Review replay privacy. Session replay records what users see. Check
Sentry's replay privacy settings against what your app displays before
enabling it in production, or set both replay rates to
0.
Remove Sentry
If you use another tool, delete sentry.server.config.ts,
sentry.edge.config.ts, src/instrumentation-client.ts, and
src/instrumentation.ts, remove the Sentry.captureException call from
src/app/global-error.tsx, unwrap withSentryConfig in next.config.ts,
drop the Sentry variables from src/lib/env.ts and src/lib/env.server.ts,
and uninstall @sentry/nextjs.