Skip to content

Commit

Permalink
feat(eventprocessors): Add name field to EventProcessor (#4932)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored and Lms24 committed Apr 26, 2022
1 parent a54a8f2 commit 25487f7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 25 deletions.
7 changes: 5 additions & 2 deletions packages/browser/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Dedupe implements Integration {
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((currentEvent: Event) => {
const eventProcessor: EventProcessor = currentEvent => {
const self = getCurrentHub().getIntegration(Dedupe);
if (self) {
// Juuust in case something goes wrong
Expand All @@ -40,7 +40,10 @@ export class Dedupe implements Integration {
return (self._previousEvent = currentEvent);
}
return currentEvent;
});
};

eventProcessor.id = this.name;
addGlobalEventProcessor(eventProcessor);
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class InboundFilters implements Integration {
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((event: Event) => {
const eventProcess: EventProcessor = (event: Event) => {
const hub = getCurrentHub();
if (hub) {
const self = hub.getIntegration(InboundFilters);
Expand All @@ -45,7 +45,10 @@ export class InboundFilters implements Integration {
}
}
return event;
});
};

eventProcess.id = this.name;
addGlobalEventProcessor(eventProcess);
}
}

Expand Down
20 changes: 12 additions & 8 deletions packages/core/test/mocks/integration.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { getCurrentHub } from '@sentry/hub';
import { configureScope } from '@sentry/minimal';
import { Event, Integration } from '@sentry/types';
import { Event, EventProcessor, Integration } from '@sentry/types';

export class TestIntegration implements Integration {
public static id: string = 'TestIntegration';

public name: string = 'TestIntegration';

public setupOnce(): void {
configureScope(scope => {
scope.addEventProcessor((event: Event) => {
if (!getCurrentHub().getIntegration(TestIntegration)) {
return event;
}
const eventProcessor: EventProcessor = (event: Event) => {
if (!getCurrentHub().getIntegration(TestIntegration)) {
return event;
}

return null;
};

return null;
});
eventProcessor.id = this.name;

configureScope(scope => {
scope.addEventProcessor(eventProcessor);
});
}
}
18 changes: 16 additions & 2 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ import {
Transaction,
User,
} from '@sentry/types';
import { dateTimestampInSeconds, getGlobalSingleton, isPlainObject, isThenable, SyncPromise } from '@sentry/utils';

import {
dateTimestampInSeconds,
getGlobalSingleton,
isPlainObject,
isThenable,
logger,
SyncPromise,
} from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';
import { Session } from './session';

/**
Expand Down Expand Up @@ -462,6 +470,12 @@ export class Scope implements ScopeInterface {
resolve(event);
} else {
const result = processor({ ...event }, hint) as Event | null;

IS_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))
Expand Down
7 changes: 5 additions & 2 deletions packages/integrations/src/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Dedupe implements Integration {
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((currentEvent: Event) => {
const eventProcessor: EventProcessor = currentEvent => {
const self = getCurrentHub().getIntegration(Dedupe);
if (self) {
// Juuust in case something goes wrong
Expand All @@ -40,7 +40,10 @@ export class Dedupe implements Integration {
return (self._previousEvent = currentEvent);
}
return currentEvent;
});
};

eventProcessor.id = this.name;
addGlobalEventProcessor(eventProcessor);
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/integrations/src/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ export class Offline implements Integration {
});
}

addGlobalEventProcessor((event: Event) => {
const eventProcessor: EventProcessor = event => {
if (this.hub && this.hub.getIntegration(Offline)) {
// cache if we are positively offline
if ('navigator' in this.global && 'onLine' in this.global.navigator && !this.global.navigator.onLine) {
IS_DEBUG_BUILD && logger.log('Event dropped due to being a offline - caching instead');

void this._cacheEvent(event)
.then((_event: Event): Promise<void> => this._enforceMaxEvents())
.catch((_error): void => {
Expand All @@ -96,7 +98,10 @@ export class Offline implements Integration {
}

return event;
});
};

eventProcessor.id = this.name;
addGlobalEventProcessor(eventProcessor);

// if online now, send any events stored in a previous offline session
if ('navigator' in this.global && 'onLine' in this.global.navigator && this.global.navigator.onLine) {
Expand Down
7 changes: 6 additions & 1 deletion packages/nextjs/src/index.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { configureScope, init as reactInit, Integrations as BrowserIntegrations } from '@sentry/react';
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
import { EventProcessor } from '@sentry/types';

import { nextRouterInstrumentation } from './performance/client';
import { buildMetadata } from './utils/metadata';
Expand Down Expand Up @@ -42,9 +43,13 @@ export function init(options: NextjsOptions): void {
...options,
integrations,
});

configureScope(scope => {
scope.setTag('runtime', 'browser');
scope.addEventProcessor(event => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
const filterTransactions: EventProcessor = event =>
event.type === 'transaction' && event.transaction === '/404' ? null : event;
filterTransactions.id = 'NextClient404Filter';
scope.addEventProcessor(filterTransactions);
});
}

Expand Down
12 changes: 7 additions & 5 deletions packages/nextjs/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Carrier, getHubFromCarrier, getMainCarrier } from '@sentry/hub';
import { RewriteFrames } from '@sentry/integrations';
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
import { hasTracingEnabled } from '@sentry/tracing';
import { Event } from '@sentry/types';
import { EventProcessor } from '@sentry/types';
import { escapeStringForRegex, logger } from '@sentry/utils';
import * as domainModule from 'domain';
import * as path from 'path';
Expand Down Expand Up @@ -71,6 +71,12 @@ export function init(options: NextjsOptions): void {

nodeInit(options);

const filterTransactions: EventProcessor = event => {
return event.type === 'transaction' && event.transaction === '/404' ? null : event;
};

filterTransactions.id = 'NextServer404Filter';

configureScope(scope => {
scope.setTag('runtime', 'node');
if (isVercel) {
Expand Down Expand Up @@ -131,10 +137,6 @@ function addServerIntegrations(options: NextjsOptions): void {
}
}

function filterTransactions(event: Event): Event | null {
return event.type === 'transaction' && event.transaction === '/404' ? null : event;
}

export type { SentryWebpackPluginOptions } from './config/types';
export { withSentryConfig } from './config';
export { withSentry } from './utils/withSentry';
Expand Down
5 changes: 4 additions & 1 deletion packages/types/src/eventprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import { Event, EventHint } from './event';
* Returning a PromiseLike<Event | null> will work just fine, but better be sure that you know what you are doing.
* Event processing will be deferred until your Promise is resolved.
*/
export type EventProcessor = (event: Event, hint?: EventHint) => PromiseLike<Event | null> | Event | null;
export interface EventProcessor {
id?: string; // This field can't be named "name" because functions already have this field natively
(event: Event, hint?: EventHint): PromiseLike<Event | null> | Event | null;
}

0 comments on commit 25487f7

Please sign in to comment.