Full Sentry SDK setup for Cloudflare Workers and Pages. Use when asked to "add Sentry to Cloudflare Workers", "install @sentry/cloudflare", or configure error monitoring, tracing, logging, crons, or AI monitoring for Cloudflare Workers, Pages, Durable Objects, Queues, Workflows, or Hono on Cloudflare.
Instrucciones de origen · Vista previa de solo lectura
name
sentry-cloudflare-sdk
description
Full Sentry SDK setup for Cloudflare Workers and Pages. Use when asked to "add Sentry to Cloudflare Workers", "install @sentry/cloudflare", or configure error monitoring, tracing, logging, crons, or AI monitoring for Cloudflare Workers, Pages, Durable Objects, Queues, Workflows, or Hono on Cloudflare.
Opinionated wizard that scans your Cloudflare project and guides you through complete Sentry setup for Workers, Pages, Durable Objects, Queues, Workflows, and Hono.
Invoke This Skill When
User asks to "add Sentry to Cloudflare Workers" or "set up Sentry" in a Cloudflare project
User wants to install or configure @sentry/cloudflare
User wants error monitoring, tracing, logging, crons, or AI monitoring for Cloudflare Workers or Pages
User asks about withSentry, sentryPagesPlugin, instrumentDurableObjectWithSentry, or instrumentD1WithSentry
User wants to monitor Durable Objects, Queues, Workflows, Scheduled handlers, or Email handlers on Cloudflare
⚡ Logging — structured logs via Sentry.logger.*; recommend when log search is needed
⚡ Crons — detect missed/failed scheduled jobs; recommend when cron triggers are configured
⚡ D1 Instrumentation — automatic query spans and breadcrumbs; recommend when D1 is bound
⚡ Durable Objects — automatic error capture and spans for DO methods; recommend when DOs are configured
⚡ Workflows — automatic span creation for workflow steps; recommend when Workflows are configured
⚡ AI Monitoring — Vercel AI SDK, OpenAI, Anthropic, LangChain; recommend when AI libraries detected
Recommendation logic:
Feature
Recommend when...
Error Monitoring
Always — non-negotiable baseline
Tracing
Always — HTTP request tracing and outbound fetch are high-value
Logging
App needs structured log search or log-to-trace correlation
Crons
Cron triggers configured in wrangler.toml
D1 Instrumentation
D1 database bindings present
Durable Objects
Durable Object bindings configured
Workflows
Workflow bindings configured
AI Monitoring
App uses Vercel AI SDK, OpenAI, Anthropic, or LangChain
Metrics
App needs custom counters, gauges, or distributions
Propose: "I recommend setting up Error Monitoring + Tracing. Want me to also add D1 instrumentation and Crons monitoring?"
Phase 3: Guide
Option 1: Source Maps Wizard
You need to run this yourself — the wizard opens a browser for login and requires interactive input that the agent can't handle. Copy-paste into your terminal:
npx @sentry/wizard@latest -i sourcemaps
This sets up source map uploading so your production stack traces show readable code. It does not set up the SDK initialization — you still need to follow Option 2 below for the actual SDK setup.
Once it finishes, continue with Option 2 for SDK setup.
Note: Unlike framework SDKs (Next.js, SvelteKit), there is no Cloudflare-specific wizard integration. The sourcemaps wizard only handles source map upload configuration.
Option 2: Manual Setup
Prerequisites: Compatibility Flags
The SDK requires AsyncLocalStorage. Add one of these flags to your Wrangler config:
Store your DSN as a Cloudflare secret — do not hardcode it:
# Local development: add to .dev.varsecho'SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"' >> .dev.vars
# Production: set as a secret
npx wrangler secret put SENTRY_DSN
Add the binding to your Env type:
interfaceEnv {
SENTRY_DSN: string;
// ... other bindings
}
Source Maps Setup
Source maps make production stack traces readable. Most Cloudflare projects build with Vite via Wrangler — wire the Sentry Vite plugin so maps upload on build:
SENTRY_AUTH_TOKEN is a build-time secret. For creating the token and wiring it into CI, see sentry-source-maps. The npx @sentry/wizard@latest -i sourcemaps shortcut noted above automates this setup.
Automatic Release Detection
The SDK can automatically detect the release version via Cloudflare's version metadata binding:
wrangler.toml:
[version_metadata]binding = "CF_VERSION_METADATA"
Release priority (highest to lowest):
release option passed to Sentry.init()
SENTRY_RELEASE environment variable
CF_VERSION_METADATA.id binding
For Each Agreed Feature
Load the corresponding reference file and follow its steps:
Structured logs via Sentry.logger.*, log-to-trace correlation
Crons
references/crons.md
Scheduled handler monitoring, withMonitor, check-in API
Durable Objects
references/durable-objects.md
Instrument Durable Object classes for error capture and spans
For each feature: read the reference file, follow its steps exactly, and verify before moving on.
Configuration Reference
Sentry.init() Options
Option
Type
Default
Notes
dsn
string
—
Required. Read from env.SENTRY_DSN automatically if not set
tracesSampleRate
number
—
0–1; 1.0 in dev, lower in prod recommended
tracesSampler
function
—
Dynamic sampling function; mutually exclusive with tracesSampleRate
dataCollection
object
conservative unless set
Controls what data the SDK captures (userInfo, httpBodies, etc.). When omitted, falls back to sendDefaultPii (default false); passing the object — even {} — enables permissive defaults. See Data Collection Reference
sendDefaultPii
boolean
false
Legacy. Prefer dataCollection for control over captured data
enableLogs
boolean
false
Enable Sentry Logs product
environment
string
auto
Read from env.SENTRY_ENVIRONMENT if not set
release
string
auto
Detected from CF_VERSION_METADATA.id or SENTRY_RELEASE
debug
boolean
false
Read from env.SENTRY_DEBUG if not set. Log SDK activity to console
tunnel
string
—
Read from env.SENTRY_TUNNEL if not set
beforeSend
function
—
Filter/modify error events before sending
beforeSendTransaction
function
—
Filter/modify transaction events before sending
beforeSendLog
function
—
Filter/modify log entries before sending
tracePropagationTargets
(string|RegExp)[]
all URLs
Control which outbound requests get trace headers
skipOpenTelemetrySetup
boolean
false
Opt-out of OpenTelemetry compatibility tracer
instrumentPrototypeMethods
boolean | string[]
false
Durable Object: instrument prototype methods for RPC spans
Data Collection Reference
dataCollection: {
// To disable sending user data and HTTP bodies, uncomment the lines below. For more info visit:// https://docs.sentry.io/platforms/javascript/configuration/options/#dataCollection// userInfo: false,// httpBodies: [],
},
Environment Variables (Read from env)
The SDK reads these from the Cloudflare env object automatically:
Variable
Purpose
SENTRY_DSN
DSN for Sentry init
SENTRY_RELEASE
Release version string
SENTRY_ENVIRONMENT
Environment name (production, staging)
SENTRY_TRACES_SAMPLE_RATE
Traces sample rate (parsed as float)
SENTRY_DEBUG
Enable debug mode ("true" / "1")
SENTRY_TUNNEL
Tunnel URL for event proxying
CF_VERSION_METADATA
Cloudflare version metadata binding (auto-detected release)
Default Integrations
These are registered automatically by getDefaultIntegrations():
Integration
Purpose
dedupeIntegration
Prevent duplicate events (disabled for Workflows)
inboundFiltersIntegration
Filter events by type, message, URL
functionToStringIntegration
Preserve original function names
linkedErrorsIntegration
Follow cause chains in errors
fetchIntegration
Trace outbound fetch() calls, create breadcrumbs
honoIntegration
Deprecated in v10.55.0 — use @sentry/hono package instead. Auto-capture Hono onError exceptions
requestDataIntegration
Attach request data to events
consoleIntegration
Capture console.* calls as breadcrumbs
Verification
After setup, verify Sentry is working:
// Add temporarily to your fetch handler, then removeexportdefaultSentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
{
asyncfetch(request, env, ctx) {
thrownewError("Sentry test error — delete me");
},
} satisfiesExportedHandler<Env>,
);
Deploy and trigger the route, then check your Sentry Issues dashboard — the error should appear within ~30 seconds.
Verification checklist:
Check
How
Errors captured
Throw in a fetch handler, verify in Sentry
Tracing working
Check Performance tab for HTTP spans
Source maps working
Check stack trace shows readable file/line names
D1 spans (if configured)
Run a D1 query, check for db.query spans
Scheduled monitoring (if configured)
Trigger a cron, check Crons dashboard
Phase 4: Cross-Link
After completing Cloudflare setup, check for companion services:
# Check for companion frontendls frontend/ web/ client/ ui/ 2>/dev/null
cat package.json 2>/dev/null | grep -E '"react"|"vue"|"svelte"|"next"|"astro"'# Check for companion backend in adjacent directoriesls ../backend ../server ../api 2>/dev/null
cat ../go.mod ../requirements.txt ../Gemfile 2>/dev/null | head -3
If a frontend is found, suggest the matching SDK skill:
Connecting frontend and backend with linked Sentry projects enables distributed tracing — stack traces that span your browser, Cloudflare Worker, and backend API in a single trace view.
Troubleshooting
Issue
Cause
Solution
Events not appearing
DSN not set or debug: false hiding errors
Set debug: true temporarily in init options; verify SENTRY_DSN secret is set with wrangler secret list
AsyncLocalStorage is not defined
Missing compatibility flag
Add nodejs_als or nodejs_compat to compatibility_flags in wrangler.toml
Stack traces show minified code
Source maps not uploaded
Configure @sentry/vite-plugin or run npx @sentry/wizard -i sourcemaps; verify SENTRY_AUTH_TOKEN in CI
Events lost on short-lived requests
SDK not flushing before worker terminates
Ensure withSentry or sentryPagesPlugin wraps your handler — they use ctx.waitUntil() to flush
Hono errors not captured
Hono app not instrumented
Use @sentry/hono/cloudflare — import sentry middleware and call app.use(sentry(app, options))
Durable Object errors missing
DO class not instrumented
Wrap class with Sentry.instrumentDurableObjectWithSentry() — see references/durable-objects.md
D1 queries not creating spans
Handler not wrapped with withSentry, or querying a non-env binding
D1 bindings on env are auto-instrumented by withSentry (v10.57.0+) — no manual wrapping needed (instrumentD1WithSentry is deprecated). All query methods (prepare, batch, exec, withSession) are traced in v10.61.0+
Scheduled handler not monitored
withSentry not wrapping the handler
Ensure export default Sentry.withSentry(...) wraps your entire exported handler object
Release not auto-detected
CF_VERSION_METADATA binding not configured
Add [version_metadata] with binding = "CF_VERSION_METADATA" to wrangler.toml
Duplicate events in Workflows
Dedupe integration filtering step failures
SDK automatically disables dedupe for Workflows; verify you use instrumentWorkflowWithSentry