Detecting process crashes with New Relic in NodeJS
Asked Answered
M

0

6

How do you get New Relic to log errors which crash the process using their New Relic application monitoring? The key things I'm trying to accomplish are to have errors which crash the process still get logged and some way to filter to these errors on the dashboard.

Here's my understanding so far:

  1. New Relic transmits data to their cloud every minute. In the event a uncaughtException occurs, this will result in all of the data currently waiting to be transmitted to be lost.

  2. There is a newrelic.noticeError() which is supposed to take a second argument allowing you to pass custom parameters with an error. New relic gets the error, but not the custom parameters.

Simple Example:

var newrelic = require("newrelic");
var express = require("express");
var app = express();

app.get("/softFail/", function(req, res) {
    res.send(500, "softFail");
});

app.get("/hardFail/", function(req, res) {
    setImmediate(function() {
        throw new Error("I failed");
    });
});

app.listen(80);

process.on("uncaughtException", function(err) {
    console.error("Uncaught Exception");
    console.error(err.stack);
    newrelic.addCustomParameter("crash", "true");
    newrelic.noticeError(err);
    console.log("sending errors to New Relic");
    newrelic.agent.harvest(function() {
        console.log("send complete, crashing process");
        process.exit(1);
    });
});

Using that block of code if I call /hardFail/ I'm able to get New Relic to at least log the error. Without the uncaughtException handler I don't get anything to New Relic. The issue is that I can't differentiate between the errors which crash the process, and normal HTTP 500 errors.

Here's something things I've tried:

  1. If I attempt to add { crash : true } to the noticeError call it doesn't seem to have any affect.

  2. I've tried to do domain instead of process.on, that doesn't change the situation.

  3. If I attempt to alter the name of the error, such as err.name = "CrashError" then the error doesn't get transmitted at all.

  4. If I create a custom error type, and then new that, and copy the stack trace on to it, it still reports as type Error rather than the prototype.name of my new error type.

Mayle answered 17/11, 2014 at 1:6 Comment(4)
Thanks! I was missing the call to harvest, you helped me a lot!Faizabad
Is there a solution? How did you get it to work?Athenian
Unfortunately we were never able to find a solution for it. We've actually stopped using Newrelic's application monitoring in favor of Loggly. It's made error logging and process logging a lot easier.Mayle
I've reported a similar issue to the node-newrelic team: github.com/newrelic/node-newrelic/issues/1561Faizabad

© 2022 - 2024 — McMap. All rights reserved.