Skip to content

feat(cloudflare): Add opt-in cron monitoring for Cron Triggers - #25014

Open
wedamija wants to merge 7 commits into
developfrom
danf/cloudflare-cron-monitors
Open

wedamija wants to merge 7 commits into
developfrom
danf/cloudflare-cron-monitors

Conversation

@wedamija

@wedamija wedamija commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Cron Triggers already run through the instrumented scheduled handler, and controller.cron holds the schedule, so the SDK can send check-ins that create the monitor on the first run. Monitors are billed, so this is an opt-in integration:

const jobs = { '30 9 * * MON-FRI': { slug: 'daily-report', run: dailyReport } };

export default Sentry.withSentry(
  env => ({ dsn: env.SENTRY_DSN, integrations: [Sentry.cronTriggersIntegration({ slug: cron => jobs[cron]?.slug })] }),
  { async scheduled(controller, env) { await jobs[controller.cron]?.run(env); } },
);
  • Tree-shaken when not imported: +101 B on @sentry/cloudflare (withSentry) - minified.
  • Cloudflare numbers weekdays from 1 = Sunday, so the weekday field is sent as names (1-5 → SUN-THU). Fields Sentry would read differently or reject are sent without a schedule.
  • Without slug, the slug comes from the expression (cron-30-9-x-x-montofri). slug can also return monitor settings, or undefined to skip a trigger.
  • Check-ins use captureCheckIn, not withMonitor, which forks the isolation scope and loses the invocation's waitUntil.

Docs: getsentry/sentry-docs#19783

Add a `monitorCronTriggers` option. When set, each Cron Trigger run of the
`scheduled` handler sends in-progress and ok/error check-ins whose monitor
config carries the trigger's cron expression, so Sentry creates the monitor
on the first run. The slug is derived from the cron expression, or picked by
a user function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.52 kB - -
@sentry/browser - with treeshaking flags 27.68 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.57 kB - -
@sentry/browser (incl. Tracing) 51.45 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.46 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.46 kB - -
@sentry/browser (incl. Tracing, Replay) 91.04 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.03 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 95.74 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 108.71 kB - -
@sentry/browser (incl. Feedback) 47.04 kB - -
@sentry/browser (incl. sendFeedback) 34.57 kB - -
@sentry/browser (incl. FeedbackAsync) 39.68 kB - -
@sentry/browser (incl. Metrics) 30.54 kB - -
@sentry/browser (incl. Logs) 30.82 kB - -
@sentry/browser (incl. Metrics & Logs) 31.48 kB - -
@sentry/react 31.36 kB - -
@sentry/react (incl. Tracing) 53.8 kB - -
@sentry/vue 37.51 kB - -
@sentry/vue (incl. Tracing) 54.33 kB - -
@sentry/svelte 29.55 kB - -
@sentry/remix (Remix 3 client bundle) 55.78 kB - -
CDN Bundle 31.22 kB - -
CDN Bundle (incl. Tracing) 51.98 kB - -
CDN Bundle (incl. Logs, Metrics) 33.46 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 53.92 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.21 kB - -
CDN Bundle (incl. Tracing, Replay) 89.56 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.53 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 95.72 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 97.71 kB - -
CDN Bundle - uncompressed 92.14 kB - -
CDN Bundle (incl. Tracing) - uncompressed 154.49 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 98.71 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 160.45 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.28 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 274.22 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 280.16 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 287.93 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 293.86 kB - -
@sentry/nextjs (client) 56.32 kB - -
@sentry/sveltekit (client) 51.87 kB - -
@sentry/core/server 40.59 kB - -
@sentry/core/browser 13.63 kB - -
@sentry/node 144.77 kB +0.01% +14 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.22 kB - -
@sentry/node - without tracing 93.32 kB +0.02% +10 B 🔺
@sentry/node - without channel injection 122.97 kB +0.01% +5 B 🔺
@sentry/aws-serverless 101.59 kB +0.01% +6 B 🔺
@sentry/cloudflare (withSentry) - minified 208.88 kB +0.13% +267 B 🔺
@sentry/cloudflare (withSentry) 518.01 kB +0.12% +609 B 🔺

View base workflow run

wedamija and others added 4 commits October 2, 2026 13:27
Cloudflare numbers weekdays from 1 = Sunday, while Sentry uses 0 = Sunday,
so the weekday field is now converted to names (or a 0-based number for
`nL`) before it is sent. Expressions that can't be converted are sent
without a schedule.

Derived slugs now write `,`, `-` and `/` as distinct tokens and append a
hash when the expression has other characters or the slug is too long. A
throwing `monitorCronTriggers` function and runs without a cron expression
send no check-ins instead of affecting the handler, and the function can
return other monitor settings along with the slug.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tion

Replaces the `monitorCronTriggers` option with an opt-in
`cronTriggersIntegration({ slug })`, so the schedule conversion and slug
code is tree-shaken for Workers that don't use it. The scheduled handler
only looks the integration up by name. Also drops `nL` and `n#k` weekday
conversion (sent without a schedule) and simplifies the slug hash.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
*/n gives the same days in both numberings and keeps Sentry's AND rule for the
day fields. Sentry rejects W and ? in the day of month, so send no schedule.

Co-Authored-By: Claude <noreply@anthropic.com>
@wedamija
wedamija marked this pull request as ready for review October 2, 2026 23:21
@wedamija
wedamija requested a review from a team as a code owner October 2, 2026 23:21
@wedamija
wedamija requested review from a team, andreiborza and isaacs and removed request for a team October 2, 2026 23:21

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit eb22bd1. Configure here.

Comment thread packages/cloudflare/src/instrumentations/worker/instrumentScheduled.ts Outdated
Comment on lines +134 to +137
// Check-ins are captured directly rather than through `withMonitor`, which would fork the
// isolation scope and lose the invocation state attached to it.
const checkInId = captureCheckIn(
{ monitorSlug, status: 'in_progress' },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When a cron expression is unconvertible, all monitor settings like checkinMargin are silently discarded, not just the schedule, because the argument to captureCheckIn becomes undefined.
Severity: MEDIUM

Suggested Fix

Update the ternary operator to ensure monitorSettings are passed to captureCheckIn even when crontab is undefined. The expression should return the monitorSettings object if it has keys, otherwise undefined.

crontab 
  ? { ...monitorSettings, schedule: { type: 'crontab', value: crontab } } 
  : Object.keys(monitorSettings).length > 0 ? monitorSettings : undefined
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/cloudflare/src/integrations/cronTriggers.ts#L134-L137

Potential issue: When a cron expression cannot be converted to a standard crontab
format, any user-provided monitor settings (e.g., `checkinMargin`, `maxRuntime`) are
silently discarded. This is caused by a ternary operator that resolves to `undefined`
when the `crontab` variable is `undefined`, causing the entire monitor configuration
object to be dropped from the `captureCheckIn` call. As a result, check-ins are sent
without any of the intended settings, which can lead to incorrect alert behavior, such
as missed alerts for check-in margins.

Did we get this right? 👍 / 👎 to inform future reviews.

wedamija and others added 2 commits October 2, 2026 17:41
…dler

Start the check-in before the handler's try block and guard the final
check-in, so a failing check-in never skips the job, fails a successful
run, or is captured as the handler's exception.

Co-Authored-By: Claude <noreply@anthropic.com>
On Cloudflare 1 = Sunday, so `1-5` runs Sunday to Thursday. Day names
are unambiguous.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant