Skip to content

Commit 6bfb589

Browse files
lforstlobsterkatie
authored andcommitted
feat: Add name field to EventProcessor (#4932)
1 parent 4dbd69e commit 6bfb589

File tree

9 files changed

+67
-25
lines changed

9 files changed

+67
-25
lines changed

packages/browser/src/integrations/dedupe.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Dedupe implements Integration {
2424
* @inheritDoc
2525
*/
2626
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
27-
addGlobalEventProcessor((currentEvent: Event) => {
27+
const eventProcessor: EventProcessor = currentEvent => {
2828
const self = getCurrentHub().getIntegration(Dedupe);
2929
if (self) {
3030
// Juuust in case something goes wrong
@@ -40,7 +40,10 @@ export class Dedupe implements Integration {
4040
return (self._previousEvent = currentEvent);
4141
}
4242
return currentEvent;
43-
});
43+
};
44+
45+
eventProcessor.id = this.name;
46+
addGlobalEventProcessor(eventProcessor);
4447
}
4548
}
4649

packages/core/src/integrations/inboundfilters.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class InboundFilters implements Integration {
3333
* @inheritDoc
3434
*/
3535
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void {
36-
addGlobalEventProcessor((event: Event) => {
36+
const eventProcess: EventProcessor = (event: Event) => {
3737
const hub = getCurrentHub();
3838
if (hub) {
3939
const self = hub.getIntegration(InboundFilters);
@@ -45,7 +45,10 @@ export class InboundFilters implements Integration {
4545
}
4646
}
4747
return event;
48-
});
48+
};
49+
50+
eventProcess.id = this.name;
51+
addGlobalEventProcessor(eventProcess);
4952
}
5053
}
5154

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { getCurrentHub } from '@sentry/hub';
22
import { configureScope } from '@sentry/minimal';
3-
import { Event, Integration } from '@sentry/types';
3+
import { Event, EventProcessor, Integration } from '@sentry/types';
44

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

88
public name: string = 'TestIntegration';
99

1010
public setupOnce(): void {
11-
configureScope(scope => {
12-
scope.addEventProcessor((event: Event) => {
13-
if (!getCurrentHub().getIntegration(TestIntegration)) {
14-
return event;
15-
}
11+
const eventProcessor: EventProcessor = (event: Event) => {
12+
if (!getCurrentHub().getIntegration(TestIntegration)) {
13+
return event;
14+
}
15+
16+
return null;
17+
};
1618

17-
return null;
18-
});
19+
eventProcessor.id = this.name;
20+
21+
configureScope(scope => {
22+
scope.addEventProcessor(eventProcessor);
1923
});
2024
}
2125
}

packages/hub/src/scope.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ import {
1919
Transaction,
2020
User,
2121
} from '@sentry/types';
22-
import { dateTimestampInSeconds, getGlobalSingleton, isPlainObject, isThenable, SyncPromise } from '@sentry/utils';
23-
22+
import {
23+
dateTimestampInSeconds,
24+
getGlobalSingleton,
25+
isPlainObject,
26+
isThenable,
27+
logger,
28+
SyncPromise,
29+
} from '@sentry/utils';
30+
31+
import { IS_DEBUG_BUILD } from './flags';
2432
import { Session } from './session';
2533

2634
/**
@@ -462,6 +470,12 @@ export class Scope implements ScopeInterface {
462470
resolve(event);
463471
} else {
464472
const result = processor({ ...event }, hint) as Event | null;
473+
474+
IS_DEBUG_BUILD &&
475+
processor.id &&
476+
result === null &&
477+
logger.log(`Event processor "${processor.id}" dropped event`);
478+
465479
if (isThenable(result)) {
466480
void result
467481
.then(final => this._notifyEventProcessors(processors, final, hint, index + 1).then(resolve))

packages/integrations/src/dedupe.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Dedupe implements Integration {
2424
* @inheritDoc
2525
*/
2626
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
27-
addGlobalEventProcessor((currentEvent: Event) => {
27+
const eventProcessor: EventProcessor = currentEvent => {
2828
const self = getCurrentHub().getIntegration(Dedupe);
2929
if (self) {
3030
// Juuust in case something goes wrong
@@ -40,7 +40,10 @@ export class Dedupe implements Integration {
4040
return (self._previousEvent = currentEvent);
4141
}
4242
return currentEvent;
43-
});
43+
};
44+
45+
eventProcessor.id = this.name;
46+
addGlobalEventProcessor(eventProcessor);
4447
}
4548
}
4649

packages/integrations/src/offline.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ export class Offline implements Integration {
8080
});
8181
}
8282

83-
addGlobalEventProcessor((event: Event) => {
83+
const eventProcessor: EventProcessor = event => {
8484
if (this.hub && this.hub.getIntegration(Offline)) {
8585
// cache if we are positively offline
8686
if ('navigator' in this.global && 'onLine' in this.global.navigator && !this.global.navigator.onLine) {
87+
IS_DEBUG_BUILD && logger.log('Event dropped due to being a offline - caching instead');
88+
8789
void this._cacheEvent(event)
8890
.then((_event: Event): Promise<void> => this._enforceMaxEvents())
8991
.catch((_error): void => {
@@ -96,7 +98,10 @@ export class Offline implements Integration {
9698
}
9799

98100
return event;
99-
});
101+
};
102+
103+
eventProcessor.id = this.name;
104+
addGlobalEventProcessor(eventProcessor);
100105

101106
// if online now, send any events stored in a previous offline session
102107
if ('navigator' in this.global && 'onLine' in this.global.navigator && this.global.navigator.onLine) {

packages/nextjs/src/index.client.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { configureScope, init as reactInit, Integrations as BrowserIntegrations } from '@sentry/react';
22
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
3+
import { EventProcessor } from '@sentry/types';
34

45
import { nextRouterInstrumentation } from './performance/client';
56
import { buildMetadata } from './utils/metadata';
@@ -42,9 +43,13 @@ export function init(options: NextjsOptions): void {
4243
...options,
4344
integrations,
4445
});
46+
4547
configureScope(scope => {
4648
scope.setTag('runtime', 'browser');
47-
scope.addEventProcessor(event => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
49+
const filterTransactions: EventProcessor = event =>
50+
event.type === 'transaction' && event.transaction === '/404' ? null : event;
51+
filterTransactions.id = 'NextClient404Filter';
52+
scope.addEventProcessor(filterTransactions);
4853
});
4954
}
5055

packages/nextjs/src/index.server.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Carrier, getHubFromCarrier, getMainCarrier } from '@sentry/hub';
22
import { RewriteFrames } from '@sentry/integrations';
33
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
44
import { hasTracingEnabled } from '@sentry/tracing';
5-
import { Event } from '@sentry/types';
5+
import { EventProcessor } from '@sentry/types';
66
import { escapeStringForRegex, logger } from '@sentry/utils';
77
import * as domainModule from 'domain';
88
import * as path from 'path';
@@ -71,6 +71,12 @@ export function init(options: NextjsOptions): void {
7171

7272
nodeInit(options);
7373

74+
const filterTransactions: EventProcessor = event => {
75+
return event.type === 'transaction' && event.transaction === '/404' ? null : event;
76+
};
77+
78+
filterTransactions.id = 'NextServer404Filter';
79+
7480
configureScope(scope => {
7581
scope.setTag('runtime', 'node');
7682
if (isVercel) {
@@ -131,10 +137,6 @@ function addServerIntegrations(options: NextjsOptions): void {
131137
}
132138
}
133139

134-
function filterTransactions(event: Event): Event | null {
135-
return event.type === 'transaction' && event.transaction === '/404' ? null : event;
136-
}
137-
138140
export type { SentryWebpackPluginOptions } from './config/types';
139141
export { withSentryConfig } from './config';
140142
export { withSentry } from './utils/withSentry';

packages/types/src/eventprocessor.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ import { Event, EventHint } from './event';
66
* Returning a PromiseLike<Event | null> will work just fine, but better be sure that you know what you are doing.
77
* Event processing will be deferred until your Promise is resolved.
88
*/
9-
export type EventProcessor = (event: Event, hint?: EventHint) => PromiseLike<Event | null> | Event | null;
9+
export interface EventProcessor {
10+
id?: string; // This field can't be named "name" because functions already have this field natively
11+
(event: Event, hint?: EventHint): PromiseLike<Event | null> | Event | null;
12+
}

0 commit comments

Comments
 (0)