Repository navigation
feat(node): Add nodeCronIntegration to monitor node-cron tasks #25473
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
5
commits into
develop
Choose a base branch
from
danf/node-cron-monitor-jobs
base: develop
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.
+511
−1
Open
Changes from all commits
Commits
Show all changes
5 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 ffb2c5d
fix(node): Instrument node-cron 4.0-4.2 and skip configs node-cron 3 …
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-cron/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.nodeCronIntegration({ excludeJobs: ['excluded-job'] })], | ||
| }); |
25 changes: 25 additions & 0 deletions
25
dev-packages/node-integration-tests/suites/cron/node-cron/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,25 @@ | ||
| import cron from 'node-cron'; | ||
|
|
||
| const excluded = cron.schedule('0 * * * * *', () => {}, { name: 'excluded-job' }); | ||
| const unnamed = cron.schedule('0 * * * * *', () => {}); | ||
|
|
||
| let runs = 0; | ||
| const task = cron.schedule( | ||
| '0 * * * * *', | ||
| () => { | ||
| runs++; | ||
| if (runs > 1) { | ||
| throw new Error('Error in cron job'); | ||
| } | ||
| }, | ||
| { name: 'my-cron-job', timezone: 'America/New_York' }, | ||
| ); | ||
|
|
||
| // Run the task without waiting for the next minute. | ||
| excluded | ||
| .execute() | ||
| .then(() => unnamed.execute()) | ||
| .then(() => task.execute()) | ||
| .then(() => task.execute()); | ||
|
|
||
| setTimeout(() => process.exit(), 10_000); |
50 changes: 50 additions & 0 deletions
50
dev-packages/node-integration-tests/suites/cron/node-cron/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,50 @@ | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; | ||
|
|
||
| const MONITOR_CONFIG = { | ||
| schedule: { type: 'crontab', value: '* * * * *' }, | ||
| timezone: 'America/New_York', | ||
| }; | ||
|
|
||
| describe('nodeCronIntegration', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('sends check-ins for named tasks', async () => { | ||
| await createRunner() | ||
| .ignore('event') | ||
| .expect({ | ||
| check_in: { monitor_slug: 'my-cron-job', status: 'in_progress', monitor_config: MONITOR_CONFIG }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'my-cron-job', status: 'ok', duration: expect.any(Number) } }) | ||
| .expect({ | ||
| check_in: { monitor_slug: 'my-cron-job', status: 'in_progress', monitor_config: MONITOR_CONFIG }, | ||
| }) | ||
| .expect({ check_in: { monitor_slug: 'my-cron-job', status: 'error', duration: expect.any(Number) } }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('captures job errors', async () => { | ||
| await createRunner() | ||
| .unordered() | ||
| .expect({ | ||
| event: { | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Error in cron job', | ||
| mechanism: { type: 'auto.function.node-cron', 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
| import { defineIntegration } from '@sentry/core'; | ||
| import { CHANNELS } from '../orchestrion/channels'; | ||
| import { nodeCronModuleNames } from '../orchestrion/config/node-cron'; | ||
| import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation'; | ||
| import type { AnyFn } from '../utils/cronMonitoring'; | ||
| import { | ||
| cronExpressionToMonitorConfig, | ||
| shouldMonitorJob, | ||
| subscribeToJobCreation, | ||
| wrapScheduledJob, | ||
| } from '../utils/cronMonitoring'; | ||
|
|
||
| const INTEGRATION_NAME = 'NodeCron' as const; | ||
|
|
||
| export interface NodeCronIntegrationOptions { | ||
| /** Task names not to monitor. Strings must match the name exactly. */ | ||
| excludeJobs?: Array<string | RegExp>; | ||
| } | ||
|
|
||
| let options: NodeCronIntegrationOptions = {}; | ||
|
|
||
| const _nodeCronIntegration = ((integrationOptions: NodeCronIntegrationOptions = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client) { | ||
| options = integrationOptions; | ||
| invokeOrchestrionInstrumentation(client, nodeCronModuleNames, subscribe, [], { | ||
| requiresTracingChannelBinding: false, | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Sends Crons check-ins for every named `node-cron` task, with its schedule as the monitor config. | ||
| * The monitor slug is the task's `name` option; tasks without a name are not monitored. | ||
| */ | ||
| export const nodeCronIntegration = defineIntegration(_nodeCronIntegration); | ||
|
|
||
| function subscribe(): void { | ||
| subscribeToJobCreation(CHANNELS.NODE_CRON_CREATE_TASK, INTEGRATION_NAME, args => wrapTask(args, false)); | ||
| subscribeToJobCreation(CHANNELS.NODE_CRON_V3_CREATE_TASK, INTEGRATION_NAME, args => wrapTask(args, true)); | ||
| } | ||
|
|
||
| // `createTask(expression, func, options)` | ||
| function wrapTask(args: unknown[], isNodeCron3: boolean): void { | ||
| const [expression, func, taskOptions] = args; | ||
| const { name, timezone } = (taskOptions || {}) as { name?: unknown; timezone?: unknown }; | ||
| if (!shouldMonitorJob(name, func, options.excludeJobs)) { | ||
| return; | ||
| } | ||
| const monitorConfig = | ||
| isNodeCron3 && !isReadAlikeByNodeCron3(String(expression)) | ||
| ? undefined | ||
| : cronExpressionToMonitorConfig(String(expression), timezone, 'and'); | ||
| args[1] = wrapScheduledJob(func as AnyFn, name, monitorConfig, 'auto.function.node-cron'); | ||
| } | ||
|
|
||
| const NODE_CRON_3_FIELD_MINIMUMS = [0, 0, 1, 1, 0]; | ||
|
|
||
| /** | ||
| * node-cron 3 keeps the values of a stepped field that are multiples of the step (`*\/2` days are 2, 4, ...) | ||
| * and reads `7` in a weekday range as `0`. Returns `false` where that differs from standard cron. | ||
| */ | ||
| export function isReadAlikeByNodeCron3(expression: string): boolean { | ||
| const fields = expression.trim().split(/\s+/); | ||
| const minimums = fields.length === 6 ? [0, ...NODE_CRON_3_FIELD_MINIMUMS] : NODE_CRON_3_FIELD_MINIMUMS; | ||
| const weekday = fields[fields.length - 1] || ''; | ||
| if (weekday.includes('-') && weekday.includes('7')) { | ||
| return false; | ||
| } | ||
| return fields.every((field, index) => { | ||
| if (!field.includes('/')) { | ||
| return true; | ||
| } | ||
| const match = /^(?:\*|(\d+)-\d+)\/(\d+)$/.exec(field); | ||
| return !!match && Number(match[1] ?? minimums[index]) % Number(match[2]) === 0; | ||
| }); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { InstrumentationConfig } from '../apmTypes'; | ||
| import { getModuleNames } from './module-names'; | ||
|
|
||
| const createTask = { functionName: 'createTask', kind: 'Sync' } as const; | ||
|
|
||
| // `schedule()` calls `createTask(expression, func, options)`, so wrapping `createTask` covers both. | ||
| export const nodeCronConfig = [ | ||
| { | ||
| channelName: 'createTask', | ||
| module: { name: 'node-cron', versionRange: '>=4.0.0 <5', filePath: 'dist/node-cron.cjs' }, | ||
| functionQuery: createTask, | ||
| }, | ||
| { | ||
| channelName: 'createTask', | ||
| module: { name: 'node-cron', versionRange: '>=4.0.0 <5', filePath: 'dist/node-cron.js' }, | ||
| functionQuery: createTask, | ||
| }, | ||
| { | ||
| channelName: 'createTask', | ||
| module: { name: 'node-cron', versionRange: '>=4.0.0 <4.3.0', filePath: 'dist/cjs/node-cron.js' }, | ||
| functionQuery: createTask, | ||
| }, | ||
| { | ||
| channelName: 'createTask', | ||
| module: { name: 'node-cron', versionRange: '>=4.0.0 <4.3.0', filePath: 'dist/esm/node-cron.js' }, | ||
| functionQuery: createTask, | ||
| }, | ||
| { | ||
| channelName: 'v3_createTask', | ||
| module: { name: 'node-cron', versionRange: '>=3.0.0 <4', filePath: 'src/node-cron.js' }, | ||
| functionQuery: createTask, | ||
| }, | ||
| ] satisfies InstrumentationConfig[]; | ||
|
|
||
| export const nodeCronModuleNames = getModuleNames(nodeCronConfig); | ||
|
|
||
| export const nodeCronChannels = { | ||
| NODE_CRON_CREATE_TASK: 'orchestrion:node-cron:createTask', | ||
| NODE_CRON_V3_CREATE_TASK: 'orchestrion:node-cron:v3_createTask', | ||
| } as const; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncommented double cast on monitored callback
Low Severity
This was flagged because the review rules call out new
as unknown asdouble-casts in SDK source. The mark onmonitoredCallbackuses that pattern with no comment on why a safer type is not possible.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit ffb2c5d. Configure here.