Cloudflare
Learn how to manually set up Sentry for Cloudflare Workers and Cloudflare Pages and capture your first errors.
Use this guide for general instructions on using the Sentry SDK with Cloudflare. If you're using any of the listed frameworks, follow their specific setup instructions:
You need:
Choose the features you want to configure, and this guide will show you how:
Run the command for your preferred package manager to add the Sentry SDK to your application:
npm install @sentry/cloudflare --save
The main Sentry configuration should happen as early as possible in your app's lifecycle.
Since the SDK needs access to the AsyncLocalStorage
API, you need to set either the nodejs_compat
or nodejs_als
compatibility flags in your wrangler.(jsonc|toml)
configuration file:
wrangler.jsonc
{
"compatibility_flags": [
"nodejs_als",
// "nodejs_compat"
],
}
Additionally, add the CF_VERSION_METADATA
binding in the same file:
wrangler.jsonc
{
// ...
"version_metadata": {
"binding": "CF_VERSION_METADATA",
},
}
Wrap your worker handler with the withSentry
function, for example, in your index.ts
file, to initialize the Sentry SDK and hook into the environment:
index.ts
import { Hono, HTTPException } from "hono";
import * as Sentry from "@sentry/cloudflare";
export default Sentry.withSentry(
(env: 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,
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
// performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// performance
};
},
// your existing worker export
app
);
To use the Sentry SDK, add the sentryPagesPlugin
as middleware to your Cloudflare Pages application. For this, we recommend you create a functions/_middleware.js
file to set up the middleware 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,
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
// performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// performance
};
}),
// Add more middlewares here
];
The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry.
First, set the upload_source_maps
option to true
in your wrangler.(jsonc|toml)
config file to enable source map uploading:
wrangler.jsonc
{
"upload_source_maps": true,
}
Next, run the Sentry Wizard to finish your setup:
npx @sentry/wizard@latest -i sourcemaps
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
First, let's make sure Sentry is correctly capturing errors and creating issues in your project.
Add the following code snippet to your main worker file to create a /debug-sentry
route that triggers an error when called:
index.js
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/debug-sentry") {
throw new Error("My first Sentry error!");
}
// Your existing routes and logic here...
return new Response("...");
},
};
Create a new route that throws an error when called by adding the following code snippet to a file in your functions
directory, such as functions/debug-sentry.js
:
debug-sentry.js
export async function onRequest(context) {
throw new Error("My first Sentry error!");
}
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes to run your code.
index.js
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/debug-sentry") {
await Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
async () => {
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
throw new Error("My first Sentry error!");
},
);
}
// Your existing routes and logic here...
return new Response("...");
},
};
debug-sentry.js
export async function onRequest(context) {
await Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
async () => {
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
throw new Error("My first Sentry error!");
},
);
}
Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
At this point, you should have integrated Sentry and should already be sending data to your Sentry project.
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
- Learn how to manually capture errors
- Continue to customize your configuration
- Make use of Cloudflare-specific features
- Get familiar with Sentry's product features like tracing, insights, and alerts
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").