Question presented to candidate: "How would you add tracing and error reporting to a Next.js app, and which hooks does Next.js give you for that?"
What a strong answer should cover:
instrumentation.tsat the project root exportsregister(), called once when a server instance starts, before it handles requests; use it to set up OpenTelemetry or an error SDK.- It also exports
onRequestError(error, request, context), called for errors during rendering, Route Handlers, Server Actions and Proxy, with the path and route type. - The error digest matches the one sent to the browser, so client reports and server logs can be joined.
instrumentation-client.tsruns in the browser before the app becomes interactive; only its synchronous top-level code is guaranteed to run before hydration.- Next.js emits OpenTelemetry spans for rendering and fetch;
@vercel/otelor the OpenTelemetry SDK exports them.
Clarifying questions expected:
- "Which vendor or collector?" β OpenTelemetry keeps you vendor-neutral.
- "Do you need to correlate browser errors with server logs?" β use the digest.
Code / implementation expected: Yes β both hooks, and client instrumentation.