Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocking controller instrumentation #91

Closed
Prinzhorn opened this issue Dec 10, 2013 · 5 comments
Closed

Blocking controller instrumentation #91

Prinzhorn opened this issue Dec 10, 2013 · 5 comments

Comments

@Prinzhorn
Copy link

Is there a node equivalent to this https://docs.newrelic.com/docs/ruby/blocking-controller-instrumentation ?

I've set up Availability monitoring but the ping tampers with the stats (much more requests, much less errors, much less response time etc). How do I create a connect/express route which will not be tracked? E.g. sth. like a middleware which I can add to the routes:

var newrelic = require('newrelic/utils');

//......

app.get('/ping', newrelic.doNotTrack(), pingController);
@othiym23
Copy link
Contributor

something like this should do the trick:

var newrelic = require('newrelic');

// ...elsewhere...
function pingController(req, res, next) {
  newrelic.setIgnoreTransaction(true);

  // ...other pingController stuff...

  next();
}

// ...elsewhere...
app.get('/ping', pingController);

You could probably create a quick and dirty middleware to call setIgnoreTransaction(true) for you if you need to use it for a bunch of routes. Something to keep in mind is that even if you disable the agent via configuration, the module will still export a stub API, so you don' t have to put the New Relic API calls in conditional blocks.

Let us know if that doesn't work!

@Prinzhorn
Copy link
Author

Looks great, thanks. Two questions though

  1. I'm currently doing this
if(process.env.NEW_RELIC_LICENSE_KEY) {
    require('newrelic');
}

is it correct to remove the if-block and instead set NEW_RELIC_ENABLED to false in dev/test env?

  1. I assume it's save to require the module multiple times, right? Because I'd need to require it a second time inside the middleware.

@othiym23
Copy link
Contributor

  1. is it correct to remove the if-block and instead set NEW_RELIC_ENABLED to false in dev/test env?

Yes. Just make sure that you set it before you require('newrelic')

  1. I assume it's save to require the module multiple times, right? Because I'd need to require it a second time inside the middleware.

Yes, the module bootstraps a single agent / API and then returns the same object each time you require the module.

@Prinzhorn
Copy link
Author

Just wanted to let you know it seems to work as expected.

Route definition:

app.get('/ping', ignoreNewrelic(), controllers.web.ping);

ignoreNewrelic middleware:

var newrelic = require('newrelic');

module.exports = function() {
    return function(req, res, next) {
        newrelic.setIgnoreTransaction(true);
        next();
    };
};

Ping controller:

module.exports = function(req, res, next) {
    res.end('pong');
};

@othiym23
Copy link
Contributor

Great! BTW, you don't need ignoreNewrelic to be a generator, because it's not capturing anything in the closure. You can just write it as

var api = require('newrelic');
module.exports = function (req, res, next) {
  api.setIgnoreTransaction(true);
  next();
}

Glad it's working for you!

cmcadams-newrelic pushed a commit to cmcadams-newrelic/node-newrelic that referenced this issue Jan 29, 2024
…nestjs/app/protobufjs-7.2.4

chore(deps): bump protobufjs from 7.2.3 to 7.2.4 in /nestjs/app
jsumners-nr pushed a commit to jsumners-nr/node-newrelic that referenced this issue Apr 11, 2024
jsumners-nr pushed a commit to jsumners-nr/node-newrelic that referenced this issue Apr 16, 2024
bizob2828 pushed a commit to bizob2828/node-newrelic that referenced this issue Apr 19, 2024
removed node 10 from ci, updated engines to >=12, added files list an…
bizob2828 pushed a commit to bizob2828/node-newrelic that referenced this issue Apr 23, 2024
removed node 10 from ci, updated engines to >=12, added files list an…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants