docs/plugins/sentry.mdx
This plugin allows you to integrate Sentry seamlessly with your Payload application.
Sentry is a powerful error tracking and performance monitoring tool that helps developers identify, diagnose, and resolve issues in their applications.
<Banner type="success"> Sentry does smart stuff with error data to make bugs easier to find and fix. - [sentry.io](https://sentry.io/) </Banner>This multi-faceted software offers a range of features that will help you manage errors with greater ease and ultimately ensure your application is running smoothly:
Install the plugin using any JavaScript package manager like pnpm, npm, or Yarn:
pnpm add @payloadcms/plugin-sentry
This plugin requires to complete the Sentry + Next.js setup before.
You can use either the automatic setup with the installation wizard:
npx @sentry/wizard@latest -i nextjs
Or the Manual Setup
In the plugins array of your Payload Config, call the plugin and pass in your Sentry DSN as an option.
import { buildConfig } from 'payload'
import { sentryPlugin } from '@payloadcms/plugin-sentry'
import { Pages, Media } from './collections'
import * as Sentry from '@sentry/nextjs'
const config = buildConfig({
collections: [Pages, Media],
plugins: [sentryPlugin({ Sentry })],
})
export default config
If you want Sentry to capture Postgres query performance traces, you need to inject the Sentry-patched pg driver into the Postgres adapter. This ensures Sentry’s instrumentation hooks into your database calls.
import * as Sentry from '@sentry/nextjs'
import { buildConfig } from 'payload'
import { sentryPlugin } from '@payloadcms/plugin-sentry'
import { postgresAdapter } from '@payloadcms/db-postgres'
import pg from 'pg'
export default buildConfig({
db: postgresAdapter({
pool: { connectionString: process.env.DATABASE_URL },
pg, // Inject the patched pg driver for Sentry instrumentation
}),
plugins: [sentryPlugin({ Sentry })],
})
Sentry : Sentry | required
The Sentry instance
enabled: boolean | optional
Set to false to disable the plugin. Defaults to true.
context: (args: ContextArgs) => Partial<ScopeContext> | Promise<Partial<ScopeContext>>
Pass additional contextual data to Sentry
captureErrors: number[] | optional
By default, Sentry.errorHandler will capture only errors with a status code of 500 or higher. To capture additional error codes, pass the values as numbers in an array.
Configure any of these options by passing them to the plugin:
import { buildConfig } from 'payload'
import { sentryPlugin } from '@payloadcms/plugin-sentry'
import * as Sentry from '@sentry/nextjs'
import { Pages, Media } from './collections'
const config = buildConfig({
collections: [Pages, Media],
plugins: [
sentryPlugin({
options: {
captureErrors: [400, 403],
context: ({ defaultContext, req }) => {
return {
...defaultContext,
tags: {
locale: req.locale,
},
}
},
debug: true,
},
Sentry,
}),
],
})
export default config
All types can be directly imported:
import { PluginOptions } from '@payloadcms/plugin-sentry'