From 100ffef54699a92be52a5a6eb7cfc2c24d163b9a Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 7 Sep 2023 11:54:53 +0200 Subject: [PATCH] ref: Replace `setup` with `preprocessEvent` on in Integration interface --- .../browser/src/integrations/linkederrors.ts | 30 +++++++---------- packages/core/src/integration.ts | 6 +++- .../node/src/integrations/linkederrors.ts | 32 ++++++++----------- packages/types/src/integration.ts | 5 +-- 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/packages/browser/src/integrations/linkederrors.ts b/packages/browser/src/integrations/linkederrors.ts index 11a3defd8f31..b07b12c98263 100644 --- a/packages/browser/src/integrations/linkederrors.ts +++ b/packages/browser/src/integrations/linkederrors.ts @@ -1,4 +1,4 @@ -import type { Client, Integration } from '@sentry/types'; +import type { Client, Event, EventHint, Integration } from '@sentry/types'; import { applyAggregateErrorsToEvent } from '@sentry/utils'; import { exceptionFromError } from '../eventbuilder'; @@ -50,23 +50,17 @@ export class LinkedErrors implements Integration { /** * @inheritDoc */ - public setup(client: Client): void { - if (!client.on) { - return; - } + public preprocessEvent(event: Event, hint: EventHint | undefined, client: Client): void { + const options = client.getOptions(); - client.on('preprocessEvent', (event, hint) => { - const options = client.getOptions(); - - applyAggregateErrorsToEvent( - exceptionFromError, - options.stackParser, - options.maxValueLength, - this._key, - this._limit, - event, - hint, - ); - }); + applyAggregateErrorsToEvent( + exceptionFromError, + options.stackParser, + options.maxValueLength, + this._key, + this._limit, + event, + hint, + ); } } diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index 13b98f11205f..b2c8e4547ab8 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -106,7 +106,11 @@ export function setupIntegration(client: Client, integration: Integration, integ installedIntegrations.push(integration.name); } - integration.setup && integration.setup(client); + if (client.on && typeof integration.preprocessEvent === 'function') { + const callback = integration.preprocessEvent.bind(integration); + client.on('preprocessEvent', (event, hint) => callback(event, hint, client)); + } + __DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`); } diff --git a/packages/node/src/integrations/linkederrors.ts b/packages/node/src/integrations/linkederrors.ts index b75992781903..78062f708d6b 100644 --- a/packages/node/src/integrations/linkederrors.ts +++ b/packages/node/src/integrations/linkederrors.ts @@ -1,4 +1,4 @@ -import type { Client, Integration } from '@sentry/types'; +import type { Client, Event, EventHint, Integration } from '@sentry/types'; import { applyAggregateErrorsToEvent } from '@sentry/utils'; import { exceptionFromError } from '../eventbuilder'; @@ -44,23 +44,17 @@ export class LinkedErrors implements Integration { /** * @inheritDoc */ - public setup(client: Client): void { - if (!client.on) { - return; - } - - client.on('preprocessEvent', (event, hint) => { - const options = client.getOptions(); - - applyAggregateErrorsToEvent( - exceptionFromError, - options.stackParser, - options.maxValueLength, - this._key, - this._limit, - event, - hint, - ); - }); + public preprocessEvent(event: Event, hint: EventHint | undefined, client: Client): void { + const options = client.getOptions(); + + applyAggregateErrorsToEvent( + exceptionFromError, + options.stackParser, + options.maxValueLength, + this._key, + this._limit, + event, + hint, + ); } } diff --git a/packages/types/src/integration.ts b/packages/types/src/integration.ts index a1142f6a2e1a..0c1feae65323 100644 --- a/packages/types/src/integration.ts +++ b/packages/types/src/integration.ts @@ -1,4 +1,5 @@ import type { Client } from './client'; +import type { Event, EventHint } from './event'; import type { EventProcessor } from './eventprocessor'; import type { Hub } from './hub'; @@ -26,7 +27,7 @@ export interface Integration { setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void; /** - * An optional hook that is called for each client, vs. only once. + * An optional hook that allows to preprocess an event _before_ it is passed to all other event processors. */ - setup?(client: Client): void; + preprocessEvent?(event: Event, hint: EventHint | undefined, client: Client): void; }