Skip to content

Commit e76ec36

Browse files
sebwsLms24
authored andcommitted
test(node-cron): isolateTrace isolates cron errors
1 parent efcb15b commit e76ec36

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

dev-packages/node-core-integration-tests/suites/cron/node-cron/scenario.ts renamed to dev-packages/node-core-integration-tests/suites/cron/node-cron/base/scenario.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Sentry from '@sentry/node-core';
22
import { loggingTransport } from '@sentry-internal/node-integration-tests';
33
import * as cron from 'node-cron';
4-
import { setupOtel } from '../../../utils/setupOtel';
4+
import { setupOtel } from '../../../../utils/setupOtel';
55

66
const client = Sentry.init({
77
dsn: 'https://public@dsn.ingest.sentry.io/1337',

dev-packages/node-core-integration-tests/suites/cron/node-cron/test.ts renamed to dev-packages/node-core-integration-tests/suites/cron/node-cron/base/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterAll, expect, test } from 'vitest';
2-
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
33

44
afterAll(() => {
55
cleanupChildProcesses();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as Sentry from '@sentry/node-core';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
import * as cron from 'node-cron';
4+
import { setupOtel } from '../../../../utils/setupOtel';
5+
6+
const client = Sentry.init({
7+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
8+
release: '1.0',
9+
transport: loggingTransport,
10+
});
11+
12+
setupOtel(client);
13+
14+
const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron, { isolateTrace: true });
15+
16+
let closeNext1 = false;
17+
let closeNext2 = false;
18+
19+
const task = cronWithCheckIn.schedule(
20+
'* * * * * *',
21+
() => {
22+
if (closeNext1) {
23+
// https://github.com/node-cron/node-cron/issues/317
24+
setImmediate(() => {
25+
task.stop();
26+
});
27+
28+
throw new Error('Error in cron job');
29+
}
30+
31+
// eslint-disable-next-line no-console
32+
console.log('You will see this message every second');
33+
closeNext1 = true;
34+
},
35+
{ name: 'my-cron-job' },
36+
);
37+
38+
const task2 = cronWithCheckIn.schedule(
39+
'* * * * * *',
40+
() => {
41+
if (closeNext2) {
42+
// https://github.com/node-cron/node-cron/issues/317
43+
setImmediate(() => {
44+
task2.stop();
45+
});
46+
47+
throw new Error('Error in cron job 2');
48+
}
49+
50+
// eslint-disable-next-line no-console
51+
console.log('You will see this message every second');
52+
closeNext2 = true;
53+
},
54+
{ name: 'my-2nd-cron-job' },
55+
);
56+
57+
setTimeout(() => {
58+
process.exit();
59+
}, 5000);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { afterAll, expect, test } from 'vitest';
2+
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
3+
4+
afterAll(() => {
5+
cleanupChildProcesses();
6+
});
7+
8+
test('node-cron instrumentation with isolateTrace creates distinct traces for each cron job', async () => {
9+
let firstErrorTraceId: string | undefined;
10+
11+
await createRunner(__dirname, 'scenario.ts')
12+
.ignore('check_in')
13+
.expect({
14+
event: event => {
15+
const traceId = event.contexts?.trace?.trace_id;
16+
const spanId = event.contexts?.trace?.span_id;
17+
18+
expect(traceId).toMatch(/[a-f\d]{32}/);
19+
expect(spanId).toMatch(/[a-f\d]{16}/);
20+
21+
firstErrorTraceId = traceId;
22+
23+
expect(event.exception?.values?.[0]).toMatchObject({
24+
type: 'Error',
25+
value: expect.stringMatching(/^Error in cron job( 2)?$/),
26+
mechanism: { type: 'auto.function.node-cron.instrumentNodeCron', handled: false },
27+
});
28+
},
29+
})
30+
.expect({
31+
event: event => {
32+
const traceId = event.contexts?.trace?.trace_id;
33+
const spanId = event.contexts?.trace?.span_id;
34+
35+
expect(traceId).toMatch(/[a-f\d]{32}/);
36+
expect(spanId).toMatch(/[a-f\d]{16}/);
37+
38+
expect(traceId).not.toBe(firstErrorTraceId);
39+
40+
expect(event.exception?.values?.[0]).toMatchObject({
41+
type: 'Error',
42+
value: expect.stringMatching(/^Error in cron job( 2)?$/),
43+
mechanism: { type: 'auto.function.node-cron.instrumentNodeCron', handled: false },
44+
});
45+
},
46+
})
47+
.start()
48+
.completed();
49+
});

0 commit comments

Comments
 (0)