-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathintegration.worker.test.ts
76 lines (66 loc) · 2.36 KB
/
integration.worker.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { Transport } from '@sentry/core';
import { type ProfilingIntegration } from '@sentry/core';
import * as Sentry from '@sentry/node';
import { expect, it, vi } from 'vitest';
import { _nodeProfilingIntegration } from '../src/integration';
// Mock the modules before the import, so that the value is initialized before the module is loaded
vi.mock('worker_threads', () => {
return {
isMainThread: false,
threadId: 9999,
};
});
vi.setConfig({ testTimeout: 10_000 });
function makeContinuousProfilingClient(): [Sentry.NodeClient, Transport] {
const integration = _nodeProfilingIntegration();
const client = new Sentry.NodeClient({
stackParser: Sentry.defaultStackParser,
tracesSampleRate: 1,
debug: true,
environment: 'test-environment',
dsn: 'https://[email protected]/6625302',
integrations: [integration],
transport: _opts =>
Sentry.makeNodeTransport({
url: 'https://[email protected]/6625302',
recordDroppedEvent: () => {
return undefined;
},
}),
});
return [client, client.getTransport() as Transport];
}
it('worker threads context', () => {
const [client, transport] = makeContinuousProfilingClient();
Sentry.setCurrentClient(client);
client.init();
const transportSpy = vi.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));
const nonProfiledTransaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
nonProfiledTransaction.end();
expect(transportSpy.mock.calls?.[0]?.[0]?.[1]?.[0]?.[1]).not.toMatchObject({
contexts: {
profile: {},
},
});
const integration = client.getIntegrationByName<ProfilingIntegration<any>>('ProfilingIntegration');
if (!integration) {
throw new Error('Profiling integration not found');
}
integration._profiler.start();
const profiledTransaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
profiledTransaction.end();
integration._profiler.stop();
expect(transportSpy.mock.calls?.[1]?.[0]?.[1]?.[0]?.[1]).toMatchObject({
contexts: {
trace: {
data: expect.objectContaining({
['thread.id']: '9999',
['thread.name']: 'worker',
}),
},
profile: {
profiler_id: expect.any(String),
},
},
});
});