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
Open
wedamija
wants to merge
19
commits into
danf/node-cron-monitor-jobs
Choose a base branch
from
danf/node-schedule-monitor-jobs
base: danf/node-cron-monitor-jobs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
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 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 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 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 3a22e99
Merge commit '69f6913ab53b1bb9b7bc4724fa421e45ac1f7f9f' into danf/nod…
wedamija 78e2e7d
Merge danf/node-cron-monitor-jobs into danf/node-schedule-monitor-jobs
wedamija 07741fb
Merge remote-tracking branch 'origin/danf/node-cron-monitor-jobs' int…
wedamija afaaa48
ref(node): Pass nodeScheduleIntegration options to the subscriber
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 0913bc2
Merge remote-tracking branch 'origin/danf/node-cron-monitor-jobs' int…
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 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); | ||
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| import type { SerializedCheckIn } from '@sentry/core'; | ||
| 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', | ||
| }; | ||
|
|
||
| const expectNoConfig = (slug: string) => (checkIn: SerializedCheckIn) => { | ||
| expect(checkIn).toMatchObject({ monitor_slug: slug, status: 'in_progress' }); | ||
| expect(checkIn.monitor_config).toBeUndefined(); | ||
| }; | ||
|
|
||
| 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: expectNoConfig('rule-job') }) | ||
| .expect({ check_in: { monitor_slug: 'rule-job', status: 'ok' } }) | ||
| .expect({ check_in: expectNoConfig('ending-job') }) | ||
| .expect({ check_in: { monitor_slug: 'ending-job', status: 'ok' } }) | ||
| .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
108 changes: 108 additions & 0 deletions
108
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,108 @@ | ||
| 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, and one-off jobs, 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'); | ||
| } | ||
|
|
||
| /** Sends the spec's config after `job.schedule(spec)`, or none when the job also keeps runs from an earlier spec. */ | ||
| 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 are sent; recurrence rules and rules limited by `start` or `end` check in without a config. | ||
| 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.