Skip to main content

Cloudflare Workers

Beta

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_compat compatibility flag. The SDK imports node:async_hooks for 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 install @bugsee/cloudflare

Install @bugsee/cloudflare directly. It builds on @bugsee/vercel-edge, which you do not install separately.

Enable Node.js compatibility​

wrangler.toml
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.

src/index.ts
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.

src/counter.ts
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).

OptionTypeDefaultDescription
appVersion, appBuild, appIdstringresolvedIdentify your release in reports.
bindingsRecord<string, unknown>noneYour Worker's env object, read once at launch for the app version and build. See App version and build.
captureLogs, captureNetwork, captureNetworkBodiesbooleantrueCapture streams.
maxDataSizenumber10 MBSize of the in-memory buffer, divided across the partitions below.
partitionCaptureByTenantbooleantrue on CloudflareKeep 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.
maxTenantPartitionsnumber8Number 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:

ExportDescription
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.
relaunchRestarts the SDK.
cfAttributes, cloudflareRequestAttributesRead Cloudflare's request.cf metadata into report attributes.
getStatus, BugseeStatus, LifecycleEvents, addReportAttachment, getDefaultNetworkSanitizerThe 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​

  1. Deploy the Worker with the secret set, or run wrangler dev with BUGSEE_APP_TOKEN in .dev.vars.
  2. Trigger an error in a handler:
export default withBugsee((env) => env.BUGSEE_APP_TOKEN, {
fetch: async () => {
throw new Error('Bugsee test error');
},
});
  1. Open the Bugsee dashboard. The report appears with the request, logs and request.cf metadata.

If the Worker fails to start, check that nodejs_compat is set.

Found an issue, typo, or wrong statement on this page? Report it now →