Skip to content

Commit 79f3a35

Browse files
committed
create general Cloudflare Quick Start guide
1 parent 6352f2c commit 79f3a35

File tree

10 files changed

+182
-66
lines changed

10 files changed

+182
-66
lines changed

docs/platforms/javascript/guides/cloudflare/frameworks/astro.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ After installing the Sentry Astro SDK, you can now install the Sentry Cloudflare
1515

1616
## Setup
1717

18+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
19+
1820
<PlatformContent includePath="getting-started-config" />
1921

2022
Then create a `functions` directory and add a `_middleware.js` file to it with the following code:
@@ -33,4 +35,4 @@ export const onRequest = [
3335
})),
3436
// Add more middlewares here
3537
];
36-
```
38+
```

docs/platforms/javascript/guides/cloudflare/frameworks/hono.mdx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ This guide explains how to set up Sentry in your Hono application running on Clo
1111

1212
## Setup
1313

14+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
15+
1416
<PlatformContent includePath="getting-started-config" />
1517

16-
<PlatformContent includePath="getting-started-config" platform="javascript.cloudflare.workers" />
18+
<PlatformContent
19+
includePath="getting-started-config"
20+
platform="javascript.cloudflare.workers"
21+
/>
1722

18-
<PlatformContent includePath="getting-started-config" platform="javascript.hono" />
23+
<PlatformContent
24+
includePath="getting-started-config"
25+
platform="javascript.hono"
26+
/>
1927

2028
## Verify
2129

22-
<PlatformContent includePath="getting-started-verify" platform="javascript.hono" />
30+
<PlatformContent
31+
includePath="getting-started-verify"
32+
platform="javascript.hono"
33+
/>

docs/platforms/javascript/guides/cloudflare/frameworks/nuxt.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ If you installed the SDK before, make sure that `@sentry/nuxt` version `9.37.0`
2424

2525
## Setup
2626

27+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
28+
2729
<PlatformContent includePath="getting-started-config" />
2830

2931
### Adding the Nitro Plugin
@@ -56,5 +58,3 @@ export default defineNitroPlugin(sentryCloudflareNitroPlugin((nitroApp: NitroApp
5658
}
5759
}))
5860
```
59-
60-

docs/platforms/javascript/guides/cloudflare/frameworks/remix.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Now you can install the Sentry Cloudflare SDK. First, install the SDK with your
2323

2424
## Setup
2525

26+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
27+
2628
<PlatformContent includePath="getting-started-config" />
2729

2830
Then create a `_middleware.js` file in your `functions` directory and add the following code:
@@ -39,4 +41,4 @@ export const onRequest = [
3941
})),
4042
// Add more middlewares here
4143
];
42-
```
44+
```

docs/platforms/javascript/guides/cloudflare/frameworks/sveltekit.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ If you installed the SDK before, make sure that `@sentry/sveltekit` version `9.2
2323

2424
## Setup
2525

26+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
27+
2628
<PlatformContent includePath="getting-started-config" />
2729

2830
Next, update your `src/hooks.server.(ts|js)` file to use the `initCloudflareSentryHandle` method from the Sentry Cloudflare SDK and remove the `Sentry.init` call from `@sentry/sveltekit`.
Lines changed: 132 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Cloudflare
3-
description: "Official Sentry SDK for Cloudflare Workers and Cloudflare Pages."
3+
description: "Learn how to manually set up Sentry for Cloudflare Workers and Cloudflare Pages and capture your first errors."
44
sdk: sentry.javascript.cloudflare
55
categories:
66
- javascript
@@ -9,43 +9,49 @@ categories:
99
- serverless
1010
---
1111

12-
You can use the Sentry Cloudflare SDK with popular frameworks running on Cloudflare:
12+
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:
1313

1414
- **[Astro](frameworks/astro/)**
1515
- **[Hono](frameworks/hono/)**
16+
- **[Nuxt](frameworks/nuxt)**
1617
- **[Remix](frameworks/remix/)**
1718
- **[SvelteKit](frameworks/sveltekit/)**
1819

19-
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](frameworks/).
20+
<PlatformContent includePath="getting-started-prerequisites" />
2021

21-
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.
22+
## Step 1: Install
2223

23-
## Features
24-
25-
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/).
26-
27-
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
24+
Choose the features you want to configure, and this guide will show you how:
2825

2926
<OnboardingOptionButtons
3027
options={["error-monitoring", "performance", "logs"]}
3128
/>
3229

33-
## Install
30+
<PlatformContent includePath="getting-started-features-expandable" />
31+
32+
### Install the Sentry SDK
33+
34+
Run the command for your preferred package manager to add the Sentry SDK to your application:
3435

3536
<PlatformContent includePath="getting-started-install" />
3637

37-
## Setup
38+
## Step 2: Configure
39+
40+
The main Sentry configuration should happen as early as possible in your app's lifecycle.
41+
42+
### Wrangler Configuration
3843

3944
<PlatformContent includePath="getting-started-config" />
4045

41-
### Setup (Cloudflare Workers)
46+
### Setup for Cloudflare Workers
4247

4348
<PlatformContent
4449
includePath="getting-started-config"
4550
platform="javascript.cloudflare.workers"
4651
/>
4752

48-
If you do not have access to the `onRequest` middleware API, you can use the `wrapRequestHandler` API instead. For example:
53+
<Expandable title="Don't have access to onRequest?">
54+
If you don't have access to the `onRequest` middleware API, you can use the `wrapRequestHandler` API instead. For example:
4955

5056
```javascript
5157
// hooks.server.js
@@ -64,12 +70,12 @@ export const handle = ({ event, resolve }) => {
6470
};
6571
```
6672

67-
### Setup (Cloudflare Pages)
73+
</Expandable>
6874

69-
To use this SDK, add the `sentryPagesPlugin` as [middleware to your Cloudflare Pages application](https://developers.cloudflare.com/pages/functions/middleware/).
75+
### Setup for Cloudflare Pages
7076

71-
We recommend adding a `functions/_middleware.js` for the middleware setup so that Sentry is initialized for your entire
72-
app.
77+
To use the Sentry SDK, add the `sentryPagesPlugin` as [middleware to your Cloudflare Pages application](https://developers.cloudflare.com/pages/functions/middleware/).
78+
For this, we recommend you create a `functions/_middleware.js` file to set up the middleware for your entire app:
7379

7480
```javascript {filename:functions/_middleware.js}
7581
import * as Sentry from "@sentry/cloudflare";
@@ -92,11 +98,11 @@ export const onRequest = [
9298
// Enable logs to be sent to Sentry
9399
enableLogs: true,
94100
// ___PRODUCT_OPTION_END___ logs
95-
96101
// ___PRODUCT_OPTION_START___ performance
102+
97103
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
98104
// Learn more at
99-
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
105+
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
100106
tracesSampleRate: 1.0,
101107
// ___PRODUCT_OPTION_END___ performance
102108
};
@@ -105,58 +111,135 @@ export const onRequest = [
105111
];
106112
```
107113

108-
### Cloudflare Durable Objects
114+
## Step 3: Add Readable Stack Traces With Source Maps (Optional)
109115

110-
You can use the `instrumentDurableObjectWithSentry` method to instrument [Cloudflare Durable Objects](https://developers.cloudflare.com/durable-objects/). This will need to be done alongside the worker setup.
116+
The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry.
111117

112-
See the [Cloudflare Durable Objects](features/durableobject/) guide for more information.
118+
First, set the `upload_source_maps` option to `true` in your `wrangler.(jsonc|toml)` config file to enable source map uploading:
113119

114-
### Cloudflare Workflows
120+
```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
121+
{
122+
"upload_source_maps": true,
123+
}
124+
```
115125

116-
You can use the `instrumentWorkflowWithSentry` method to instrument [Cloudflare Workflows](https://developers.cloudflare.com/workflows/). This will need to be done alongside the worker setup.
126+
```toml {tabTitle:Toml} {filename:wrangler.toml}
127+
upload_source_maps = true
128+
```
117129

118-
See the [Cloudflare Workflows](features/workflows/) guide for more information.
130+
Next, run the Sentry Wizard to finish your setup:
119131

120-
## Add Readable Stack Traces to Errors
132+
```bash
133+
npx @sentry/wizard@latest -i sourcemaps
134+
```
121135

122-
Depending on how you've set up your project, the stack traces in your Sentry errors probably don't look like your actual code.
136+
## Step 4: Verify Your Setup
123137

124-
To fix this, upload your source maps to Sentry.
138+
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
125139

126-
To start, set the `upload_source_maps` option to `true` in your wrangler config file to enable source map uploading.
140+
### Issues
127141

128-
```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
129-
{
130-
"upload_source_maps": true,
142+
First, let's make sure Sentry is correctly capturing errors and creating issues in your project. Add the following code to a new route that will trigger an error when called.
143+
144+
#### Cloudflare Workers
145+
146+
Add the following code snippet to your main worker file to create a `/debug-sentry` route:
147+
148+
```javascript {filename:index.js}
149+
export default {
150+
async fetch(request) {
151+
const url = new URL(request.url);
152+
153+
if (url.pathname === "/debug-sentry") {
154+
throw new Error("My first Sentry error!");
155+
}
156+
157+
// Your existing routes and logic here...
158+
return new Response("...");
159+
},
160+
};
161+
```
162+
163+
#### Cloudflare Pages
164+
165+
Create a new route by adding the following code snippet to a file in your `functions` directory, such as `functions/debug-sentry.js`:
166+
167+
```javascript {filename:debug-sentry.js}
168+
export async function onRequest(context) {
169+
throw new Error("My first Sentry error!");
131170
}
132171
```
133172

134-
```toml {tabTitle:Toml} {filename:wrangler.toml}
135-
upload_source_maps = true
173+
<OnboardingOption optionId="performance">
174+
### Tracing
175+
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes to run your code.
176+
177+
#### Cloudflare Workers
178+
179+
```javascript {filename:index.js}
180+
export default {
181+
async fetch(request) {
182+
const url = new URL(request.url);
183+
184+
if (url.pathname === "/debug-sentry") {
185+
await Sentry.startSpan(
186+
{
187+
op: "test",
188+
name: "My First Test Transaction",
189+
},
190+
async () => {
191+
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
192+
throw new Error("My first Sentry error!");
193+
}
194+
);
195+
}
196+
197+
// Your existing routes and logic here...
198+
return new Response("...");
199+
},
200+
};
201+
```
202+
203+
#### Cloudflare Pages
204+
205+
```javascript {filename:debug-sentry.js}
206+
export async function onRequest(context) {
207+
await Sentry.startSpan(
208+
{
209+
op: "test",
210+
name: "My First Test Transaction",
211+
},
212+
async () => {
213+
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
214+
throw new Error("My first Sentry error!");
215+
}
216+
);
217+
}
136218
```
137219

138-
Then run the Sentry Wizard to finish your setup:
220+
</OnboardingOption>
139221

140-
<Include name="sourcemaps-wizard-instructions.mdx" />
222+
### View Captured Data in Sentry
141223

142-
For more information on source maps or for more options to upload them, head over to our <PlatformLink to="/sourcemaps/">Source Maps</PlatformLink> documentation.
224+
Now, head over to your project on [Sentry.io](https://sentry.io) to view the collected data (it takes a couple of moments for the data to appear).
143225

144-
## Verify
226+
<PlatformContent includePath="getting-started-verify-locate-data" />
145227

146-
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
228+
## Next Steps
147229

148-
Verify your setup by adding the following snippet anywhere in your code and running it:
230+
At this point, you should have integrated Sentry and should already be sending data to your Sentry project.
149231

150-
```javascript
151-
setTimeout(() => {
152-
throw new Error();
153-
});
154-
```
232+
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
155233

156-
<Alert>
234+
- Learn how to [manually capture errors](/platforms/javascript/guides/cloudflare/usage/)
235+
- Continue to [customize your configuration](/platforms/javascript/guides/cloudflare/configuration/)
236+
- Make use of [Cloudflare-specific features](/platforms/javascript/guides/cloudflare/features)
237+
- Get familiar with [Sentry's product features](/product) like tracing, insights, and alerts
157238

158-
Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>.
239+
<Expandable permalink={false} title="Are you having problems setting up the SDK?">
159240

160-
</Alert>
241+
- Check out setup instructions for popular [frameworks on Cloudflare](/platforms/javascript/guides/cloudflare/frameworks/)
242+
- Find various support topics in <PlatformLink to="/troubleshooting">troubleshooting</PlatformLink>
243+
- [Get support](https://sentry.zendesk.com/hc/en-us/)
161244

162-
To view and resolve the recorded error, log into [sentry.io](https://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.
245+
</Expandable>

platform-includes/getting-started-config/javascript.cloudflare.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Configuration should happen as early as possible in your application's lifecycle.
2-
3-
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.
1+
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:
42

53
```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
64
{
@@ -16,13 +14,13 @@ compatibility_flags = ["nodejs_als"]
1614
# compatibility_flags = ["nodejs_compat"]
1715
```
1816

19-
You will also want to add the `CF_VERSION_METADATA` binding:
17+
Additionally, add the `CF_VERSION_METADATA` binding in the same file:
2018

2119
```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
2220
{
2321
// ...
2422
"version_metadata": {
25-
"binding": "CF_VERSION_METADATA"
23+
"binding": "CF_VERSION_METADATA",
2624
},
2725
}
2826
```

0 commit comments

Comments
 (0)