TL;DR If you’re using NodeJS and want to monitor using NewRelic, follow these steps. Additionally if you want to do this on Heroku using the NewRelic account you added through the Heroku API, make sure your package.json has “newrelic” and your newrelic.js points to the NEW_RELIC_LICENSE_KEY environment variable.

Last Friday, I set up a NodeJS push notification server for my current project. Essentially it was mostly inspired by this excellent blog post, but I updated it to use the latest NPM packages and to support sending notifications to all of a user’s open sockets as well as cleaning up on disconnect. That’s another story for another time.

This blog post will cover how to get NodeJS monitoring with NewRelic on Heroku.

Some veterans may know that for a while, NewRelic didn’t support NodeJS. That has now changed.

All you really need to do is define your dependencies:

Sample NewRelic package file (node_js_newrelic_package.json) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    "name": "app",
    "description": "",
    "version": "0.0.1",
    "private": true,
    "author": "Lloyd Meta github.com/lloydmeta",
    "dependencies": {
        "express": ">=3.0",
        "socket.io": ">=0.9",
        "async": ">=0.1.22",
        "newrelic": ">=0.9"
    },
    "engines": {
        "node": ">=0.8",
        "npm": ">= 1.1"
    }
}

Run npm install and add require('newrelic'); to your server.js (or whatever you named your NodeJS main file).

Next, copy newrelic.js from node_modules/newrelic into the root directory of your application. Open it up, and as the instructions say, add your licence key for NewRelic into this file. That said, for Heroku users who opted in for the free NewRelic account via Heroku, you want to do this:

newrelic.js (newrelic.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * New Relic agent configuration.
 *
 * See lib/config.defaults.js in the agent distribution for a more complete
 * description of configuration variables and their potential values.
 */
exports.config = {
  /**
   * Array of application names.
   */
  app_name : ['Notifications'],
  /**
   * Your New Relic license key.
   * Make sureto set NEW_RELIC_LICENSE_KEY as this env variable
   */
  license_key : process.env.NEW_RELIC_LICENSE_KEY,
  logging : {
    /**
     * Level at which to log. 'trace' is most useful to New Relic when diagnosing
     * issues with the agent, 'info' and higher will impose the least overhead on
     * production applications.
     */
    level : 'trace'
  }
};

So that you can point to the environment variable that Heroku set up for you when you added NewRelic to your account.

Thats it !

If you have any suggestions, contributions or issues, please feel free to leave a comment.

Comments