newrelic / node-newrelic

New Relic Node.js agent code base. Developers are welcome to create pull requests here, please see our contributing guidelines. For New Relic technical support, please go to http://support.newrelic.com.
Apache License 2.0
967 stars 395 forks source link

Missing error details when module initialization throws during SSR #2420

Closed DrewML closed 1 month ago

DrewML commented 1 month ago

Description

I'm investigating an issue in my employer's app where we see an increase in 500 errors in New Relic, but these errors do not have any context or stack traces. Looking through our application logs, we see Next.js logging the actual error objects directly to console.error, but the NR agent doesn't seem to capture this info.

As a test, we added some code locally to purposely throw, during a request to the / route, during the top-level execution of one of our modules. That code is:

if (process.env.NEXT_PHASE !== 'phase-production-build') {
  // emulate an error thrown as a side-effect of module initialization.
  // process.env check is to make sure the build passes, to make sure this is a runtime error
  // and not a build error
  throw new Error('this error is not captured by New Relic');
}

We then traced the execution path through Next.js code to determine where these errors were being passed to console.error, and determined the error is caught (and logged) inside of renderToResponseImpl. The error is then passed to the renderErrorToResponse method.

My suspicion is that this package, newrelic-node-nextjs, is missing instrumentation of the renderErrorToResponse method, which would unlock the ability to capture the Error object associated with the failed request and 500 error.

Expected Behavior

If an exception is thrown as a side-effect of module initialization in the dependency graph for a Next.js app, that error is reported to New Relic.

Troubleshooting or NR Diag results

Steps to Reproduce

  1. Get a Next.js app setup with the "Pages Router" (as opposed to "App Router") and newrelic-node-nextjs
  2. In the top-level of pages/index.tsx, throw an error using the sample code provided in this ticket
  3. Run a build
  4. Run the app in Production mode, and navigate to the / route in your browser
  5. Confirm a generic 500 was reported to New Relic

Your Environment

Additional context

workato-integration[bot] commented 1 month ago

https://new-relic.atlassian.net/browse/NR-295323

bizob2828 commented 1 month ago

@DrewML sorry for your troubles. Thanks for the repro, I'll triage and provide an update.

DrewML commented 1 month ago

Thanks @bizob2828! Super appreciated.

bizob2828 commented 1 month ago

@DrewML the Next.js instrumentation doesn't automatically handle errors. This example application demonstrates how to handle errors here. If you use this application you will see errors getting logged.

workato-integration[bot] commented 1 month ago

https://new-relic.atlassian.net/browse/NR-297527

bizob2828 commented 1 month ago

Moving this issue to node-newrelic as the Next.js instrumentation has been merged into agent

bizob2828 commented 1 month ago

I'm closing this based on my comment from 3 weeks ago. If this doesn't meet your needs, please reopen with more details