Cloudflare
Official Sentry SDK for Cloudflare Workers and Cloudflare Pages.
We've released version 8 of the JavaScript SDKs. If you're using version 7.x, we recommend upgrading to the latest version. Check out the Migration docs to learn how to make the switch.
Don't have a Sentry account? Sign up for Sentry for free, then return to this page.
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also get to the root of an error or performance issue faster, by watching a video-like reproduction of a user session with session replay.
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing.
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
Sentry captures data by using an SDK within your application’s runtime.
npm install @sentry/cloudflare --save
Configuration should happen as early as possible in your application's lifecycle.
To use the SDK, you'll need to 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.
wrangler.toml
compatibility_flags = ["nodejs_compat"]
# compatibility_flags = ["nodejs_als"]
Then you can install the SDK and add it to your project:
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 = [
// Make sure Sentry is the first middleware
Sentry.sentryPagesPlugin((context) => ({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
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.
import * as Sentry from '@sentry/cloudflare';
export default withSentry(
env => ({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// 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>,
);
Depending on how you've set up your project, the stack traces in your Sentry errors probably don't look like your actual code.
To fix this, upload your source maps to Sentry.
To start, add upload_source_maps = true
to your wrangler.toml
file to enable source map uploading.
Then run the Sentry Wizard to finish your set up:
npx @sentry/wizard@latest -i sourcemaps
The wizard will guide you through the following steps:
- Logging into Sentry and selecting a project
- Installing the necessary Sentry packages
- Configuring your build tool to generate and upload source maps
- Configuring your CI to upload source maps
For more information on source maps or for more options to upload them, head over to our Source Maps documentation.
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
Verify your setup by adding the following snippet anywhere in your code and running it:
setTimeout(() => {
throw new Error();
});
Learn more about manually capturing an error or message in our Usage documentation.
To view and resolve the recorded error, log into sentry.io and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").