Skip to content

Commit

Permalink
ref: Replace setup with preprocessEvent on Integration interface (#…
Browse files Browse the repository at this point in the history
…8969)

A change to #8956
based on feedback.

I figured it makes sense to also pass the client (as in the one concrete
example we have we actually need it 😅 ) - not sure if this should be the
first or last argument, but IMHO you probably don't need this always and
then it is nicer to have (event, hint) as the API...?
  • Loading branch information
mydea committed Sep 11, 2023
1 parent 976143b commit 40cd69e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
30 changes: 12 additions & 18 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
);
}
}
6 changes: 5 additions & 1 deletion packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down
32 changes: 13 additions & 19 deletions packages/node/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
);
}
}
5 changes: 3 additions & 2 deletions packages/types/src/integration.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}

0 comments on commit 40cd69e

Please sign in to comment.