Skip to content

feat(bun): Automatically add performance integrations #15586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/bun/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as os from 'node:os';
import {
applySdkMetadata,
functionToStringIntegration,
hasSpansEnabled,
inboundFiltersIntegration,
linkedErrorsIntegration,
requestDataIntegration,
Expand All @@ -11,6 +12,7 @@ import type { NodeClient } from '@sentry/node';
import {
consoleIntegration,
contextLinesIntegration,
getAutoPerformanceIntegrations,
httpIntegration,
init as initNode,
modulesIntegration,
Expand Down Expand Up @@ -48,6 +50,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
modulesIntegration(),
// Bun Specific
bunServerIntegration(),
...(hasSpansEnabled(_options) ? getAutoPerformanceIntegrations() : []),
];
}

Expand Down
112 changes: 112 additions & 0 deletions packages/bun/test/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { type Integration } from '@sentry/core';
import * as sentryNode from '@sentry/node';
import type { Mock } from 'bun:test';
import { afterEach, beforeEach, describe, it, spyOn, mock, expect } from 'bun:test';
import { getClient, init } from '../src';

const PUBLIC_DSN = 'https://username@domain/123';

class MockIntegration implements Integration {
public name: string;
public setupOnce: Mock<() => void>;
public constructor(name: string) {
this.name = name;
this.setupOnce = mock(() => undefined);
}
}

describe('init()', () => {
let mockAutoPerformanceIntegrations: Mock<() => Integration[]>;

beforeEach(() => {
// @ts-expect-error weird
mockAutoPerformanceIntegrations = spyOn(sentryNode, 'getAutoPerformanceIntegrations');
});

afterEach(() => {
mockAutoPerformanceIntegrations.mockRestore();
});

describe('integrations', () => {
it("doesn't install default integrations if told not to", () => {
init({ dsn: PUBLIC_DSN, defaultIntegrations: false });

const client = getClient();

expect(client?.getOptions().integrations).toEqual([]);

expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});

it('installs merged default integrations, with overrides provided through options', () => {
const mockDefaultIntegrations = [
new MockIntegration('Some mock integration 2.1'),
new MockIntegration('Some mock integration 2.2'),
];

const mockIntegrations = [
new MockIntegration('Some mock integration 2.1'),
new MockIntegration('Some mock integration 2.3'),
];

init({ dsn: PUBLIC_DSN, integrations: mockIntegrations, defaultIntegrations: mockDefaultIntegrations });

expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(0);
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});

it('installs integrations returned from a callback function', () => {
const mockDefaultIntegrations = [
new MockIntegration('Some mock integration 3.1'),
new MockIntegration('Some mock integration 3.2'),
];

const newIntegration = new MockIntegration('Some mock integration 3.3');

init({
dsn: PUBLIC_DSN,
defaultIntegrations: mockDefaultIntegrations,
integrations: integrations => {
const newIntegrations = [...integrations];
newIntegrations[1] = newIntegration;
return newIntegrations;
},
});

expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(0);
expect(newIntegration.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});

it('installs performance default instrumentations if tracing is enabled', () => {
const autoPerformanceIntegrations = [new MockIntegration('Performance integration')];
mockAutoPerformanceIntegrations.mockImplementation(() => autoPerformanceIntegrations);

const mockIntegrations = [
new MockIntegration('Some mock integration 4.1'),
new MockIntegration('Some mock integration 4.3'),
];

init({
dsn: PUBLIC_DSN,
integrations: mockIntegrations,
tracesSampleRate: 1,
});

expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(autoPerformanceIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(1);

const integrations = getClient()?.getOptions().integrations;
expect(integrations).toBeArray();
expect(integrations?.map(({ name }) => name)).toContain('Performance integration');
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.1');
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.3');
});
});
});
Loading