You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sentry.metrics.* (count, gauge, distribution) is exported and enabled by default in @sentry/cloudflare, but capturing a metric triggers a setTimeout(..., 5000) that fires outside the request context, causing a Cloudflare Workers runtime error.
This is particularly visible when bundling with @cloudflare/vite-plugin (which applies stricter runtime checks than older Wrangler builds).
Root Cause
The core client's setupWeightBasedFlushing (packages/core/src/client.ts:105-158) registers an afterCaptureMetric hook that schedules a 5-second setTimeout (DEFAULT_FLUSH_INTERVAL = 5000) to batch-flush the metric envelope. In Cloudflare Workers, this timer fires after the request handler returns, violating the request-scoped execution model.
The CloudflareClient.flush() method handles pending spans but does not address the metric flush timer created by the base client.
Current Workarounds
Disable standalone metrics and use span attributes/span metrics instead:
Sentry.init({enableMetrics: false});
Call Sentry.flush() at the end of every request — this triggers the flushMetrics hook which clears the timer, but the timer still gets created before being cancelled.
Possible Fixes
Make DEFAULT_FLUSH_INTERVAL configurable (e.g. metricsFlushInterval option), allowing @sentry/cloudflare to set it to 0 for synchronous flushing.
Have @sentry/cloudflare override the flushing strategy to flush synchronously within the request context rather than relying on background timers.
At minimum, document that Sentry.metrics.* is not fully supported in Workers and recommend span metrics.
Context
Related to [Metrics] Support cloudflare workers #11220 (closed in 2024 as "won't do" when the old metrics.increment()/metrics.set() API was replaced with span-attribute-based metrics). However, Application Metrics (Sentry.metrics.count/gauge/distribution) is a newer product that shipped after that decision, so the closure rationale no longer fully applies.
Problem
Sentry.metrics.*(count,gauge,distribution) is exported and enabled by default in@sentry/cloudflare, but capturing a metric triggers asetTimeout(..., 5000)that fires outside the request context, causing a Cloudflare Workers runtime error.This is particularly visible when bundling with
@cloudflare/vite-plugin(which applies stricter runtime checks than older Wrangler builds).Root Cause
The core client's
setupWeightBasedFlushing(packages/core/src/client.ts:105-158) registers anafterCaptureMetrichook that schedules a 5-secondsetTimeout(DEFAULT_FLUSH_INTERVAL = 5000) to batch-flush the metric envelope. In Cloudflare Workers, this timer fires after the request handler returns, violating the request-scoped execution model.The
CloudflareClient.flush()method handles pending spans but does not address the metric flush timer created by the base client.Current Workarounds
Sentry.flush()at the end of every request — this triggers theflushMetricshook which clears the timer, but the timer still gets created before being cancelled.Possible Fixes
DEFAULT_FLUSH_INTERVALconfigurable (e.g.metricsFlushIntervaloption), allowing@sentry/cloudflareto set it to0for synchronous flushing.@sentry/cloudflareoverride the flushing strategy to flush synchronously within the request context rather than relying on background timers.Sentry.metrics.*is not fully supported in Workers and recommend span metrics.Context
metrics.increment()/metrics.set()API was replaced with span-attribute-based metrics). However, Application Metrics (Sentry.metrics.count/gauge/distribution) is a newer product that shipped after that decision, so the closure rationale no longer fully applies.Package
@sentry/cloudflare