thom_nic
thom_nic

Reputation: 8143

Sentry, sequelizejs: Is there a way to capture the SQL statement that cause the error?

Sentry captures an error from a failed sequelize operation, but the message is generic. Is it possible to capture the SQL statement that caused the error? (screenshot below)

The Sequelize API documentation does not indicate there are any additional properties (like error.query?) that would achieve what I'm looking for. I know it's possible to turn on statement logging in sequelize but that's just printing to stdout. Also because there are many queries going on asynchronously, they could not all just be added as breadcrumbs or something.

Sentry does not appear to have integrations for sequelize or sqlite3 out of the box. Actually if I knew how to retrieve the statement I think I could manage attaching it to the Sentry context.

The only useful piece of information that Sentry gives me, is the very last stack trace frame that sentry displays is code from my application. So I may be able to infer what the statement looked like from the code. But this is still not as helpful as seeing the actual statement. Is this possible?

sentry event screenshot

Upvotes: 0

Views: 278

Answers (1)

lhaley2011
lhaley2011

Reputation: 26

If you can switch to OpenTelemetry for instrumentation, you can use this package.
OpenTelemetry instrumentation works with Sentry.

https://opentelemetry.io/ecosystem/registry/?s=sequelize
https://www.npmjs.com/package/opentelemetry-instrumentation-sequelize

If you can't switch instrumentation packages you can use something like this to get the query via log messages

const sequelize = new Sequelize({
        dialect: 'sqlite',
        storage: ':memory:',
        logging: (msg) => {
            const match = msg.match(/Executing \(.*\)\:(.*)/);
            if (match) {
                console.log(match[1].trim());
            } else {
                console.log(msg);
            }
        }
    });

Upvotes: 1

Related Questions