Skip to content

feat(core): Add addIntegration utility #9186

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
Oct 9, 2023
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
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type { ReportDialogOptions } from './helpers';
export {
addGlobalEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
captureEvent,
captureMessage,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type { BunOptions } from './types';
export {
addGlobalEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
captureEvent,
captureMessage,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export { createTransport } from './transports/base';
export { makeOfflineTransport } from './transports/offline';
export { makeMultiplexedTransport } from './transports/multiplexed';
export { SDK_VERSION } from './version';
export { getIntegrationsToSetup } from './integration';
export { getIntegrationsToSetup, addIntegration } from './integration';
export { FunctionToString, InboundFilters } from './integrations';
export { prepareEvent } from './utils/prepareEvent';
export { createCheckInEnvelope } from './checkin';
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ export function setupIntegration(client: Client, integration: Integration, integ
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
}

/** Add an integration to the current hub's client. */
export function addIntegration(integration: Integration): void {
const client = getCurrentHub().getClient();

if (!client || !client.addIntegration) {
__DEBUG_BUILD__ && logger.warn(`Cannot add integration "${integration.name}" because no SDK Client is available.`);
return;
}

client.addIntegration(integration);
}

// Polyfill for Array.findIndex(), which is not supported in ES5
function findIndex<T>(arr: T[], callback: (item: T) => boolean): number {
for (let i = 0; i < arr.length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ describe('BaseClient', () => {
});

test('handles being passed an invalid Dsn', () => {
// Hide warning logs in the test
jest.spyOn(console, 'error').mockImplementation(() => {});

const options = getDefaultTestClientOptions({ dsn: 'abc' });
const client = new TestClient(options);

Expand Down
52 changes: 51 additions & 1 deletion packages/core/test/lib/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Integration, Options } from '@sentry/types';
import { logger } from '@sentry/utils';

import { getIntegrationsToSetup, installedIntegrations, setupIntegration } from '../../src/integration';
import { Hub, makeMain } from '../../src/hub';
import { addIntegration, getIntegrationsToSetup, installedIntegrations, setupIntegration } from '../../src/integration';
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';

function getTestClient(): TestClient {
Expand Down Expand Up @@ -559,3 +561,51 @@ describe('setupIntegration', () => {
expect(sendEvent).not.toHaveBeenCalled();
});
});

describe('addIntegration', () => {
beforeEach(function () {
// Reset the (global!) list of installed integrations
installedIntegrations.splice(0, installedIntegrations.length);
});

afterEach(() => {
jest.clearAllMocks();
});

it('works with a client setup', () => {
const warnings = jest.spyOn(logger, 'warn');

class CustomIntegration implements Integration {
name = 'test';
setupOnce = jest.fn();
}

const client = getTestClient();
const hub = new Hub(client);
makeMain(hub);

const integration = new CustomIntegration();
addIntegration(integration);

expect(integration.setupOnce).toHaveBeenCalledTimes(1);
expect(warnings).not.toHaveBeenCalled();
});

it('works without a client setup', () => {
const warnings = jest.spyOn(logger, 'warn');
class CustomIntegration implements Integration {
name = 'test';
setupOnce = jest.fn();
}

const hub = new Hub();
makeMain(hub);

const integration = new CustomIntegration();
addIntegration(integration);

expect(integration.setupOnce).not.toHaveBeenCalled();
expect(warnings).toHaveBeenCalledTimes(1);
expect(warnings).toHaveBeenCalledWith('Cannot add integration "test" because no SDK Client is available.');
});
});
5 changes: 5 additions & 0 deletions packages/core/test/lib/transports/multiplexed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ describe('makeMultiplexedTransport', () => {
});

it('Falls back to options DSN when a matched DSN is invalid', async () => {
// Hide warning logs in the test
jest.spyOn(console, 'error').mockImplementation(() => {});

expect.assertions(1);

const makeTransport = makeMultiplexedTransport(
Expand All @@ -99,6 +102,8 @@ describe('makeMultiplexedTransport', () => {

const transport = makeTransport({ url: DSN1_URL, ...transportOptions });
await transport.send(ERROR_ENVELOPE);

jest.clearAllMocks();
});

it('DSN can be overridden via match callback', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type { NodeOptions } from './types';
export {
addGlobalEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
captureEvent,
captureMessage,
Expand Down
1 change: 1 addition & 0 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
Scope,
addBreadcrumb,
addGlobalEventProcessor,
addIntegration,
autoDiscoverNodePerformanceMonitoringIntegrations,
captureEvent,
captureException,
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export {
addGlobalEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
captureEvent,
captureMessage,
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type { VercelEdgeOptions } from './types';
export {
addGlobalEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
captureEvent,
captureMessage,
Expand Down