Integrate Sentry With Strapi

by Sujay Prabhu, Senior System Analyst

Sentry is a platform that notifies exceptions or errors that a user runs into while using the application. It can also be used for performance monitoring, setting alerts of errors and monitoring the health of each releases. Sentry can be integrated with Strapi in a couple of ways.

Using the official plugin: @strapi/plugin-sentry

  • This is the official Sentry plugin for Strapi which can be used to track errors.
  • Install the plugin using the command:
yarn add @strapi/plugin-sentry // yarn
npm install @strapi/plugin-sentry // npm
  • In order to track error events, we have to enable the plugin by adding it to plugins configuration file i.e. ./config/plugins.js
  sentry: {
    enabled: true,
    config: {
      dsn: process.env.SENTRY_DSN,
      sendMetadata: true,
    },
  }

Here is the link of the pull request for integrating Sentry using officail plugin.

Note: strapi-plugin-sentry was the plugin to be used with Strapi v3 and it is deprecated with Strapi v4

Create custom middleware in Strapi

  • This approach can be used to leverage the benefits of Sentry like performance monitoring, track release health, reporting errors.
  • Initialize the Sentry SDK and capture the exceptions in the custom middleware.
  • Common options that can be passed to SDK are:
    • dsn: Dsn is a unique identifier for the project created in Sentry and it will let SDK know where to log the events.
    • environment: This will set the current environment of the project and this will also help to filter errors or exceptions based on enviroment in Sentry.
    • tracesSampleRate: This will be between 0 and 1 where 0 means 0% and 1 means 100%. This can be used to decide the percentage of transactions that can be logged in Sentry.
// src/middlewares/sentry.js

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: strapi.config.environment,
  integrations: [new Sentry.Integrations.Http({ tracing: true })],
  tracesSampleRate: 1.0,
});

module.exports = (config, { strapi }) => {
  return async (ctx, next) => {
    try {
      await next();
    } catch (error) {
      // Error object or string can be passed to captureException
      Sentry.captureException(error);
      throw error;
    }
  };
};
  • Once middleware is created, it needs to be added to the middlewares configuration file
// config/middleware.js

[
  // ... existing middlewares
  'global::sentry',
];

Here is the link of the pull request for integrating Sentry by creating custom middleware.

Related Articles:

Sentry for Koa

Why Koa?

Middlewares in Strapi

More articles

Operating Kafka in Rails with Karafka: Production Architecture, Consumers, and DLQs (Part 2)

In Part 2, we dive deep into the Sync-Out pipeline—how Rails publishes events to Kafka, how our legacy adapter writes to SQL Server 2009 using TinyTDS, and how Dead-Letter Queues (DLQs) became our lifeline during production incidents. This post covers transaction management, service objects, and operational workflows for handling failures.

Read more

Operating Kafka in Rails with Karafka: Production Architecture, Consumers, and DLQs (Part 1)

This post breaks down our production architecture for event streaming in Rails using Kafka and Karafka—from designing producers and consumer flows to handling failures with DLQs and keeping warehouse databases in sync reliably.

Read more

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals