question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requiring newrelic through NODE_OPTIONS results in out-of-memory exit

See original GitHub issue

Description

I’m running a server with Next.js and want to bring newrelic in for monitoring. I’m not using a custom server with Next.js, so I don’t have a traditional entry-point file that I can put require('newrelic') at the top of. I can though require modules before my server starts, with NODE_OPTIONS="-r newrelic". This works locally for me and in some deployed environments.

However, in a memory limited environment, I’m experiencing intermittent out-of-memory kills (code 137). I suspected this was due to Next.js’ compatibility with newrelic, but creating a small, reproducible example with Next.js experiences the same behaviour.

Expected Behavior

The newrelic agent required with NODE_OPTIONS="-r newrelic" should give the same behaviour as require('newrelic').

Steps to Reproduce

I have two environment variables set, NEW_RELIC_APP_NAME and NEW_RELIC_LICENSE_KEY. To reproduce this, you’ll need to set them to valid values for your environment.

All commands start a Node alpine docker container, install newrelic via npm and attempt to run a “Hello world” node script. They are memory limited to 256MB - but this is arbitrary, as even 512MB gives similar results (it just takes longer to crash).

The below command runs require("newrelic") then console.log("Hello world"). The message will be logged, along with a warning about a missing newrelic.js config file - but the container will exit with code 0.

$ docker run --rm -w /tmp -m 256MB -e NEW_RELIC_APP_NAME -e NEW_RELIC_LICENSE_KEY node:16-alpine sh -c "npm install newrelic && node -e 'require(\"newrelic\");console.log(\"Hello world\")'"

The next command sets NODE_OPTIONS='-r newrelic then runs console.log("Hello world"). The container will exit with code 137, and intermittently this will happen before the message is logged (or the warning about a newrelic.js config is printed).

$ docker run --rm -w /tmp -m 256MB -e NEW_RELIC_APP_NAME -e NEW_RELIC_LICENSE_KEY node:16-alpine sh -c "npm install newrelic && NODE_OPTIONS='-r newrelic' node -e 'console.log(\"Hello world\")'"

This can also be reproduced via npm scripts (i.e. "start": "NODE_OPTIONS='-r newrelic' node ."), putting the commands into a shell script, or when running an npm command or node script.

I’ve reproduced it on node:16-alpine, node:14-alpine, node:16 and node:14.

Your Environment

  • Browser name and version: N/A
  • Node version: 16, 14
  • Operating System and version: Ubuntu via WSL2, Alpine via Docker

Additional context

This could be unrelated to newrelic and is instead an error handling difference between require(...) and NODE_OPTIONS="-r ..." - but I wasn’t able to reproduce this with other packages.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bizob2828commented, Aug 26, 2021

@atkinchris Ok I traced it down in our codebase and logged https://github.com/newrelic/node-native-metrics/issues/155. A few options to get you around this issue if you do not want to wait for a fix:

  • change from NODE_OPTIONS='-r newrelic' to node -r newrelic node_modules/.bin/next start

OR

  • Disable native metrics module in config
    plugins: {
      native_metrics: {
        enabled: false
      }
    }
0reactions
atkinchriscommented, Aug 30, 2021

I’m no longer able to reproduce this bug after the update. Thanks for this! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

OutOfMemory error because of New Relic? - Agents
Hi all, Today I got out of memory error in my server and causes downtime as we need to restart the server. After...
Read more >
Relic Solution: Troubleshooting Java Agent Memory Leaks ...
If you suspect that the Java Agent is causing your application to run out of memory or you've received an OutOfMemoryError , reviewing...
Read more >
[Node] High memory consumption in long-running API calls
Hello I'm using node.js newrelic agent for a long time and everything worked ... I added a long-running API that causes node run...
Read more >
Troubleshooting large memory usage (Node.js)
If you installed the New Relic Node.js agent and your application's memory usage increases read this troubleshooting doc for potential causes and solutions....
Read more >
Out Of Memory on Shared Preferences -> NRPayloadStore.xml
The shared preference file NRPayloadStore.xml is too big and it raised a lot of memory problems in the app. Exists a function that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found