Skip to content

Commit 50d5386

Browse files
committed
test: Fix withMonitor integration test to use proper test runner
Rewrite integration test to use createRunner pattern like other tests. Test now properly captures check-ins and verifies distinct trace IDs when isolateTrace is enabled, following PR review feedback.
1 parent a16ae77 commit 50d5386

File tree

2 files changed

+82
-38
lines changed

2 files changed

+82
-38
lines changed
Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
import { withMonitor } from '@sentry/node';
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
23

3-
export async function run(): Promise<void> {
4-
// First withMonitor call without isolateTrace (should share trace)
5-
await withMonitor('cron-job-1', async () => {
6-
// Simulate some work
7-
await new Promise<void>((resolve) => {
8-
setTimeout(() => {
9-
resolve();
10-
}, 100);
11-
});
12-
}, {
13-
schedule: { type: 'crontab', value: '* * * * *' }
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
});
9+
10+
// First withMonitor call without isolateTrace
11+
// eslint-disable-next-line @sentry-internal/sdk/no-floating-promises
12+
Sentry.withMonitor('cron-job-1', async () => {
13+
// Simulate some work
14+
await new Promise<void>((resolve) => {
15+
setTimeout(() => {
16+
resolve();
17+
}, 100);
1418
});
19+
}, {
20+
schedule: { type: 'crontab', value: '* * * * *' },
21+
});
1522

16-
// Second withMonitor call with isolateTrace (should have different trace)
17-
await withMonitor('cron-job-2', async () => {
18-
// Simulate some work
19-
await new Promise<void>((resolve) => {
20-
setTimeout(() => {
21-
resolve();
22-
}, 100);
23-
});
24-
}, {
25-
schedule: { type: 'crontab', value: '* * * * *' },
26-
isolateTrace: true
23+
// Second withMonitor call with isolateTrace (should have different trace)
24+
// eslint-disable-next-line @sentry-internal/sdk/no-floating-promises
25+
Sentry.withMonitor('cron-job-2', async () => {
26+
// Simulate some work
27+
await new Promise<void>((resolve) => {
28+
setTimeout(() => {
29+
resolve();
30+
}, 100);
2731
});
28-
}
32+
}, {
33+
schedule: { type: 'crontab', value: '* * * * *' },
34+
isolateTrace: true,
35+
});
36+
37+
// Wait a bit for check-ins to complete before exiting
38+
setTimeout(() => {
39+
process.exit();
40+
}, 500);
Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1-
import { expect } from 'vitest';
2-
import type { Event, TransactionEvent } from '@sentry/types';
1+
import type { SerializedCheckIn } from '@sentry/core';
2+
import { afterAll, describe, expect, test } from 'vitest';
3+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
34

4-
export async function testResults(events: Event[]): Promise<void> {
5-
// Get all transaction events (which represent traces)
6-
const transactionEvents = events.filter((event): event is TransactionEvent => event.type === 'transaction');
5+
describe('withMonitor isolateTrace', () => {
6+
afterAll(() => {
7+
cleanupChildProcesses();
8+
});
79

8-
// Should have at least 2 transaction events (one for each withMonitor call)
9-
expect(transactionEvents.length).toBeGreaterThanOrEqual(2);
10+
test('creates distinct traces when isolateTrace is enabled', async () => {
11+
const checkIns: SerializedCheckIn[] = [];
1012

11-
// Get trace IDs from the transactions
12-
const traceIds = transactionEvents.map(event => event.contexts?.trace?.trace_id).filter(Boolean);
13+
await createRunner(__dirname, 'scenario.ts')
14+
.expect({
15+
check_in: checkIn => {
16+
checkIns.push(checkIn);
17+
},
18+
})
19+
.expect({
20+
check_in: checkIn => {
21+
checkIns.push(checkIn);
22+
},
23+
})
24+
.expect({
25+
check_in: checkIn => {
26+
checkIns.push(checkIn);
27+
},
28+
})
29+
.expect({
30+
check_in: checkIn => {
31+
checkIns.push(checkIn);
32+
},
33+
})
34+
.start()
35+
.completed();
1336

14-
// Should have at least 2 different trace IDs (verifying trace isolation)
15-
const uniqueTraceIds = [...new Set(traceIds)];
16-
expect(uniqueTraceIds.length).toBeGreaterThanOrEqual(2);
37+
// Find the two 'ok' check-ins for comparison
38+
const checkIn1Ok = checkIns.find(c => c.monitor_slug === 'cron-job-1' && c.status === 'ok');
39+
const checkIn2Ok = checkIns.find(c => c.monitor_slug === 'cron-job-2' && c.status === 'ok');
1740

18-
console.log('✅ Found traces with different trace IDs:', uniqueTraceIds);
19-
}
41+
expect(checkIn1Ok).toBeDefined();
42+
expect(checkIn2Ok).toBeDefined();
43+
44+
// Verify both check-ins have trace contexts
45+
expect(checkIn1Ok!.contexts?.trace?.trace_id).toBeDefined();
46+
expect(checkIn2Ok!.contexts?.trace?.trace_id).toBeDefined();
47+
48+
// The key assertion: trace IDs should be different when isolateTrace is enabled
49+
expect(checkIn1Ok!.contexts!.trace!.trace_id).not.toBe(checkIn2Ok!.contexts!.trace!.trace_id);
50+
});
51+
});

0 commit comments

Comments
 (0)