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

ref(core): Refactor InboundFilters integration to use processEvent #9020

Merged
merged 1 commit into from
Sep 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 9 additions & 17 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
import type { Client, Event, EventHint, Integration, StackFrame } from '@sentry/types';
import { getEventDescription, logger, stringMatchesSomePattern } from '@sentry/utils';

// "Script error." is hard coded into browsers for errors that it can't read.
Expand Down Expand Up @@ -48,23 +48,15 @@ export class InboundFilters implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void {
const eventProcess: EventProcessor = (event: Event) => {
const hub = getCurrentHub();
if (hub) {
const self = hub.getIntegration(InboundFilters);
if (self) {
const client = hub.getClient();
const clientOptions = client ? client.getOptions() : {};
const options = _mergeOptions(self._options, clientOptions);
return _shouldDropEvent(event, options) ? null : event;
}
}
return event;
};
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

eventProcess.id = this.name;
addGlobalEventProcessor(eventProcess);
/** @inheritDoc */
public processEvent(event: Event, _eventHint: EventHint, client: Client): Event | null {
const clientOptions = client.getOptions();
const options = _mergeOptions(this._options, clientOptions);
return _shouldDropEvent(event, options) ? null : event;
}
}

Expand Down
43 changes: 19 additions & 24 deletions packages/core/test/lib/integrations/inboundfilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { Event, EventProcessor } from '@sentry/types';

import type { InboundFiltersOptions } from '../../../src/integrations/inboundfilters';
import { InboundFilters } from '../../../src/integrations/inboundfilters';
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';

const PUBLIC_DSN = 'https://username@domain/123';

/**
* Creates an instance of the InboundFilters integration and returns
Expand All @@ -25,30 +28,22 @@ function createInboundFiltersEventProcessor(
options: Partial<InboundFiltersOptions> = {},
clientOptions: Partial<InboundFiltersOptions> = {},
): EventProcessor {
const eventProcessors: EventProcessor[] = [];
const inboundFiltersInstance = new InboundFilters(options);

function addGlobalEventProcessor(processor: EventProcessor): void {
eventProcessors.push(processor);
expect(eventProcessors).toHaveLength(1);
}

function getCurrentHub(): any {
return {
getIntegration(_integration: any): any {
// pretend integration is enabled
return inboundFiltersInstance;
},
getClient(): any {
return {
getOptions: () => clientOptions,
};
},
};
}

inboundFiltersInstance.setupOnce(addGlobalEventProcessor, getCurrentHub);
return eventProcessors[0];
const client = new TestClient(
getDefaultTestClientOptions({
dsn: PUBLIC_DSN,
...clientOptions,
defaultIntegrations: false,
integrations: [new InboundFilters(options)],
}),
);

client.setupIntegrations();

const eventProcessors = client['_eventProcessors'];
const eventProcessor = eventProcessors.find(processor => processor.id === 'InboundFilters');

expect(eventProcessor).toBeDefined();
return eventProcessor!;
}

// Fixtures
Expand Down