Cloudflare Workers
This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.
@bugsee/cloudflare captures errors, logs and network calls inside your Worker, Durable Objects, and Cron, Queue, Email and Tail handlers, and uploads a report when something goes wrong.
Edge isolates run only Web APIs, so capture is incident-driven. Data is buffered in memory and uploaded only when an incident happens: a handled logException, a thrown handler error, or an unhandled rejection. A clean request uploads nothing.
Requirements
- A Cloudflare Worker (
workerd). - The
nodejs_compatcompatibility flag. The SDK importsnode:async_hooksfor per-request context, and without the flag your Worker fails to start. - An app token. Create a JavaScript application in Bugsee and choose Cloudflare Workers as its target; see Create your app. Store it as a Worker secret.
Install
- npm
- pnpm
- yarn
- bun
npm install @bugsee/cloudflare
pnpm add @bugsee/cloudflare
yarn add @bugsee/cloudflare
bun add @bugsee/cloudflare
Install @bugsee/cloudflare directly. It builds on @bugsee/vercel-edge, which you do not install separately.
Enable Node.js compatibility
compatibility_flags = ["nodejs_compat"]
If your project uses wrangler.jsonc, set "compatibility_flags": ["nodejs_compat"] there instead.
Wrap your handlers
Wrap everything your Worker exports with withBugsee. The first argument is a function that receives env, because Cloudflare exposes secrets only inside handlers. The client launches on the first invocation and is cached for the isolate's lifetime.
import { withBugsee } from '@bugsee/cloudflare';
export default withBugsee((env) => env.BUGSEE_APP_TOKEN, {
fetch: async (request, env, ctx) => new Response('ok'),
scheduled: async (controller, env, ctx) => {
/* Cron */
},
queue: async (batch, env, ctx) => {
/* Queue consumer */
},
email: async (message, env, ctx) => {
/* Email */
},
tail: async (events, env, ctx) => {
/* Tail Worker */
},
});
Add the secret with wrangler secret put BUGSEE_APP_TOKEN. For wrangler dev, put BUGSEE_APP_TOKEN=<your_app_token> in a .dev.vars file instead, because local development does not read the deployed secret.
Each handler runs in its own Bugsee context. Errors are captured and rethrown, and the upload is flushed through that handler's ctx.waitUntil, so it survives the isolate freezing after the response. Each non-fetch trigger is labeled (cron, queue, email, tail), so a report says which trigger fired. Email addresses and message bodies are never captured. For fetch, the report includes Cloudflare's coarse geo and network metadata from request.cf (colo, country, city, timezone, ASN, TLS version).
withBugsee also accepts a WorkerEntrypoint class. Pass { instrumentRpcMethods: true } to wrap its RPC methods too.
Durable Objects
A Durable Object is bound separately from the module handlers, so wrap its export with instrumentDurableObject.
import { DurableObject } from 'cloudflare:workers';
import { instrumentDurableObject } from '@bugsee/cloudflare';
class CounterBase extends DurableObject<Env> {
async fetch(request: Request) {
/* ... */
}
async alarm() {
/* ... */
}
}
export const Counter = instrumentDurableObject((env) => env.BUGSEE_APP_TOKEN, CounterBase, {
instrumentRpcMethods: true, // optional, also wraps RPC methods (default off)
});
Options
Instead of a token string, the callback passed to withBugsee or instrumentDurableObject may return an options object that includes appToken:
export default withBugsee(
(env) => ({ appToken: env.BUGSEE_APP_TOKEN, appVersion: '1.4.2' }),
{ fetch: async () => new Response('ok') },
);
The options are the same as for launch(appToken, options).
| Option | Type | Default | Description |
|---|---|---|---|
appVersion, appBuild, appId | string | resolved | Identify your release in reports. |
bindings | Record<string, unknown> | none | Your Worker's env object, read once at launch for the app version and build. See App version and build. |
captureLogs, captureNetwork, captureNetworkBodies | boolean | true | Capture streams. |
maxDataSize | number | 10 MB | Size of the in-memory buffer, divided across the partitions below. |
partitionCaptureByTenant | boolean | true on Cloudflare | Keep each Durable Object's capture in its own partition, so an incident uploads only the faulting tenant's data. Setting false turns that isolation off and logs a warning. |
maxTenantPartitions | number | 8 | Number of tenant partitions. The maxDataSize budget is divided across maxTenantPartitions + 1 buffers. Lower it if your Workers host few tenants. |
See Configuration for the shared options.
What is exported
@bugsee/cloudflare re-exports everything from @bugsee/vercel-edge and adds the following:
| Export | Description |
|---|---|
withBugsee(config, handlers) | Instruments a handler object or a WorkerEntrypoint class. |
instrumentDurableObject(config, Class, options?) | Instruments a Durable Object class. |
launch(appToken, options?) | Launches the client directly, with the platform identity set to Cloudflare. Returns the client synchronously. |
relaunch | Restarts the SDK. |
cfAttributes, cloudflareRequestAttributes | Read Cloudflare's request.cf metadata into report attributes. |
getStatus, BugseeStatus, LifecycleEvents, addReportAttachment, getDefaultNetworkSanitizer | The shared client exports. |
App version and build
Pass appVersion and appBuild in the options. The Vite and webpack plugins can also inject __BUGSEE_APP_VERSION__ and __BUGSEE_APP_BUILD__ at build time, and the BUGSEE_APP_VERSION and BUGSEE_APP_BUILD environment variables apply below those constants. Workers often have an empty process.env, so pass your Worker's env object as the bindings option, for example (env) => ({ appToken: env.BUGSEE_APP_TOKEN, bindings: env }), and Bugsee reads BUGSEE_APP_VERSION and BUGSEE_APP_BUILD from it. Below those, a CF_VERSION_METADATA binding supplies the version (its tag) and the build (its id) when nothing explicit answered. If none is found, reports show version 0.0.0 and build 0.
Source maps
Upload source maps for your Worker bundle so stack traces are readable. See Symbolication.
Verify
- Deploy the Worker with the secret set, or run
wrangler devwithBUGSEE_APP_TOKENin.dev.vars. - Trigger an error in a handler:
export default withBugsee((env) => env.BUGSEE_APP_TOKEN, {
fetch: async () => {
throw new Error('Bugsee test error');
},
});
- Open the Bugsee dashboard. The report appears with the request, logs and
request.cfmetadata.
If the Worker fails to start, check that nodejs_compat is set.
Related pages
- Vercel Edge — the edge build that this package wraps
- Configuration — all launch options
- Manual crash and error reporting —
logExceptionfor handled errors - Symbolication — source maps and debug IDs