Startup hooks for tracing/monitoring.
Skip to solutionKEEP THE
hardSystem Design
How do you handle instrumentation and observability in Next.js (instrumentation.ts)?
1.0k views
01
Understand the problem
nextjsinstrumentationobservabilityopentelemetry
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
instrumentation.ts exports a register() function that runs once when the server starts — the place to initialize OpenTelemetry, error monitoring (Sentry), or other tracing. Next.js also supports onRequestError and OpenTelemetry integration for distributed tracing across server rendering and handlers.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Initialize tracing at startup
// instrumentation.ts (project root)
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { NodeSDK } = await import('@opentelemetry/sdk-node');
new NodeSDK({ /* exporter, instrumentations */ }).start();
}
}
// Optional: capture server errors with context
export async function onRequestError(err, request, context) {
reportToSentry(err, { request, context });
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 77 of 95 decoded in the Next.js track. One more won't hurt.