# Sentry
This plugin enables you to track errors in your Strapi application using Sentry (opens new window).
By using the Sentry plugin you can:
- Initialize a Sentry instance upon startup of a Strapi application
- Send Strapi application errors as events to Sentry
- Include additional metadata in Sentry events to assist in debugging
- Expose a global Sentry service
Begin by first installing the Sentry plugin, and then configuring the plugin to enable your Strapi application to send events to the Sentry instance.
# Installation
Install the Sentry plugin by adding the dependency to your Strapi application as follows:
npm install @strapi/plugin-sentry
yarn add @strapi/plugin-sentry
# Configuration
Create or edit your ./config/plugins.js
file to configure the Sentry plugin. The following properties are available:
Property | Type | Default Value | Description |
---|---|---|---|
dsn | string | null | Your Sentry data source name (opens new window). |
sendMetadata | boolean | true | Whether the plugin should attach additional information (e.g. OS, browser, etc.) to the events sent to Sentry. |
init | object | {} | A config object that is passed directly to Sentry during initialization. See the official Sentry documentation (opens new window) for all available options. |
An example configuration:
// path: ./config/plugins.js module.exports = ({ env }) => ({ // ... sentry: { enabled: true, config: { dsn: env('SENTRY_DSN'), sendMetadata: true, }, }, // ... });
Copied to clipboard!
# Environment configuration
Using the env
utility, you can enable or disable the Sentry plugin based on the environment. For example, to only enable the plugin in your production
environment:
// path: ./config/plugins.js module.exports = ({ env }) => ({ // ... sentry: { enabled: env('NODE_ENV') === 'production', }, // ... });
Copied to clipboard!
# Global Sentry service access
After installing and configuring the plugin, you can access a Sentry service in your Strapi application as follows:
const sentryService = strapi.plugin('sentry').service('sentry');
Copied to clipboard!
This service exposes the following methods:
Method | Description | Parameters |
---|---|---|
sendError() | Manually send errors to Sentry. |
See the official Sentry documentation (opens new window) for more details. |
getInstance() | Used for direct access to the Sentry instance. |
Below are examples for each method.
try { // Your code here } catch (error) { // Either send a simple error strapi .plugin('sentry') .service('sentry') .sendError(error); // Or send an error with a customized Sentry scope strapi .plugin('sentry') .service('sentry') .sendError(error, (scope, sentryInstance) => { // Customize the scope here scope.setTag('my_custom_tag', 'Tag value'); }); throw error; }
Copied to clipboard!