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

feat(core): Introduce processEvent hook on Integration #9017

Merged
merged 3 commits into from
Sep 15, 2023
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
16 changes: 15 additions & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Event,
EventDropReason,
EventHint,
EventProcessor,
Integration,
IntegrationClass,
Outcome,
Expand Down Expand Up @@ -107,6 +108,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
// eslint-disable-next-line @typescript-eslint/ban-types
private _hooks: Record<string, Function[]>;

private _eventProcessors: EventProcessor[];

/**
* Initializes this client instance.
*
Expand All @@ -119,6 +122,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
this._numProcessing = 0;
this._outcomes = {};
this._hooks = {};
this._eventProcessors = [];

if (options.dsn) {
this._dsn = makeDsn(options.dsn);
Expand Down Expand Up @@ -280,6 +284,16 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
});
}

/** Get all installed event processors. */
public getEventProcessors(): EventProcessor[] {
return this._eventProcessors;
}

/** @inheritDoc */
public addEventProcessor(eventProcessor: EventProcessor): void {
this._eventProcessors.push(eventProcessor);
}

/**
* Sets up the integrations
*/
Expand Down Expand Up @@ -545,7 +559,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {

this.emit('preprocessEvent', event, hint);

return prepareEvent(options, event, hint, scope).then(evt => {
return prepareEvent(options, event, hint, scope, this).then(evt => {
if (evt === null) {
return evt;
}
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/eventProcessors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Event, EventHint, EventProcessor } from '@sentry/types';
import { getGlobalSingleton, isThenable, logger, SyncPromise } from '@sentry/utils';

/**
* Returns the global event processors.
*/
export function getGlobalEventProcessors(): EventProcessor[] {
return getGlobalSingleton<EventProcessor[]>('globalEventProcessors', () => []);
}

/**
* Add a EventProcessor to be kept globally.
* @param callback EventProcessor to add
*/
export function addGlobalEventProcessor(callback: EventProcessor): void {
getGlobalEventProcessors().push(callback);
}

/**
* Process an array of event processors, returning the processed event (or `null` if the event was dropped).
*/
export function notifyEventProcessors(
processors: EventProcessor[],
event: Event | null,
hint: EventHint,
index: number = 0,
): PromiseLike<Event | null> {
return new SyncPromise<Event | null>((resolve, reject) => {
const processor = processors[index];
if (event === null || typeof processor !== 'function') {
resolve(event);
} else {
const result = processor({ ...event }, hint) as Event | null;

__DEBUG_BUILD__ &&
processor.id &&
result === null &&
logger.log(`Event processor "${processor.id}" dropped event`);

if (isThenable(result)) {
void result
.then(final => notifyEventProcessors(processors, final, hint, index + 1).then(resolve))
.then(null, reject);
} else {
void notifyEventProcessors(processors, result, hint, index + 1)
.then(resolve)
.then(null, reject);
}
}
});
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export {
} from './hub';
export { makeSession, closeSession, updateSession } from './session';
export { SessionFlusher } from './sessionflusher';
export { addGlobalEventProcessor, Scope } from './scope';
export { Scope } from './scope';
export { addGlobalEventProcessor } from './eventProcessors';
export { getEnvelopeEndpointWithUrlEncodedAuth, getReportDialogEndpoint } from './api';
export { BaseClient } from './baseclient';
export { ServerRuntimeClient } from './server-runtime-client';
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Client, Integration, Options } from '@sentry/types';
import type { Client, Event, EventHint, Integration, Options } from '@sentry/types';
import { arrayify, logger } from '@sentry/utils';

import { addGlobalEventProcessor } from './eventProcessors';
import { getCurrentHub } from './hub';
import { addGlobalEventProcessor } from './scope';

declare module '@sentry/types' {
interface Integration {
Expand Down Expand Up @@ -107,10 +107,20 @@ export function setupIntegration(client: Client, integration: Integration, integ
}

if (client.on && typeof integration.preprocessEvent === 'function') {
const callback = integration.preprocessEvent.bind(integration);
const callback = integration.preprocessEvent.bind(integration) as typeof integration.preprocessEvent;
client.on('preprocessEvent', (event, hint) => callback(event, hint, client));
}

if (client.addEventProcessor && typeof integration.processEvent === 'function') {
const callback = integration.processEvent.bind(integration) as typeof integration.processEvent;

const processor = Object.assign((event: Event, hint: EventHint) => callback(event, hint, client), {
id: integration.name,
});

client.addEventProcessor(processor);
}

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

Expand Down
63 changes: 3 additions & 60 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@ import type {
Transaction,
User,
} from '@sentry/types';
import {
arrayify,
dateTimestampInSeconds,
getGlobalSingleton,
isPlainObject,
isThenable,
logger,
SyncPromise,
uuid4,
} from '@sentry/utils';
import { arrayify, dateTimestampInSeconds, isPlainObject, uuid4 } from '@sentry/utils';

import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
import { updateSession } from './session';

/**
Expand Down Expand Up @@ -525,7 +517,7 @@ export class Scope implements ScopeInterface {
propagationContext: this._propagationContext,
};

return this._notifyEventProcessors([...getGlobalEventProcessors(), ...this._eventProcessors], event, hint);
return notifyEventProcessors([...getGlobalEventProcessors(), ...this._eventProcessors], event, hint);
}

/**
Expand Down Expand Up @@ -559,40 +551,6 @@ export class Scope implements ScopeInterface {
return this._breadcrumbs;
}

/**
* This will be called after {@link applyToEvent} is finished.
*/
protected _notifyEventProcessors(
processors: EventProcessor[],
event: Event | null,
hint: EventHint,
index: number = 0,
): PromiseLike<Event | null> {
return new SyncPromise<Event | null>((resolve, reject) => {
const processor = processors[index];
if (event === null || typeof processor !== 'function') {
resolve(event);
} else {
const result = processor({ ...event }, hint) as Event | null;

__DEBUG_BUILD__ &&
processor.id &&
result === null &&
logger.log(`Event processor "${processor.id}" dropped event`);

if (isThenable(result)) {
void result
.then(final => this._notifyEventProcessors(processors, final, hint, index + 1).then(resolve))
.then(null, reject);
} else {
void this._notifyEventProcessors(processors, result, hint, index + 1)
.then(resolve)
.then(null, reject);
}
}
});
}

/**
* This will be called on every set call.
*/
Expand Down Expand Up @@ -629,21 +587,6 @@ export class Scope implements ScopeInterface {
}
}

/**
* Returns the global event processors.
*/
function getGlobalEventProcessors(): EventProcessor[] {
return getGlobalSingleton<EventProcessor[]>('globalEventProcessors', () => []);
}

/**
* Add a EventProcessor to be kept globally.
* @param callback EventProcessor to add
*/
export function addGlobalEventProcessor(callback: EventProcessor): void {
getGlobalEventProcessors().push(callback);
}

function generatePropagationContext(): PropagationContext {
return {
traceId: uuid4(),
Expand Down
35 changes: 21 additions & 14 deletions packages/core/src/utils/prepareEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types';
import type { Client, ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types';
import { dateTimestampInSeconds, GLOBAL_OBJ, normalize, resolvedSyncPromise, truncate, uuid4 } from '@sentry/utils';

import { DEFAULT_ENVIRONMENT } from '../constants';
import { notifyEventProcessors } from '../eventProcessors';
import { Scope } from '../scope';

/**
Expand All @@ -26,6 +27,7 @@ export function prepareEvent(
event: Event,
hint: EventHint,
scope?: Scope,
client?: Client,
): PromiseLike<Event | null> {
const { normalizeDepth = 3, normalizeMaxBreadth = 1_000 } = options;
const prepared: Event = {
Expand Down Expand Up @@ -74,20 +76,25 @@ export function prepareEvent(
result = finalScope.applyToEvent(prepared, hint);
}

return result.then(evt => {
if (evt) {
// We apply the debug_meta field only after all event processors have ran, so that if any event processors modified
// file names (e.g.the RewriteFrames integration) the filename -> debug ID relationship isn't destroyed.
// This should not cause any PII issues, since we're only moving data that is already on the event and not adding
// any new data
applyDebugMeta(evt);
}
return result
.then(evt => {
// Process client-scoped event processors
return client && client.getEventProcessors ? notifyEventProcessors(client.getEventProcessors(), evt, hint) : evt;
Copy link
Member Author

Choose a reason for hiding this comment

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

I actually noticed a problem with the implementation in one of the other PRs (for browser). Which is that we can't just call this after prepareEvent is completed in the baseclient, because then the stuff that happens in here below will not be applied correctly.

So I updated it to instead run this here. And I am wondering, what do you think is nicer:

  1. Expose getEventProcessors() on the client (like I did here)
  2. Instead expose e.g. applyToEvent() like we have on the scope and call that directly (which under the hood applies the event processors)?

cc @AbhiPrasad / @Lms24 / @lforst

Copy link
Member

Choose a reason for hiding this comment

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

Let's do getEventProcessors on the client - I'd rather not show more internals than what is needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

sounds good. we maybe will adjust this for v8 anyhow if we adjust the event processing pipeline, but should be good for now I guess.

})
.then(evt => {
if (evt) {
// We apply the debug_meta field only after all event processors have ran, so that if any event processors modified
// file names (e.g.the RewriteFrames integration) the filename -> debug ID relationship isn't destroyed.
// This should not cause any PII issues, since we're only moving data that is already on the event and not adding
// any new data
applyDebugMeta(evt);
}

if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
return normalizeEvent(evt, normalizeDepth, normalizeMaxBreadth);
}
return evt;
});
if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
return normalizeEvent(evt, normalizeDepth, normalizeMaxBreadth);
}
return evt;
});
}

/**
Expand Down
Loading