Note: This SDK is unreleased. Please follow the tracking GH issue for updates.
To get started, first install the @sentry/cloudflare package:
npm install @sentry/cloudflareThen set either the nodejs_compat or nodejs_als compatibility flags in your wrangler.toml. This is because the SDK
needs access to the AsyncLocalStorage API to work correctly.
compatibility_flags = ["nodejs_compat"]
# compatibility_flags = ["nodejs_als"]Then you can either setup up the SDK for Cloudflare Pages or Cloudflare Workers.
To use this SDK, add the sentryPagesPlugin as
middleware to your Cloudflare Pages application.
We recommend adding a functions/_middleware.js for the middleware setup so that Sentry is initialized for your entire
app.
// functions/_middleware.js
import * as Sentry from '@sentry/cloudflare';
export const onRequest = Sentry.sentryPagesPlugin({
dsn: process.env.SENTRY_DSN,
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
tracesSampleRate: 1.0,
});If you need to to chain multiple middlewares, you can do so by exporting an array of middlewares. Make sure the Sentry middleware is the first one in the array.
import * as Sentry from '@sentry/cloudflare';
export const onRequest = [
// Make sure Sentry is the first middleware
Sentry.sentryPagesPlugin({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
// Add more middlewares here
];To use this SDK, wrap your handler with the withSentry function. This will initialize the SDK and hook into the
environment. Note that you can turn off almost all side effects using the respective options.
Currently only ESM handlers are supported.
import * as Sentry from '@sentry/cloudflare';
export default withSentry(
(env) => ({
dsn: env.SENTRY_DSN,
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
tracesSampleRate: 1.0,
}),
{
async fetch(request, env, ctx) {
return new Response('Hello World!');
},
} satisfies ExportedHandler<Env>
);Configure uploading sourcemaps via the Sentry Wizard:
npx @sentry/wizard@latest -i sourcemapsSee more details in our docs.
To set context information or send manual events, use the exported functions of @sentry/cloudflare. Note that these
functions will require the usage of the Sentry helpers, either withSentry function for Cloudflare Workers or the
sentryPagesPlugin middleware for Cloudflare Pages.
import * as Sentry from '@sentry/cloudflare';
// Set user information, as well as tags and further extras
Sentry.setExtra('battery', 0.7);
Sentry.setTag('user_mode', 'admin');
Sentry.setUser({ id: '4711' });
// Add a breadcrumb for future events
Sentry.addBreadcrumb({
message: 'My Breadcrumb',
// ...
});
// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Good bye'));
Sentry.captureEvent({
message: 'Manual',
stacktrace: [
// ...
],
});