Skip to content

Commit cf62806

Browse files
authored
feat(cloudflare): Add framework specific guides (#11396)
1 parent ebd922a commit cf62806

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Cloudflare Astro Framework Guide
3+
description: "Learn how to add Cloudflare instrumentation to your Astro app."
4+
---
5+
6+
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.
7+
8+
First, install the Sentry Astro SDK in your application. We recommend [setting up the Astro SDK manually](/platforms/javascript/guides/astro/manual-setup/) 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.
9+
10+
After installing the Sentry Astro SDK, you can now install the Sentry Cloudflare SDK. First, install the SDK with your package manager:
11+
12+
<PlatformContent includePath="getting-started-install" />
13+
14+
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
15+
needs access to the `AsyncLocalStorage` API to work correctly.
16+
17+
```toml {filename:wrangler.toml}
18+
compatibility_flags = ["nodejs_compat"]
19+
# compatibility_flags = ["nodejs_als"]
20+
```
21+
22+
Then create a `functions` directory and add a `_middleware.js` file to it with the following code:
23+
24+
```javascript {filename:functions/_middleware.js}
25+
import * as Sentry from "@sentry/cloudflare";
26+
27+
export const onRequest = [
28+
// Make sure Sentry is the first middleware
29+
Sentry.sentryPagesPlugin((context) => ({
30+
dsn: "___PUBLIC_DSN___",
31+
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
32+
tracesSampleRate: 1.0,
33+
})),
34+
// Add more middlewares here
35+
];
36+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Cloudflare SDK Framework Guide
3+
description: "Learn how to set up the Cloudflare SDK with popular Frameworks like SvelteKit or Remix."
4+
---
5+
6+
You can use the Sentry Cloudlare SDK in combination with Sentry meta-framework SDKs like SvelteKit or Remix to instrument your Cloudflare Pages applications that use these frameworks.
7+
8+
<PageGrid />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Cloudflare Remix Framework Guide
3+
description: "Learn how to add Cloudflare instrumentation to your Remix app."
4+
---
5+
6+
If you're running your Remix app on Cloudflare Pages, you can use the Sentry Remix SDK in combination with the Sentry Cloudflare SDK to add Sentry instrumentation.
7+
8+
First, install the Sentry Remix SDK in your application. We recommend using the Sentry wizard to automatically install the SDK:
9+
10+
```bash
11+
npx @sentry/wizard@latest -i remix
12+
```
13+
14+
If the setup through the wizard doesn't work for you, you can also [set up the Remix SDK manually](/platforms/javascript/guides/remix/manual-setup/).
15+
16+
After installing the Sentry Remix SDK, delete the newly generated `instrumentation.server.mjs` file. This instrumentation is not needed when using the Cloudflare SDK.
17+
18+
Now you can install the Sentry Cloudflare SDK. First, install the SDK with your package manager:
19+
20+
<PlatformContent includePath="getting-started-install" />
21+
22+
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
23+
needs access to the `AsyncLocalStorage` API to work correctly.
24+
25+
```toml {filename:wrangler.toml}
26+
compatibility_flags = ["nodejs_compat"]
27+
# compatibility_flags = ["nodejs_als"]
28+
```
29+
30+
Then create a `_middleware.js` file in your `functions` directory and add the following code:
31+
32+
```javascript {filename:functions/_middleware.js}
33+
import * as Sentry from "@sentry/cloudflare";
34+
35+
export const onRequest = [
36+
// Make sure Sentry is the first middleware
37+
Sentry.sentryPagesPlugin((context) => ({
38+
dsn: "___PUBLIC_DSN___",
39+
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
40+
tracesSampleRate: 1.0,
41+
})),
42+
// Add more middlewares here
43+
];
44+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: Cloudflare Sveltekit Framework Guide
3+
description: "Learn how to add Cloudflare instrumentation to your SvelteKit app."
4+
---
5+
6+
If you're running your SvelteKit app on Cloudflare Pages, you can use the Sentry SvelteKit SDK in combination with the Sentry Cloudflare SDK to add Sentry instrumentation.
7+
8+
First, install the Sentry SvelteKit SDK in your application. We recommend using the Sentry wizard to automatically install the SDK:
9+
10+
```bash
11+
npx @sentry/wizard@latest -i sveltekit
12+
```
13+
14+
If the setup through the wizard doesn't work for you, you can also [set up the SvelteKit SDK manually](/platforms/javascript/guides/sveltekit/manual-setup/).
15+
16+
After installing the Sentry SvelteKit SDK, you can move on to setting up the Cloudflare SDK. First, install the SDK with your package manager:
17+
18+
<PlatformContent includePath="getting-started-install" />
19+
20+
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
21+
needs access to the `AsyncLocalStorage` API to work correctly.
22+
23+
```toml {filename:wrangler.toml}
24+
compatibility_flags = ["nodejs_compat"]
25+
# compatibility_flags = ["nodejs_als"]
26+
```
27+
28+
To use this SDK, update your `src/hooks.server.ts` file to use the `Sentry.wrapRequestHandler` method from the Sentry Cloudflare SDK and remove the `Sentry.init` call from `@sentry/sveltekit`.
29+
30+
```diff
31+
import { sequence } from "@sveltejs/kit/hooks";
32+
import { handleErrorWithSentry, sentryHandle } from "@sentry/sveltekit";
33+
-import * as Sentry from '@sentry/sveltekit';
34+
+import * as Sentry from "@sentry/cloudflare";
35+
36+
-Sentry.init({
37+
- dsn: '___PUBLIC_DSN___',
38+
- tracesSampleRate: 1.0,
39+
-
40+
- // uncomment the line below to enable Spotlight (https://spotlightjs.com)
41+
- // spotlight: import.meta.env.DEV,
42+
-});
43+
-
44+
+const handleInitSentry = ({ event, resolve }) => {
45+
+ return event.platform
46+
+ ? Sentry.wrapRequestHandler(
47+
+ {
48+
+ options: {
49+
+ dsn: '___PUBLIC_DSN___',
50+
+ tracesSampleRate: 1.0,
51+
+ },
52+
+ request: event.request,
53+
+ context: event.platform.ctx,
54+
+ },
55+
+ () => resolve(event)
56+
+ )
57+
+ : resolve(event);
58+
+};
59+
-// If you have custom handlers, make sure to place them after `sentryHandle()` in the `sequence` function.
60+
-export const handle = sequence(sentryHandle());
61+
+export const handle = sequence(handleInitSentry, sentryHandle());
62+
```
63+
64+
If you need to use environmental variables, you can access them using `event.platform.env`.

0 commit comments

Comments
 (0)