Skip to content
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

ref(baseclient): Simplify the setup logic of integrations #4952

Merged
merged 1 commit into from
Apr 20, 2022
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
10 changes: 7 additions & 3 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
protected readonly _dsn?: DsnComponents;

/** Array of used integrations. */
/** Array of set up integrations. */
protected _integrations: IntegrationIndex = {};

/** Indicates whether this client's integrations have been set up. */
protected _integrationsInitialized: boolean = false;

/** Number of calls being processed */
protected _numProcessing: number = 0;

Expand Down Expand Up @@ -251,8 +254,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
* Sets up the integrations
*/
public setupIntegrations(): void {
if (this._isEnabled() && !this._integrations.initialized) {
this._integrations = setupIntegrations(this._options);
if (this._isEnabled() && !this._integrationsInitialized) {
this._integrations = setupIntegrations(this._options.integrations);
this._integrationsInitialized = true;
}
}

Expand Down
39 changes: 16 additions & 23 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
import { ClientOptions, Integration, Options } from '@sentry/types';
import { addNonEnumerableProperty, logger } from '@sentry/utils';
import { Integration, Options } from '@sentry/types';
import { logger } from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';

Expand All @@ -9,7 +9,7 @@ export const installedIntegrations: string[] = [];
/** Map of integrations assigned to a client */
export type IntegrationIndex = {
[key: string]: Integration;
} & { initialized?: boolean };
};

/**
* @private
Expand Down Expand Up @@ -54,31 +54,24 @@ export function getIntegrationsToSetup(options: Options): Integration[] {
return integrations;
}

/** Setup given integration */
export function setupIntegration(integration: Integration): void {
if (installedIntegrations.indexOf(integration.name) !== -1) {
return;
}
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
installedIntegrations.push(integration.name);
IS_DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
}

/**
* Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default
* integrations are added unless they were already provided before.
* @param integrations array of integration instances
* @param withDefault should enable default integrations
*/
export function setupIntegrations<O extends ClientOptions>(options: O): IntegrationIndex {
const integrations: IntegrationIndex = {};
options.integrations.forEach(integration => {
integrations[integration.name] = integration;
setupIntegration(integration);
export function setupIntegrations(integrations: Integration[]): IntegrationIndex {
const integrationIndex: IntegrationIndex = {};

integrations.forEach(integration => {
integrationIndex[integration.name] = integration;

if (installedIntegrations.indexOf(integration.name) === -1) {
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
installedIntegrations.push(integration.name);
IS_DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
}
Comment on lines +67 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a question than anything else, especially since this PR doesn't change anything in this regard : Can setupOnce fail? Currently, each integration is added to integrationIndex (which according to the JSdoc holds all set up integrations) before it is installed. This means that the index could hold integrations that actually aren't installed if setupOnce fails (?).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Looking at some integrations, it seems they are generally designed in a way that doesn't throw, but just returns if anything goes wrong.

});
// set the `initialized` flag so we don't run through the process again unecessarily; use `Object.defineProperty`
// because by default it creates a property which is nonenumerable, which we want since `initialized` shouldn't be
// considered a member of the index the way the actual integrations are
addNonEnumerableProperty(integrations, 'initialized', true);
return integrations;

return integrationIndex;
}