Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Using NewRelic with Sails.js

tutorial

#1

The following is a blog post I just finished on using NewRelic with Sails.js

Thanks :smiley:


I’ve been developing for Procur like a beast lately, and when it came time to drop in a monitoring solution, NewRelic was my first choice. While Sails.js is exploding in popularity, especially with its upcoming v0.10 release, there is still the problem of the lack of support resources for it. While I could easily go to the Sails.js Google group, or Github repo and ask questions, I was in a place where I needed to get the issue resolved quickly so I could get back to working on the actual product. With that, I started digging around.


The NewRelic documentation site says the following regarding plugging NewRelic into a Node.js application:

{<2>}

Let’s tackle step 3 first.

We’ll go ahead and create newrelic.js in the root of the project directory, and fill it with the following:

exports.config = {
  app_name : ['Your App Name'],
  license_key : 'your license key',
  logging : {
    level : 'warn',
    rules: {
      ignore: ['^/socket.io/*/xhr-polling']
    }
  }
};

Note that we’ve included ignore: ['^/socket.io/*/xhr-polling'] as a logging rule. NewRelic tends to report unrealistic and inaccurate transaction times for socket and xhr-polling, so we’ll go ahead and omit those from logging.

Step 4

To any Sails.js developer, adding a module to our application startup would mean going into config/bootstrap.js and adding the require statement in the following way:

module.exports.bootstrap = function (cb) {
  sails.newrelic = require('newrelic');
  cb();
};

Sadly, this didn’t do the trick. The frustrating part is that NewRelic takes a few minutes to begin reporting, and this can lead to some pretty lengthy and irritating debugging.

The remedy!

To correctly install NewRelic into a Sails.js application, simply modify app.js in the root of your project directory in the following way:

var newrelic = require('newrelic');
var sails = require('sails');
var config = require('optimist').argv;
sails.lift(config);


Voila!

With that, you should be up and rolling with NewRelic’s DevOps magic, and beautiful analytic data should start flowing in within minutes.

{<3>}

Happy monitoring!


Config sailsjs for newrelic
#2

Nice post! Thanks for adding it to the forum, and especially for calling out that because of its need to instrument the other modules you’re using, New Relic must be required first, prior to those other modules (including Sails). It’s also great that you included the socket.io ignoring rule in your suggested config – since we don’t currently have visibility inside such transactions, that’s definitely a best-practice that will prevent confusing long transaction times and reduce the likelihood of metric grouping issues.

Do you have any thoughts on how we might improve the wording of our documentation to clarify that New Relic needs to precede all other modules? It sounds like the language we use wasn’t clear for Sails.js since it has its own convention for where to require other modules.


#3

I moved a post to a new topic: Strange transaction names in New Relic for a Sails.js app


#4

Worth mentioning, it took a little work to get NewRelic to report nice looking routing data. I added the following to /api/hooks/newrelic/index.js

var newrelic = require('newrelic');

module.exports = function (sails) {

  return {

    // Pause sails lifting until this hook has completed initializing
    ready: false,

    initialize: function (next) {

      sails.on('router:route', function (route) {
        if (route.req.options && route.req.options.controller) {
          newrelic.setControllerName(route.req.options.controller, route.req.options.action);
        }
      });

      this.ready = true;

      return next();
    }
  }
}

#5

Awesome. Thank you for sharing this, @jim_mpirik!


#6

hey guys,

If your version of sails is >= 0.11.0 then you have hook support. I have collected all the information of this post in a module for be more easy setup newrelic with a fresh sails application.

All information is i the repository: https://github.com/Kikobeats/sails-hook-newrelic


#7

Wow, that is awesome. thank you @kikobeats!!


#8

I’d like to point out a couple typos in the newrelic.js configuration example. To start, the rules.ignore object is placed inside of the logging object. The rules object should be moved outside of the logging object.

Also, the regex rule needs a . in front of the *. I’d advise using the following configuration:

exports.config = {
  app_name : ['Your App Name'],
  license_key : 'your license key',
  logging : {
    level : 'warn'
  },
  rules: {
    ignore: ['^/socket.io/.*/xhr-polling']
  }
};