Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node): Fix cron instrumentation and add tests (#11811)
Closes #11766 These tests are also in TypeScript so they check the types too. I found that two out of three cron libraries were actually swallowing exceptions so that they were not captured by Sentry. I added calls to `captureException` to rectify that.
- Loading branch information
Showing
13 changed files
with
444 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
dev-packages/node-integration-tests/suites/cron/cron/scenario.ts
This file contains 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,31 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
import { CronJob } from 'cron'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
autoSessionTracking: false, | ||
transport: loggingTransport, | ||
}); | ||
|
||
const CronJobWithCheckIn = Sentry.cron.instrumentCron(CronJob, 'my-cron-job'); | ||
|
||
let closeNext = false; | ||
|
||
const cron = new CronJobWithCheckIn('* * * * * *', () => { | ||
if (closeNext) { | ||
cron.stop(); | ||
throw new Error('Error in cron job'); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('You will see this message every second'); | ||
closeNext = true; | ||
}); | ||
|
||
cron.start(); | ||
|
||
setTimeout(() => { | ||
process.exit(); | ||
}, 5000); |
75 changes: 75 additions & 0 deletions
75
dev-packages/node-integration-tests/suites/cron/cron/test.ts
This file contains 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,75 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('cron instrumentation', done => { | ||
createRunner(__dirname, 'scenario.ts') | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'in_progress', | ||
release: '1.0', | ||
monitor_config: { schedule: { type: 'crontab', value: '* * * * * *' } }, | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'ok', | ||
release: '1.0', | ||
duration: expect.any(Number), | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'in_progress', | ||
release: '1.0', | ||
monitor_config: { schedule: { type: 'crontab', value: '* * * * * *' } }, | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'error', | ||
release: '1.0', | ||
duration: expect.any(Number), | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
event: { | ||
exception: { values: [{ type: 'Error', value: 'Error in cron job' }] }, | ||
}, | ||
}) | ||
.start(done); | ||
}); |
38 changes: 33 additions & 5 deletions
38
dev-packages/node-integration-tests/suites/cron/node-cron/scenario.ts
This file contains 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 |
---|---|---|
@@ -1,9 +1,37 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
import { CronJob } from 'cron'; | ||
import * as cron from 'node-cron'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const CronJobWithCheckIn = Sentry.cron.instrumentCron(CronJob, 'my-cron-job'); | ||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
autoSessionTracking: false, | ||
transport: loggingTransport, | ||
}); | ||
|
||
const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron); | ||
|
||
let closeNext = false; | ||
|
||
const task = cronWithCheckIn.schedule( | ||
'* * * * * *', | ||
() => { | ||
if (closeNext) { | ||
// https://github.com/node-cron/node-cron/issues/317 | ||
setImmediate(() => { | ||
task.stop(); | ||
}); | ||
|
||
throw new Error('Error in cron job'); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('You will see this message every second'); | ||
closeNext = true; | ||
}, | ||
{ name: 'my-cron-job' }, | ||
); | ||
|
||
setTimeout(() => { | ||
process.exit(0); | ||
}, 1_000); | ||
process.exit(); | ||
}, 5000); |
This file contains 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
29 changes: 29 additions & 0 deletions
29
dev-packages/node-integration-tests/suites/cron/node-schedule/scenario.ts
This file contains 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,29 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
import * as schedule from 'node-schedule'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
autoSessionTracking: false, | ||
transport: loggingTransport, | ||
}); | ||
|
||
const scheduleWithCheckIn = Sentry.cron.instrumentNodeSchedule(schedule); | ||
|
||
let closeNext = false; | ||
|
||
const job = scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * * * *', () => { | ||
if (closeNext) { | ||
job.cancel(); | ||
throw new Error('Error in cron job'); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('You will see this message every second'); | ||
closeNext = true; | ||
}); | ||
|
||
setTimeout(() => { | ||
process.exit(); | ||
}, 5000); |
75 changes: 75 additions & 0 deletions
75
dev-packages/node-integration-tests/suites/cron/node-schedule/test.ts
This file contains 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,75 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('node-schedule instrumentation', done => { | ||
createRunner(__dirname, 'scenario.ts') | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'in_progress', | ||
release: '1.0', | ||
monitor_config: { schedule: { type: 'crontab', value: '* * * * * *' } }, | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'ok', | ||
release: '1.0', | ||
duration: expect.any(Number), | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'in_progress', | ||
release: '1.0', | ||
monitor_config: { schedule: { type: 'crontab', value: '* * * * * *' } }, | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
check_in: { | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'my-cron-job', | ||
status: 'error', | ||
release: '1.0', | ||
duration: expect.any(Number), | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
event: { | ||
exception: { values: [{ type: 'Error', value: 'Error in cron job' }] }, | ||
}, | ||
}) | ||
.start(done); | ||
}); |
Oops, something went wrong.