Astro on Cloudflare
Learn how to add Cloudflare instrumentation to your Astro app.
If you're running your Astro app on Cloudflare Pages, you can use the Sentry Astro SDK in combination with the Sentry Cloudflare SDK to add Sentry instrumentation.
First, install the Sentry Astro SDK in your application. We recommend setting up the Astro SDK manually and manually initializing the SDK. Make sure you have a sentry.client.config.js
file created, but do not add a sentry.server.config.js
file.
After installing the Sentry Astro SDK, you can now install the Sentry Cloudflare SDK. First, install the SDK with your package manager:
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"
},
}
Then create a functions
directory and add a _middleware.js
file to it with the following code:
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.
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
})),
// Add more middlewares here
];
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").