Cloudflare
Official Sentry SDK for Cloudflare Workers and Cloudflare Pages.
You can use the Sentry Cloudflare SDK with popular frameworks running on Cloudflare:
Take a look at your framework of choice, as there are likely additional instructions for setting up Sentry with it. For more framework-specific guidance, see the frameworks section.
If you're not using one of these frameworks, or are just looking for general instructions for Cloudflare usage, you're in the right place.
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.jsonc
/ wrangler.toml
config. This is because the SDK needs access to the AsyncLocalStorage
API to work correctly.
wrangler.jsonc
{
"compatibility_flags": [
"nodejs_als",
// "nodejs_compat"
],
}
You will also want to add the CF_VERSION_METADATA
binding:
wrangler.jsonc
{
// ...
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
}
Wrap your worker 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.
index.ts
import { Hono, HTTPException } from "hono";
import * as Sentry from "@sentry/cloudflare";
export default Sentry.withSentry(
(env) => {
const { id: versionId } = env.CF_VERSION_METADATA;
return {
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
release: versionId,
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
// performance
};
},
// your existing worker export
app,
);
If you do not have access to the onRequest
middleware API, you can use the wrapRequestHandler
API instead. For example:
// hooks.server.js
import * as Sentry from "@sentry/cloudflare";
export const handle = ({ event, resolve }) => {
const requestHandlerOptions = {
options: {
dsn: event.platform.env.SENTRY_DSN,
tracesSampleRate: 1.0,
},
request: event.request,
context: event.platform.ctx,
};
return Sentry.wrapRequestHandler(requestHandlerOptions, () =>
resolve(event),
);
};
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) => {
const { id: versionId } = env.CF_VERSION_METADATA;
return {
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
release: versionId,
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
// performance
};
}),
// Add more middlewares here
];
You can use the instrumentDurableObjectWithSentry
method to instrument Cloudflare Durable Objects. This will need to be done alongside the worker setup.
See the Cloudflare Durable Objects guide for more information.
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, set the upload_source_maps
option to true
in your wrangler config file to enable source map uploading.
wrangler.jsonc
{
"upload_source_maps": true,
}
Then run the Sentry Wizard to finish your setup:
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").