Skip to content

docs(js): Create general Cloudflare Quick Start guide #14541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ After installing the Sentry Astro SDK, you can now install the Sentry Cloudflare

## Setup

The main Sentry configuration should happen as early as possible in your app's lifecycle.

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

Then create a `functions` directory and add a `_middleware.js` file to it with the following code:
Expand All @@ -33,4 +35,4 @@ export const onRequest = [
})),
// Add more middlewares here
];
```
```
17 changes: 14 additions & 3 deletions docs/platforms/javascript/guides/cloudflare/frameworks/hono.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ This guide explains how to set up Sentry in your Hono application running on Clo

## Setup

The main Sentry configuration should happen as early as possible in your app's lifecycle.

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

<PlatformContent includePath="getting-started-config" platform="javascript.cloudflare.workers" />
<PlatformContent
includePath="getting-started-config"
platform="javascript.cloudflare.workers"
/>

<PlatformContent includePath="getting-started-config" platform="javascript.hono" />
<PlatformContent
includePath="getting-started-config"
platform="javascript.hono"
/>

## Verify

<PlatformContent includePath="getting-started-verify" platform="javascript.hono" />
<PlatformContent
includePath="getting-started-verify"
platform="javascript.hono"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ If you installed the SDK before, make sure that `@sentry/nuxt` version `9.37.0`

## Setup

The main Sentry configuration should happen as early as possible in your app's lifecycle.

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

### Adding the Nitro Plugin
Expand Down Expand Up @@ -56,5 +58,3 @@ export default defineNitroPlugin(sentryCloudflareNitroPlugin((nitroApp: NitroApp
}
}))
```


Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Now you can install the Sentry Cloudflare SDK. First, install the SDK with your

## Setup

The main Sentry configuration should happen as early as possible in your app's lifecycle.

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

Then create a `_middleware.js` file in your `functions` directory and add the following code:
Expand All @@ -39,4 +41,4 @@ export const onRequest = [
})),
// Add more middlewares here
];
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ If you installed the SDK before, make sure that `@sentry/sveltekit` version `9.2

## Setup

The main Sentry configuration should happen as early as possible in your app's lifecycle.

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

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`.
Expand Down
181 changes: 132 additions & 49 deletions docs/platforms/javascript/guides/cloudflare/index.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Cloudflare
description: "Official Sentry SDK for Cloudflare Workers and Cloudflare Pages."
description: "Learn how to manually set up Sentry for Cloudflare Workers and Cloudflare Pages and capture your first errors."
sdk: sentry.javascript.cloudflare
categories:
- javascript
Expand All @@ -9,43 +9,49 @@ categories:
- serverless
---

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

- **[Astro](frameworks/astro/)**
- **[Hono](frameworks/hono/)**
- **[Nuxt](frameworks/nuxt)**
- **[Remix](frameworks/remix/)**
- **[SvelteKit](frameworks/sveltekit/)**

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/).
<PlatformContent includePath="getting-started-prerequisites" />

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.
## Step 1: Install

## Features

In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/).

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
Choose the features you want to configure, and this guide will show you how:

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

## Install
<PlatformContent includePath="getting-started-features-expandable" />

### Install the Sentry SDK

Run the command for your preferred package manager to add the Sentry SDK to your application:

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

## Setup
## Step 2: Configure

The main Sentry configuration should happen as early as possible in your app's lifecycle.

### Wrangler Configuration

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

### Setup (Cloudflare Workers)
### Setup for Cloudflare Workers

<PlatformContent
includePath="getting-started-config"
platform="javascript.cloudflare.workers"
/>

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

```javascript
// hooks.server.js
Expand All @@ -64,12 +70,12 @@ export const handle = ({ event, resolve }) => {
};
```

### Setup (Cloudflare Pages)
</Expandable>

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

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

```javascript {filename:functions/_middleware.js}
import * as Sentry from "@sentry/cloudflare";
Expand All @@ -92,11 +98,11 @@ export const onRequest = [
// Enable logs to be sent to Sentry
enableLogs: true,
// ___PRODUCT_OPTION_END___ logs

// ___PRODUCT_OPTION_START___ 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
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
};
Expand All @@ -105,58 +111,135 @@ export const onRequest = [
];
```

### Cloudflare Durable Objects
## Step 3: Add Readable Stack Traces With Source Maps (Optional)

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.
The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry.

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

### Cloudflare Workflows
```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
{
"upload_source_maps": true,
}
```

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.
```toml {tabTitle:Toml} {filename:wrangler.toml}
upload_source_maps = true
```

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

## Add Readable Stack Traces to Errors
```bash
npx @sentry/wizard@latest -i sourcemaps
```

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

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

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

```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
{
"upload_source_maps": true,
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a code block beneath this paragraph?


#### Cloudflare Workers

Add the following code snippet to your main worker file to create a `/debug-sentry` route:

```javascript {filename: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("...");
},
};
```

#### Cloudflare Pages

Create a new route by adding the following code snippet to a file in your `functions` directory, such as `functions/debug-sentry.js`:

```javascript {filename:debug-sentry.js}
export async function onRequest(context) {
throw new Error("My first Sentry error!");
}
```

```toml {tabTitle:Toml} {filename:wrangler.toml}
upload_source_maps = true
<OnboardingOption optionId="performance">
### Tracing
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes to run your code.

#### Cloudflare Workers

```javascript {filename: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("...");
},
};
```

#### Cloudflare Pages

```javascript {filename: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!");
}
);
}
```

Then run the Sentry Wizard to finish your setup:
</OnboardingOption>

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

For more information on source maps or for more options to upload them, head over to our <PlatformLink to="/sourcemaps/">Source Maps</PlatformLink> documentation.
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).

## Verify
<PlatformContent includePath="getting-started-verify-locate-data" />

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

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

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

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

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

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

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.
</Expandable>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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.
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:

```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
{
Expand All @@ -16,13 +14,13 @@ compatibility_flags = ["nodejs_als"]
# compatibility_flags = ["nodejs_compat"]
```

You will also want to add the `CF_VERSION_METADATA` binding:
Additionally, add the `CF_VERSION_METADATA` binding in the same file:

```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
{
// ...
"version_metadata": {
"binding": "CF_VERSION_METADATA"
"binding": "CF_VERSION_METADATA",
},
}
```
Expand Down
Loading