Requiring newrelic through NODE_OPTIONS results in out-of-memory exit
See original GitHub issueDescription
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:
- Created 2 years ago
- Comments:8 (5 by maintainers)
@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:
NODE_OPTIONS='-r newrelic'
tonode -r newrelic node_modules/.bin/next start
OR
I’m no longer able to reproduce this bug after the update. Thanks for this! 🎉