Skip to content

Commit

Permalink
Add integration.setup hook that always runs
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Sep 6, 2023
1 parent e9d0c67 commit a28ae01
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 55 deletions.
45 changes: 22 additions & 23 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';
import { applyAggregateErrorsToEvent } from '@sentry/utils';

import { exceptionFromError } from '../eventbuilder';
Expand Down Expand Up @@ -42,32 +42,31 @@ export class LinkedErrors implements Integration {
this._limit = options.limit || DEFAULT_LIMIT;
}

/** @inheritdoc */
public setupOnce(): void {
// noop
}

/**
* @inheritDoc
*/
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
const client = getCurrentHub().getClient();
if (client && client.on) {
client.on('preprocessEvent', (event, hint) => {
const hub = getCurrentHub();
const client = hub.getClient();
const self = hub.getIntegration(LinkedErrors);
public setup(client: Client): void {
if (!client.on) {
return;
}

if (!client || !self) {
return;
}
client.on('preprocessEvent', (event, hint) => {
const options = client.getOptions();

const options = client.getOptions();
applyAggregateErrorsToEvent(
exceptionFromError,
options.stackParser,
options.maxValueLength,
self._key,
self._limit,
event,
hint,
);
});
}
applyAggregateErrorsToEvent(
exceptionFromError,
options.stackParser,
options.maxValueLength,
this._key,
this._limit,
event,
hint,
);
});
}
}
4 changes: 2 additions & 2 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
*/
public setupIntegrations(): void {
if (this._isEnabled() && !this._integrationsInitialized) {
this._integrations = setupIntegrations(this._options.integrations);
this._integrations = setupIntegrations(this, this._options.integrations);
this._integrationsInitialized = true;
}
}
Expand Down Expand Up @@ -315,7 +315,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
* @inheritDoc
*/
public addIntegration(integration: Integration): void {
setupIntegration(integration, this._integrations);
setupIntegration(this, integration, this._integrations);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Integration, Options } from '@sentry/types';
import type { Client, Integration, Options } from '@sentry/types';
import { arrayify, logger } from '@sentry/utils';

import { getCurrentHub } from './hub';
Expand Down Expand Up @@ -84,28 +84,30 @@ export function getIntegrationsToSetup(options: Options): Integration[] {
* @param integrations array of integration instances
* @param withDefault should enable default integrations
*/
export function setupIntegrations(integrations: Integration[]): IntegrationIndex {
export function setupIntegrations(client: Client, integrations: Integration[]): IntegrationIndex {
const integrationIndex: IntegrationIndex = {};

integrations.forEach(integration => {
// guard against empty provided integrations
if (integration) {
setupIntegration(integration, integrationIndex);
setupIntegration(client, integration, integrationIndex);
}
});

return integrationIndex;
}

/** Setup a single integration. */
export function setupIntegration(integration: Integration, integrationIndex: IntegrationIndex): void {
export function setupIntegration(client: Client, integration: Integration, integrationIndex: IntegrationIndex): void {
integrationIndex[integration.name] = integration;

if (installedIntegrations.indexOf(integration.name) === -1) {
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
installedIntegrations.push(integration.name);
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
}

integration.setup && integration.setup(client);
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
}

// Polyfill for Array.findIndex(), which is not supported in ES5
Expand Down
47 changes: 22 additions & 25 deletions packages/node/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';
import { applyAggregateErrorsToEvent } from '@sentry/utils';

import { exceptionFromError } from '../eventbuilder';
Expand Down Expand Up @@ -36,34 +36,31 @@ export class LinkedErrors implements Integration {
this._limit = options.limit || DEFAULT_LIMIT;
}

/** @inheritdoc */
public setupOnce(): void {
// noop
}

/**
* @inheritDoc
*/
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
const client = getCurrentHub().getClient();
if (client && client.on) {
client.on('preprocessEvent', (event, hint) => {
const hub = getCurrentHub();
const client = hub.getClient();

const self = hub.getIntegration(LinkedErrors);

if (!client || !self) {
return;
}
public setup(client: Client): void {
if (!client.on) {
return;
}

const options = client.getOptions();
client.on('preprocessEvent', (event, hint) => {
const options = client.getOptions();

applyAggregateErrorsToEvent(
exceptionFromError,
options.stackParser,
options.maxValueLength,
self._key,
self._limit,
event,
hint,
);
});
}
applyAggregateErrorsToEvent(
exceptionFromError,
options.stackParser,
options.maxValueLength,
this._key,
this._limit,
event,
hint,
);
});
}
}
6 changes: 6 additions & 0 deletions packages/types/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Client } from './client';
import type { EventProcessor } from './eventprocessor';
import type { Hub } from './hub';

Expand All @@ -23,4 +24,9 @@ export interface Integration {
* This takes no options on purpose, options should be passed in the constructor
*/
setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void;

/**
* An optional hook that is called for each client, vs. only once.
*/
setup?(client: Client): void;
}

0 comments on commit a28ae01

Please sign in to comment.