How to install Sentry on the Back-end (NodeJS)

With the upcoming update you can have in your custom route next to exports.handler an exports.before and exports.after where the before is like the handler and comes before the generated routes and the after comes after all the routes are generated.

So code would become like:

const Sentry = require('@sentry/node');

exports.before = function(app) {
  Sentry.init({ dsn: "CHANGE_ME" });
  app.use(Sentry.Handlers.requestHandler());
};

exports.after = function(app) {
  app.use(Sentry.Handlers.errorHandler());
};

Save this to extensions/server_connect/routes/sentry.js.

6 Likes

@patrick Thanks!!!

Implemented in Wappler 3.9.5

Hello guys! How to get Sentry to track only production environment?

@otavionestares You can use ENV variables or maybe even globals to get the environment. Something like this should do it:

const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
require('dotenv').config();

if (process.env.MODE === 'production') {
  Sentry.init({
    dsn: process.env.SENTRY_DSN,
    integrations: [
      new Sentry.Integrations.Http({ tracing: true }),
      new Tracing.Integrations.Express({ app }),
    ],
    environment: process.env.MODE,
  });
}

exports.handler = function (app) {
  if (process.env.MODE === 'production') {
    app.use(Sentry.Handlers.requestHandler());
    app.use(Sentry.Handlers.tracingHandler());
    app.use(Sentry.Handlers.errorHandler());
  }
};

2 Likes

This is awesome! :tada: :tada: :tada: So the only things you need to do to get Sentry working is?

  1. create a Sentry project, choose for express and obtain dsn
  2. npm install --save @sentry/node @sentry/tracing in your terminal
  3. create file extensions/server_connect/routes/sentry.js with Patrick’s contents

And this keeps sentry installed despite Wappler updates?