Sentry/Raven with Meteor on Server

Sure:

Step 1:
Add the following to your client: (In my case /imports/startup/client/index.js)

Meteor.startup(function () {
    let sentryURL = 'https://' + Meteor.settings.public.sentryPublicKey + '@sentry.io/' + Meteor.settings.public.sentryAppId;
    let settings = {
        release: commitInfo.appHash, // In my case this is the commit hash - but this can be a random string
        environment: Meteor.settings.public.environment, // your environment prod / test / staging ... can also be a random string
        autoBreadcrumbs: {
            http: true // This is cool, auto clicks / navigations / etc.
        },
        shouldSendCallback: function(data) {
            return commitInfo.appHash !== 'dev'; // here you can decide if you want to send the errors to Sentry (in my case if the appHash equals 'dev', I don't want to send the errors.
        }
    };

    ravenClient.config(sentryURL, settings).install();

    let originalMeteorDebug = Meteor._debug;
    Meteor._debug = function(m, s) {
        // We need to asign variables like this. Otherwise,
        // we can't see proper error messages.
        // See: https://github.com/meteorhacks/kadira/issues/193
        var message = m;
        var stack = s;

        // We hate Meteor._debug (no single usage pattern)
        if(message instanceof Error) {
            ravenClient.captureException(message);
        } else if(typeof message === 'string') {
            let extra = {level: "error"};
            //meteor._debug never seems to receive a stack here but just incase let's add it as context.
            if(stack) {
                extra.stack = stack;
            } else {
                //otherwise let's generate a stack trace
                extra.stacktrace = true;
            }
            ravenClient.captureMessage(message, extra);
        }
        return originalMeteorDebug.apply(this, arguments);
    };
});

Step 2 (optional):
Attach the account onLogin & onLogout to also track the user info. (add this in the same file)

Accounts.onLogin(function() {
    ravenClient.setUserContext({
        email: Meteor.user().emails[0].address,
        id: Meteor.userId()
    });
});

Accounts.onLogout(function() {
    ravenClient.setUserContext();
});

Hope this helps!

5 Likes

Thanks a thousand times, @batist. I will attempt this soon.

Could you also explain what to do based on the quote below?

Is it enough to simply run meteor remove meteorhacks:kadira?

What do I do about Meteor.debug?

No idea, I did not remove it yet…

Thanks for this. We are trying it out. One surprise was how enabling autoBreadcrumbs hides the file and line number for console.logs saying printer.js:170 instead of the actual file and line that did the console.log.

I had to alter the line below:

extra.stack = stack;

would result in an Invalid parameter message at sentry. It should become which will actually add the stacktrace for those events.

extra.stacktrace = stack;

Otherwise I received all template helper issues as one error in sentry without stacktrace.

Thanks for this. We were encountering "Discarded invalid parameter ‘stack’ " also.