Repository navigation
feat(node): Add nodeScheduleIntegration to monitor node-schedule jobs #25474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
db27775
feat(node): Add nodeCronIntegration to monitor node-cron tasks
wedamija 0cb322d
ref(node): Move cron schedule conversion to a shared helper
wedamija d9e3b62
test(node): Assert node-cron check-ins and errors separately
wedamija 905ed6b
test(node): Skip node-cron integration suite on bun run and allow opt…
wedamija 1aa125d
feat(node): Add nodeScheduleIntegration to monitor node-schedule jobs
wedamija 7f4f833
test(node): Assert node-schedule check-ins and errors separately
wedamija 231f7bc
test(node): Skip node-schedule integration suite on bun run
wedamija ffb2c5d
fix(node): Instrument node-cron 4.0-4.2 and skip configs node-cron 3 …
wedamija 676b738
Merge branch 'danf/node-cron-monitor-jobs' into danf/node-schedule-mo…
wedamija 4f360eb
fix(node): Send no monitor config for node-schedule rules with a star…
wedamija 3d72a0b
ref(node): Mark monitored node-cron callbacks without a double cast
wedamija e7e6339
Merge branch 'danf/node-cron-monitor-jobs' into danf/node-schedule-mo…
wedamija 8184b78
ref(node): Mark monitored node-schedule callbacks without a double cast
wedamija 9df41c3
Merge branch 'develop' into danf/node-cron-monitor-jobs
wedamija 59979d9
Merge branch 'danf/node-cron-monitor-jobs' into danf/node-schedule-mo…
wedamija 6f712c7
fix(node): Don't monitor one-off node-schedule jobs from timestamps o…
wedamija 69f6913
Merge remote-tracking branch 'origin/develop' into HEAD
wedamija 3a22e99
Merge commit '69f6913ab53b1bb9b7bc4724fa421e45ac1f7f9f' into danf/nod…
wedamija 5c38418
Merge develop into danf/node-cron-monitor-jobs
wedamija 78e2e7d
Merge danf/node-cron-monitor-jobs into danf/node-schedule-monitor-jobs
wedamija ea0509e
ref(node): Pass nodeCronIntegration options to the subscriber
wedamija 07741fb
Merge remote-tracking branch 'origin/danf/node-cron-monitor-jobs' int…
wedamija afaaa48
ref(node): Pass nodeScheduleIntegration options to the subscriber
wedamija 4649550
ref(node): Address node-cron review comments
wedamija 7a2b83f
Merge remote-tracking branch 'origin/danf/node-cron-monitor-jobs' int…
wedamija 82b2bff
ref(node): Export nodeScheduleIntegration from Deno
wedamija 0af113c
fix(node): Send the new schedule after a node-schedule job is resched…
wedamija 7826628
fix(node): Start a new trace per cron job run and send Sunday range e…
wedamija 0913bc2
Merge remote-tracking branch 'origin/danf/node-cron-monitor-jobs' int…
wedamija c87c128
fix(node): Send no monitor config for weekday ranges ending on Sunday…
wedamija 3a13940
Merge branch 'danf/node-cron-monitor-jobs-fix' into danf/node-schedul…
wedamija 754fd55
feat(node): Monitor node-schedule jobs started with new Job().schedule()
wedamija 5efee25
fix(node): Skip jobs with unsupported schedules when monitoring all jobs
wedamija 0df863e
Merge branch 'danf/node-cron-monitor-jobs' into danf/node-schedule-mo…
wedamija c03a72c
fix(node): Skip jobs with unsupported schedules when monitoring all jobs
wedamija 01f6696
fix(node): Capture errors from jobs that send no check-ins
wedamija 774829f
Merge branch 'danf/node-cron-monitor-jobs' into danf/node-schedule-mo…
wedamija d21b712
Merge remote-tracking branch 'origin/develop' into danf/node-schedule…
wedamija eac0c09
Merge remote-tracking branch 'origin/develop' into danf/node-schedule…
wedamija File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/cron/node-schedule-integration/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| transport: loggingTransport, | ||
| integrations: [Sentry.nodeScheduleIntegration({ excludeJobs: ['excluded-job'] })], | ||
| }); |
35 changes: 35 additions & 0 deletions
35
dev-packages/node-integration-tests/suites/cron/node-schedule-integration/scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import schedule from 'node-schedule'; | ||
|
|
||
| const excluded = schedule.scheduleJob('excluded-job', '0 0 * * *', () => {}); | ||
| const unnamed = schedule.scheduleJob('0 0 * * *', () => {}); | ||
| const oneOff = schedule.scheduleJob('one-off-job', Date.now() + 86_400_000, () => {}); | ||
| const recurrenceRule = schedule.scheduleJob('rule-job', { hour: 3, minute: 0 }, () => {}); | ||
| const ending = schedule.scheduleJob('ending-job', { rule: '0 0 * * *', end: Date.now() + 86_400_000 }, () => {}); | ||
| const newJob = new schedule.Job('new-job', () => {}); | ||
| newJob.schedule({ rule: '0 0 * * *', tz: 'UTC' }); | ||
|
|
||
| let runs = 0; | ||
| const job = schedule.scheduleJob('my-schedule-job', { rule: '30 9 * * 1-5', tz: 'Europe/Vienna' }, () => { | ||
| runs++; | ||
| if (runs === 2) { | ||
| throw new Error('Error in scheduled job'); | ||
| } | ||
| }); | ||
|
|
||
| // Run the jobs without waiting for their schedule. | ||
| excluded.invoke(); | ||
| unnamed.invoke(); | ||
| oneOff.invoke(); | ||
| recurrenceRule.invoke(); | ||
| ending.invoke(); | ||
| newJob.invoke(); | ||
| job.invoke(); | ||
| try { | ||
| job.invoke(); | ||
| } catch { | ||
| // node-schedule catches errors when it runs the job itself | ||
| } | ||
| schedule.rescheduleJob(job, { rule: '0 8 * * *', tz: 'UTC' }); | ||
| job.invoke(); | ||
|
|
||
| setTimeout(() => process.exit(), 10_000); | ||
66 changes: 66 additions & 0 deletions
66
dev-packages/node-integration-tests/suites/cron/node-schedule-integration/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| const MONITOR_CONFIG = { | ||
| schedule: { type: 'crontab', value: '30 9 * * 1-5' }, | ||
| timezone: 'Europe/Vienna', | ||
| }; | ||
|
|
||
| describe('nodeScheduleIntegration', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('sends check-ins for named jobs', async () => { | ||
| await createRunner() | ||
| .ignore('event') | ||
| .expect({ | ||
| check_in: { | ||
| monitor_slug: 'new-job', | ||
| status: 'in_progress', | ||
| monitor_config: { schedule: { type: 'crontab', value: '0 0 * * *' }, timezone: 'UTC' }, | ||
| }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'new-job', status: 'ok' } }) | ||
| .expect({ | ||
| check_in: { monitor_slug: 'my-schedule-job', status: 'in_progress', monitor_config: MONITOR_CONFIG }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'my-schedule-job', status: 'ok', duration: expect.any(Number) } }) | ||
| .expect({ | ||
| check_in: { monitor_slug: 'my-schedule-job', status: 'in_progress', monitor_config: MONITOR_CONFIG }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'my-schedule-job', status: 'error', duration: expect.any(Number) } }) | ||
| .expect({ | ||
| check_in: { | ||
| monitor_slug: 'my-schedule-job', | ||
| status: 'in_progress', | ||
| monitor_config: { schedule: { type: 'crontab', value: '0 8 * * *' }, timezone: 'UTC' }, | ||
| }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'my-schedule-job', status: 'ok', duration: expect.any(Number) } }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('captures job errors', async () => { | ||
| await createRunner() | ||
| .unordered() | ||
| .expect({ | ||
| event: { | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Error in scheduled job', | ||
| mechanism: { type: 'auto.function.node-schedule', handled: false }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/server-utils/src/integrations/node-schedule.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import type { IntegrationFn, MonitorConfig } from '@sentry/core'; | ||
| import { defineIntegration } from '@sentry/core'; | ||
| import { CHANNELS } from '../orchestrion/channels'; | ||
| import { nodeScheduleModuleNames } from '../orchestrion/config/node-schedule'; | ||
| import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation'; | ||
| import type { AnyFn, ScheduleChange } from '../utils/cronMonitoring'; | ||
| import { | ||
| cronExpressionToMonitorConfig, | ||
| shouldMonitorJob, | ||
| subscribeToScheduleChange, | ||
| updateMonitorConfig, | ||
| wrapScheduledJob, | ||
| } from '../utils/cronMonitoring'; | ||
|
|
||
| const INTEGRATION_NAME = 'NodeSchedule' as const; | ||
|
|
||
| export interface NodeScheduleIntegrationOptions { | ||
| /** Job names not to monitor. Strings must match the name exactly. */ | ||
| excludeJobs?: Array<string | RegExp>; | ||
| } | ||
|
|
||
| const _nodeScheduleIntegration = ((options: NodeScheduleIntegrationOptions = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client) { | ||
| invokeOrchestrionInstrumentation(client, nodeScheduleModuleNames, subscribe, [options], { | ||
| requiresTracingChannelBinding: false, | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Sends Crons check-ins for every named `node-schedule` job, whether it's created with `scheduleJob` or `new Job()`. | ||
| * The monitor slug is the job name. Jobs without a name, one-off jobs, and jobs with a schedule Crons can't express | ||
| * are not monitored. | ||
| */ | ||
| export const nodeScheduleIntegration = defineIntegration(_nodeScheduleIntegration); | ||
|
|
||
| function subscribe(options: NodeScheduleIntegrationOptions): void { | ||
| subscribeToScheduleChange(CHANNELS.NODE_SCHEDULE_SCHEDULE, INTEGRATION_NAME, onSchedule, change => | ||
| beforeSchedule(change, options), | ||
| ); | ||
| } | ||
|
|
||
| interface Job { | ||
| name?: unknown; | ||
| job?: unknown; | ||
| nextInvocation?: () => unknown; | ||
| } | ||
|
|
||
| // The name node-schedule gives jobs created without one. | ||
| const ANONYMOUS_NAME = /^<Anonymous Job \d+[ >]/; | ||
|
|
||
| // `job.schedule(spec)` calls that add to runs the job already has pending, rather than replace them. | ||
| const addsToPendingRuns = new WeakSet<ScheduleChange>(); | ||
|
|
||
| /** Wraps a named job's method before `job.schedule(spec)` with a recurring spec, unless it's already wrapped. */ | ||
| export function beforeSchedule(change: ScheduleChange, options: NodeScheduleIntegrationOptions): void { | ||
| const job = change.self as Job | undefined; | ||
| if (!job) { | ||
| return; | ||
| } | ||
| if (typeof job.nextInvocation === 'function' && job.nextInvocation() != null) { | ||
| addsToPendingRuns.add(change); | ||
| } | ||
| const name = typeof job.name === 'string' && !ANONYMOUS_NAME.test(job.name) ? job.name : undefined; | ||
| if (isOneOffSpec(change.arguments[0]) || !shouldMonitorJob(name, job.job, options.excludeJobs)) { | ||
| return; | ||
| } | ||
| job.job = wrapScheduledJob(job.job as AnyFn, name, undefined, 'auto.function.node-schedule'); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the spec's config after `job.schedule(spec)`. Without one, from a spec Crons can't express or a job that also | ||
| * keeps runs from an earlier spec, the job doesn't check in. | ||
| */ | ||
| export function onSchedule(change: ScheduleChange): void { | ||
| // `schedule` returns false when it scheduled nothing, and `reschedule` then keeps the old schedule. | ||
| if (change.result === true) { | ||
| const job = change.self as Job | undefined; | ||
| updateMonitorConfig(job?.job, addsToPendingRuns.has(change) ? undefined : getMonitorConfig(change.arguments[0])); | ||
| } | ||
| } | ||
|
|
||
| /** Whether node-schedule runs a job with this spec once: a `Date`, a timestamp, or a date string. */ | ||
| export function isOneOffSpec(spec: unknown): boolean { | ||
| const { rule } = (spec || {}) as { rule?: unknown }; | ||
| const when = typeof spec === 'object' && rule ? rule : spec; | ||
| if (typeof when === 'string') { | ||
| // `Date.parse` also accepts some crons, like `0 9 * * *`. | ||
| return !isCronShaped(when) && !Number.isNaN(Date.parse(when)); | ||
| } | ||
| return when instanceof Date || typeof when === 'number'; | ||
| } | ||
|
|
||
| function isCronShaped(value: string): boolean { | ||
| const fields = value.trim().split(/\s+/); | ||
| return ( | ||
| /^@[a-z]+$/i.test(value.trim()) || | ||
| ((fields.length === 5 || fields.length === 6) && fields.every(field => /^[\d*,/?#a-z-]+$/i.test(field))) | ||
| ); | ||
| } | ||
|
|
||
| // Only cron strings convert; recurrence rules and rules limited by `start` or `end` don't. | ||
| export function getMonitorConfig(spec: unknown): MonitorConfig | undefined { | ||
| if (typeof spec === 'string') { | ||
| return cronExpressionToMonitorConfig(spec, undefined, 'or'); | ||
| } | ||
| const { rule, tz, start, end } = (spec || {}) as { rule?: unknown; tz?: unknown; start?: unknown; end?: unknown }; | ||
| return typeof rule === 'string' && !start && !end ? cronExpressionToMonitorConfig(rule, tz, 'or') : undefined; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.