Add Sentry to backend

I’ve added Sentry to the frontend quite easily but for the backend I have an issue trying to catch something.

I’m using the express “version” of Sentry. Do I need to configure something else in another file than lib/server.js?

...
    const cors = require('cors');
    const Sentry = require('@sentry/node');
    const Tracing = require("@sentry/tracing");
    const app = express();

    Sentry.init({
        dsn: "...",
        integrations: [
            // enable HTTP calls tracing
            new Sentry.Integrations.Http({ tracing: true }),
            // enable Express.js middleware tracing
            new Tracing.Integrations.Express({ app }),
        ],
        environment: process.env.MODE,
    });

    // RequestHandler creates a separate execution context using domains, so that every
    // transaction/span/breadcrumb is attached to its own Hub instance
    app.use(Sentry.Handlers.requestHandler());
    // TracingHandler creates a trace for every incoming request
    app.use(Sentry.Handlers.tracingHandler());

    app.use(
        Sentry.Handlers.errorHandler({
            shouldHandleError(error) {
                console.error('shouldHandleError', error);
                if (error.status === 404 || error.status === 500) {
                    return true;
                }
                return false;
            },
        })
    );

    app.set('trust proxy', true);
...

Does anybody have Sentry working on their Wappler backend?

What is the problem? Not receiving events? Or do you get a specific error?
I only have it on the frontend but maybe I can help.

Interesting integration maybe @patrick check check if we can integrate more generally to Sentry with Server Connect.

6 Likes

I don’t receive events, on front I do but with this implementation of Sentry on the backend I don’t receive events.

I’m trying, to trigger an event, to make impossible database select or error in server connect (the kind of error where with the debug flag on you can see the full stacktrace in front).

But I don’t know how Wappler is build, so I don’t know what kind of errors I can expect to catch with the middleware express from Sentry.

You should add the error handler after all the other routes. In the server.js place it in the start function, there we also add the 404 and error handler for server connect. Place it just in from of those.

3 Likes

Thank you it’s working now :smiley: !

       module.exports = {
           server, app, io,
           start: function(port) {
               app.use(Sentry.Handlers.errorHandler()); // <-- added the error handler here

               // We add the 404 and 500 routes as last
               app.use((req, res) => {
                   res.status(404).json({
                       status: '404',
                       message: `${req.url} not found.`
                   });
               });
3 Likes

Hi @Eldynn - thank you for this - I’d be very interested in integrating Sentry into FE and BE. The free tier only works with a direct integration, rather than via a third party tool like Segment - would you be willing to do a step-by-step instruction of how to integrate into a nodejs project so that (selfishly) I can follow this, and perhaps others on the forum?

Well it’s pretty straightforward for the frontend part, and for the backend you have what you need here.

Tell me what part you are struggling with?

Thanks @Eldynn - I’ll have a run at it tomorrow/day after and report back if I get stuck.

1 Like

@mgaussie I’ve compiled this thread into steps for the back-end :slight_smile: How to install Sentry on the Back-end (NodeJS)

1 Like