Skip to content

Commit 5bf4598

Browse files
authored
feat(bun): Automatically add performance integrations (#15586)
Automatically adds performance integrations to bun when tracing is enabled.
1 parent 97155e9 commit 5bf4598

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

packages/bun/src/sdk.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as os from 'node:os';
22
import {
33
applySdkMetadata,
44
functionToStringIntegration,
5+
hasSpansEnabled,
56
inboundFiltersIntegration,
67
linkedErrorsIntegration,
78
requestDataIntegration,
@@ -11,6 +12,7 @@ import type { NodeClient } from '@sentry/node';
1112
import {
1213
consoleIntegration,
1314
contextLinesIntegration,
15+
getAutoPerformanceIntegrations,
1416
httpIntegration,
1517
init as initNode,
1618
modulesIntegration,
@@ -48,6 +50,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
4850
modulesIntegration(),
4951
// Bun Specific
5052
bunServerIntegration(),
53+
...(hasSpansEnabled(_options) ? getAutoPerformanceIntegrations() : []),
5154
];
5255
}
5356

packages/bun/test/init.test.ts

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { type Integration } from '@sentry/core';
2+
import * as sentryNode from '@sentry/node';
3+
import type { Mock } from 'bun:test';
4+
import { afterEach, beforeEach, describe, it, spyOn, mock, expect } from 'bun:test';
5+
import { getClient, init } from '../src';
6+
7+
const PUBLIC_DSN = 'https://username@domain/123';
8+
9+
class MockIntegration implements Integration {
10+
public name: string;
11+
public setupOnce: Mock<() => void>;
12+
public constructor(name: string) {
13+
this.name = name;
14+
this.setupOnce = mock(() => undefined);
15+
}
16+
}
17+
18+
describe('init()', () => {
19+
let mockAutoPerformanceIntegrations: Mock<() => Integration[]>;
20+
21+
beforeEach(() => {
22+
// @ts-expect-error weird
23+
mockAutoPerformanceIntegrations = spyOn(sentryNode, 'getAutoPerformanceIntegrations');
24+
});
25+
26+
afterEach(() => {
27+
mockAutoPerformanceIntegrations.mockRestore();
28+
});
29+
30+
describe('integrations', () => {
31+
it("doesn't install default integrations if told not to", () => {
32+
init({ dsn: PUBLIC_DSN, defaultIntegrations: false });
33+
34+
const client = getClient();
35+
36+
expect(client?.getOptions().integrations).toEqual([]);
37+
38+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
39+
});
40+
41+
it('installs merged default integrations, with overrides provided through options', () => {
42+
const mockDefaultIntegrations = [
43+
new MockIntegration('Some mock integration 2.1'),
44+
new MockIntegration('Some mock integration 2.2'),
45+
];
46+
47+
const mockIntegrations = [
48+
new MockIntegration('Some mock integration 2.1'),
49+
new MockIntegration('Some mock integration 2.3'),
50+
];
51+
52+
init({ dsn: PUBLIC_DSN, integrations: mockIntegrations, defaultIntegrations: mockDefaultIntegrations });
53+
54+
expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(0);
55+
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
56+
expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
57+
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
58+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
59+
});
60+
61+
it('installs integrations returned from a callback function', () => {
62+
const mockDefaultIntegrations = [
63+
new MockIntegration('Some mock integration 3.1'),
64+
new MockIntegration('Some mock integration 3.2'),
65+
];
66+
67+
const newIntegration = new MockIntegration('Some mock integration 3.3');
68+
69+
init({
70+
dsn: PUBLIC_DSN,
71+
defaultIntegrations: mockDefaultIntegrations,
72+
integrations: integrations => {
73+
const newIntegrations = [...integrations];
74+
newIntegrations[1] = newIntegration;
75+
return newIntegrations;
76+
},
77+
});
78+
79+
expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
80+
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(0);
81+
expect(newIntegration.setupOnce).toHaveBeenCalledTimes(1);
82+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
83+
});
84+
85+
it('installs performance default instrumentations if tracing is enabled', () => {
86+
const autoPerformanceIntegrations = [new MockIntegration('Performance integration')];
87+
mockAutoPerformanceIntegrations.mockImplementation(() => autoPerformanceIntegrations);
88+
89+
const mockIntegrations = [
90+
new MockIntegration('Some mock integration 4.1'),
91+
new MockIntegration('Some mock integration 4.3'),
92+
];
93+
94+
init({
95+
dsn: PUBLIC_DSN,
96+
integrations: mockIntegrations,
97+
tracesSampleRate: 1,
98+
});
99+
100+
expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
101+
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
102+
expect(autoPerformanceIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
103+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(1);
104+
105+
const integrations = getClient()?.getOptions().integrations;
106+
expect(integrations).toBeArray();
107+
expect(integrations?.map(({ name }) => name)).toContain('Performance integration');
108+
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.1');
109+
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.3');
110+
});
111+
});
112+
});

0 commit comments

Comments
 (0)