Install New Relic on Nodejs

2017-03-02

New Relic provides various tools for tracking your applications and infrastructure. It is great for tracking your application’s performance and troubleshooting issues. Two of those tools that we are going to discuss in this article are:

  1. APM
  2. Browser

Installing New Relic APM

Go to New Relic dashboard and select APM from the navigation on top. Click on Add more, select NodeJS from languages and hit Reveal License Key to get your license key for the new app.

Next, go ahead and install the New Relic agent:

npm install --save newrelic

Now copy the newrelic.js file from node_modules/newrelic/ into your root directory.

cp node_modules/newrelic/newrelic.js .

This is your configuration file. Edit the file and add your app_name (this is how your application will show up on the dashboard) and license in the configuration file and you are all set. You should be seeing data in the dashboard within a few minutes!

Configuration File

This is the New Relic configuration file. New Relic supports configurations from 4 different mechanisms in the following order of precedence (Server-side overrides environment variables which overrides agent configurations which overrides default configurations):

  1. Server-Side Configurations
  2. Environment Variables Configurations
  3. Agent Configurations (newrelic.js file)
  4. Agent Defaults Configurations (config.default.js file. This can also be found under node_modules/newrelic/config.default.js)

Agent Defaults Configurations

This file can be found in node_modules/newrelic/config.default.js and contains all the configurations New Relic agent needs. You rarely need to modify this file.

Agent Configurations

This is the newrelic.js file you copied to your project root. All your app level configurations should go in this file. This is what the file looks like:

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
'use strict'
/**
* 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: ['my-application'],
/**
* Your New Relic license key.
*/
license_key: 'a9adb5d5d338488ad93c2fe90e7c2aa6d26d386b',
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: 'info'
}
};

This is the file which can be distributed by your deployment script in case you want to specify different app_name and licenses for your staging/production environments (which is generally a good idea).

license_key is required. app_name is not required but it’s a good idea to set the app_name so that you can identify your application (especially when you have multiple) in the admin dashboard.

Environment Variables

All the configurations supported in newrelic.js and config.default.js can be specified by environment variables by preceeding them with NEW_RELIC_. However, there are two configurations which can only be set via environment variables:

  1. NEW_RELIC_HOME: Points to the path of newrelic.js
  2. NEW_RELIC_NO_CONFIG_FILE: Tells the New Relic Agent to not read newrelic.js

Server-Side Configurations

These are the configurations which are done from the New Relic admin dashboard UI. A small subset of the configurations can be done from the dashboard and overrides any other values of these configurations anywhere else.

Staging Environemnt

There are two ways to manage staging environments:

1. Disable New Relic Agent

In order to do this, set the following environment variable:

NEW_RELIC_ENABLE=false

You can do this permanently in your staging environment or you can set this variable temporarily if you want to disable New Relic from reporting temporarily.

2. Create a Separate App

You can create a separate app for your staging environment and configure the app_name in your newrelic.js:

1
app_name: ["my_application_staging"]

But of course, you will have to pay for a separate instance in this case

Installing New Relic Browser

Head to Browser from the top navigation and hit Add more. From there, you have two ways to setup your browser application. If you are already using APM, the best way to do that will be via APM agent. Just select Enable via APM and then search and select the name of the APM app and hit Enable. That’s it!