Skip to content

Commit 225b8a3

Browse files
committed
Add integration.setup hook that always runs
1 parent 31cea0c commit 225b8a3

File tree

5 files changed

+59
-55
lines changed

5 files changed

+59
-55
lines changed

packages/browser/src/integrations/linkederrors.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EventProcessor, Hub, Integration } from '@sentry/types';
1+
import type { Client, Integration } from '@sentry/types';
22
import { applyAggregateErrorsToEvent } from '@sentry/utils';
33

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

45+
/** @inheritdoc */
46+
public setupOnce(): void {
47+
// noop
48+
}
49+
4550
/**
4651
* @inheritDoc
4752
*/
48-
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
49-
const client = getCurrentHub().getClient();
50-
if (client && client.on) {
51-
client.on('preprocessEvent', (event, hint) => {
52-
const hub = getCurrentHub();
53-
const client = hub.getClient();
54-
const self = hub.getIntegration(LinkedErrors);
53+
public setup(client: Client): void {
54+
if (!client.on) {
55+
return;
56+
}
5557

56-
if (!client || !self) {
57-
return;
58-
}
58+
client.on('preprocessEvent', (event, hint) => {
59+
const options = client.getOptions();
5960

60-
const options = client.getOptions();
61-
applyAggregateErrorsToEvent(
62-
exceptionFromError,
63-
options.stackParser,
64-
options.maxValueLength,
65-
self._key,
66-
self._limit,
67-
event,
68-
hint,
69-
);
70-
});
71-
}
61+
applyAggregateErrorsToEvent(
62+
exceptionFromError,
63+
options.stackParser,
64+
options.maxValueLength,
65+
this._key,
66+
this._limit,
67+
event,
68+
hint,
69+
);
70+
});
7271
}
7372
}

packages/core/src/baseclient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
285285
*/
286286
public setupIntegrations(): void {
287287
if (this._isEnabled() && !this._integrationsInitialized) {
288-
this._integrations = setupIntegrations(this._options.integrations);
288+
this._integrations = setupIntegrations(this, this._options.integrations);
289289
this._integrationsInitialized = true;
290290
}
291291
}
@@ -315,7 +315,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
315315
* @inheritDoc
316316
*/
317317
public addIntegration(integration: Integration): void {
318-
setupIntegration(integration, this._integrations);
318+
setupIntegration(this, integration, this._integrations);
319319
}
320320

321321
/**

packages/core/src/integration.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Integration, Options } from '@sentry/types';
1+
import type { Client, Integration, Options } from '@sentry/types';
22
import { arrayify, logger } from '@sentry/utils';
33

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

9090
integrations.forEach(integration => {
9191
// guard against empty provided integrations
9292
if (integration) {
93-
setupIntegration(integration, integrationIndex);
93+
setupIntegration(client, integration, integrationIndex);
9494
}
9595
});
9696

9797
return integrationIndex;
9898
}
9999

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

104104
if (installedIntegrations.indexOf(integration.name) === -1) {
105105
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
106106
installedIntegrations.push(integration.name);
107-
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
108107
}
108+
109+
integration.setup && integration.setup(client);
110+
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
109111
}
110112

111113
// Polyfill for Array.findIndex(), which is not supported in ES5

packages/node/src/integrations/linkederrors.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EventProcessor, Hub, Integration } from '@sentry/types';
1+
import type { Client, Integration } from '@sentry/types';
22
import { applyAggregateErrorsToEvent } from '@sentry/utils';
33

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

39+
/** @inheritdoc */
40+
public setupOnce(): void {
41+
// noop
42+
}
43+
3944
/**
4045
* @inheritDoc
4146
*/
42-
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
43-
const client = getCurrentHub().getClient();
44-
if (client && client.on) {
45-
client.on('preprocessEvent', (event, hint) => {
46-
const hub = getCurrentHub();
47-
const client = hub.getClient();
48-
49-
const self = hub.getIntegration(LinkedErrors);
50-
51-
if (!client || !self) {
52-
return;
53-
}
47+
public setup(client: Client): void {
48+
if (!client.on) {
49+
return;
50+
}
5451

55-
const options = client.getOptions();
52+
client.on('preprocessEvent', (event, hint) => {
53+
const options = client.getOptions();
5654

57-
applyAggregateErrorsToEvent(
58-
exceptionFromError,
59-
options.stackParser,
60-
options.maxValueLength,
61-
self._key,
62-
self._limit,
63-
event,
64-
hint,
65-
);
66-
});
67-
}
55+
applyAggregateErrorsToEvent(
56+
exceptionFromError,
57+
options.stackParser,
58+
options.maxValueLength,
59+
this._key,
60+
this._limit,
61+
event,
62+
hint,
63+
);
64+
});
6865
}
6966
}

packages/types/src/integration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Client } from './client';
12
import type { EventProcessor } from './eventprocessor';
23
import type { Hub } from './hub';
34

@@ -23,4 +24,9 @@ export interface Integration {
2324
* This takes no options on purpose, options should be passed in the constructor
2425
*/
2526
setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void;
27+
28+
/**
29+
* An optional hook that is called for each client, vs. only once.
30+
*/
31+
setup?(client: Client): void;
2632
}

0 commit comments

Comments
 (0)