|
10 | 10 | [](https://www.npmjs.com/package/@sentry/cloudflare) |
11 | 11 | [](https://www.npmjs.com/package/@sentry/cloudflare) |
12 | 12 |
|
13 | | -## Links |
| 13 | +The official Sentry SDK for monitoring Cloudflare applications. |
14 | 14 |
|
15 | | -- [Official SDK Docs](https://docs.sentry.io/quickstart/) |
| 15 | +## Documentation |
16 | 16 |
|
17 | | -## Install |
| 17 | +- [Getting started](https://docs.sentry.io/platforms/javascript/guides/cloudflare/) |
| 18 | +- [Configuration](https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/) |
| 19 | +- [Cloudflare-specific features](https://docs.sentry.io/platforms/javascript/guides/cloudflare/features/) |
18 | 20 |
|
19 | | -To get started, first install the `@sentry/cloudflare` package: |
| 21 | +## Support |
20 | 22 |
|
21 | | -```bash |
22 | | -npm install @sentry/cloudflare |
23 | | -``` |
24 | | - |
25 | | -Then set the `nodejs_compat` compatibility flag in your `wrangler.jsonc`/`wrangler.toml` config. This is because the SDK needs access to Node.js compatibility APIs to work correctly. |
26 | | - |
27 | | -```jsonc {tabTitle:JSON} {filename:wrangler.jsonc} |
28 | | -{ |
29 | | - "compatibility_flags": ["nodejs_compat"], |
30 | | -} |
31 | | -``` |
32 | | - |
33 | | -```toml {tabTitle:Toml} {filename:wrangler.toml} |
34 | | -compatibility_flags = ["nodejs_compat"] |
35 | | -``` |
36 | | - |
37 | | -## Setup (Cloudflare Pages) |
38 | | - |
39 | | -To use this SDK, add the `sentryPagesPlugin` as |
40 | | -[middleware to your Cloudflare Pages application](https://developers.cloudflare.com/pages/functions/middleware/). |
41 | | - |
42 | | -We recommend adding a `functions/_middleware.js` for the middleware setup so that Sentry is initialized for your entire |
43 | | -app. |
44 | | - |
45 | | -```javascript |
46 | | -// functions/_middleware.js |
47 | | -import * as Sentry from '@sentry/cloudflare'; |
48 | | - |
49 | | -export const onRequest = Sentry.sentryPagesPlugin({ |
50 | | - dsn: process.env.SENTRY_DSN, |
51 | | - // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing. |
52 | | - tracesSampleRate: 1.0, |
53 | | -}); |
54 | | -``` |
55 | | - |
56 | | -If you need to to chain multiple middlewares, you can do so by exporting an array of middlewares. Make sure the Sentry |
57 | | -middleware is the first one in the array. |
58 | | - |
59 | | -```javascript |
60 | | -import * as Sentry from '@sentry/cloudflare'; |
61 | | - |
62 | | -export const onRequest = [ |
63 | | - // Make sure Sentry is the first middleware |
64 | | - Sentry.sentryPagesPlugin({ |
65 | | - dsn: process.env.SENTRY_DSN, |
66 | | - tracesSampleRate: 1.0, |
67 | | - }), |
68 | | - // Add more middlewares here |
69 | | -]; |
70 | | -``` |
71 | | - |
72 | | -If you need to access the `context` object (for example to grab environmental variables), you can pass a function to |
73 | | -`sentryPagesPlugin` that takes the `context` object as an argument and returns `init` options: |
74 | | - |
75 | | -```javascript |
76 | | -export const onRequest = Sentry.sentryPagesPlugin(context => ({ |
77 | | - dsn: context.env.SENTRY_DSN, |
78 | | - tracesSampleRate: 1.0, |
79 | | -})); |
80 | | -``` |
81 | | - |
82 | | -If you do not have access to the `onRequest` middleware API, you can use the `wrapRequestHandler` API instead. |
83 | | - |
84 | | -Here is an example with SvelteKit: |
85 | | - |
86 | | -```javascript |
87 | | -// hooks.server.js |
88 | | -import * as Sentry from '@sentry/cloudflare'; |
89 | | - |
90 | | -export const handle = ({ event, resolve }) => { |
91 | | - const requestHandlerOptions = { |
92 | | - options: { |
93 | | - dsn: event.platform.env.SENTRY_DSN, |
94 | | - tracesSampleRate: 1.0, |
95 | | - }, |
96 | | - request: event.request, |
97 | | - context: event.platform.ctx, |
98 | | - }; |
99 | | - return Sentry.wrapRequestHandler(requestHandlerOptions, () => resolve(event)); |
100 | | -}; |
101 | | -``` |
102 | | - |
103 | | -## Setup (Cloudflare Workers) |
104 | | - |
105 | | -To use this SDK, wrap your handler with the `withSentry` function. This will initialize the SDK and hook into the |
106 | | -environment. Note that you can turn off almost all side effects using the respective options. |
107 | | - |
108 | | -Currently only ESM handlers are supported. |
109 | | - |
110 | | -```javascript |
111 | | -import * as Sentry from '@sentry/cloudflare'; |
112 | | - |
113 | | -export default withSentry( |
114 | | - env => ({ |
115 | | - dsn: env.SENTRY_DSN, |
116 | | - // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing. |
117 | | - tracesSampleRate: 1.0, |
118 | | - }), |
119 | | - { |
120 | | - async fetch(request, env, ctx) { |
121 | | - return new Response('Hello World!'); |
122 | | - }, |
123 | | - } satisfies ExportedHandler<Env>, |
124 | | -); |
125 | | -``` |
126 | | - |
127 | | -### Sourcemaps |
128 | | - |
129 | | -Configure uploading sourcemaps via the Sentry Wizard: |
130 | | - |
131 | | -```bash |
132 | | -npx @sentry/wizard@latest -i sourcemaps |
133 | | -``` |
134 | | - |
135 | | -See more details in our [docs](https://docs.sentry.io/platforms/javascript/sourcemaps/). |
136 | | - |
137 | | -## Usage |
138 | | - |
139 | | -To set context information or send manual events, use the exported functions of `@sentry/cloudflare`. Note that these |
140 | | -functions will require the usage of the Sentry helpers, either `withSentry` function for Cloudflare Workers or the |
141 | | -`sentryPagesPlugin` middleware for Cloudflare Pages. |
142 | | - |
143 | | -```javascript |
144 | | -import * as Sentry from '@sentry/cloudflare'; |
145 | | - |
146 | | -// Set user information, as well as tags and further extras |
147 | | -Sentry.setExtra('battery', 0.7); |
148 | | -Sentry.setTag('user_mode', 'admin'); |
149 | | -Sentry.setUser({ id: '4711' }); |
150 | | - |
151 | | -// Add a breadcrumb for future events |
152 | | -Sentry.addBreadcrumb({ |
153 | | - message: 'My Breadcrumb', |
154 | | - // ... |
155 | | -}); |
156 | | - |
157 | | -// Capture exceptions, messages or manual events |
158 | | -Sentry.captureMessage('Hello, world!'); |
159 | | -Sentry.captureException(new Error('Good bye')); |
160 | | -Sentry.captureEvent({ |
161 | | - message: 'Manual', |
162 | | - stacktrace: [ |
163 | | - // ... |
164 | | - ], |
165 | | -}); |
166 | | -``` |
167 | | - |
168 | | -## Cloudflare D1 Instrumentation |
169 | | - |
170 | | -`withSentry()` automatically instruments all [Cloudflare D1](https://developers.cloudflare.com/d1/) bindings on `env`, |
171 | | -Cloudflare's serverless SQL database. Just use the binding as usual: |
172 | | - |
173 | | -```javascript |
174 | | -// env.DB is the D1 DB binding configured in your `wrangler.toml` |
175 | | -await env.DB.prepare('SELECT * FROM table WHERE id = ?').bind(1).run(); |
176 | | -``` |
177 | | - |
178 | | -## Cron Monitoring (Cloudflare Workers) |
179 | | - |
180 | | -[Sentry Crons](https://docs.sentry.io/product/crons/) allows you to monitor the uptime and performance of any scheduled, |
181 | | -recurring job in your application. |
182 | | - |
183 | | -To instrument your cron triggers, use the `Sentry.withMonitor` API in your |
184 | | -[`Scheduled` handler](https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/). |
185 | | - |
186 | | -```js |
187 | | -export default { |
188 | | - async scheduled(event, env, ctx) { |
189 | | - ctx.waitUntil( |
190 | | - Sentry.withMonitor('your-cron-name', () => { |
191 | | - return doSomeTaskOnASchedule(); |
192 | | - }), |
193 | | - ); |
194 | | - }, |
195 | | -}; |
196 | | -``` |
197 | | - |
198 | | -You can also use supply a monitor config to upsert cron monitors with additional metadata: |
199 | | - |
200 | | -```js |
201 | | -const monitorConfig = { |
202 | | - schedule: { |
203 | | - type: 'crontab', |
204 | | - value: '* * * * *', |
205 | | - }, |
206 | | - checkinMargin: 2, // In minutes. Optional. |
207 | | - maxRuntime: 10, // In minutes. Optional. |
208 | | - timezone: 'America/Los_Angeles', // Optional. |
209 | | -}; |
210 | | - |
211 | | -export default { |
212 | | - async scheduled(event, env, ctx) { |
213 | | - Sentry.withMonitor('your-cron-name', () => doSomeTaskOnASchedule(), monitorConfig); |
214 | | - }, |
215 | | -}; |
216 | | -``` |
| 23 | +- [Report a bug](https://github.com/getsentry/sentry-javascript/issues/new/choose) |
| 24 | +- [Contributing](https://github.com/getsentry/sentry-javascript/blob/develop/CONTRIBUTING.md) |
0 commit comments