Using NewRelic with Sails.js

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:


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: ['^/*/xhr-polling']

Note that we’ve included ignore: ['^/*/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');

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;


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


Happy monitoring!


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 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.

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

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();
1 Like

Awesome. Thank you for sharing this, @jim_mpirik!

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:


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

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: ['^/*/xhr-polling']

Hi I’m having the same issue . Tried solution 1 and solution 2 (with the hook library) but still not getting data ? I have problem with library and posted a issue there … Does someone have any solution for this problem ?

Hey @goran.kuburovic-

It looks like these work arounds previously posted were tested out on an old versions of our agent. Especially since v 2, there are have been many major changes to our instrumentation.

Considering that Sails is unsupported, perhaps someone else in the community may have an approach to solving this on a current version of our agent.



Can u direct me or share a contact of a c. member ? Tnx @sschultz
Best regards

@goran.kuburovic I can’t think of anyone off the top of my head who is using Sails.js, so we will just have to wait and see if anyone jumps into the conversation to help out. :blush: