Skip to content

ref(integrations): Do not use event processor for Debug integration #9014

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

Merged
merged 1 commit into from
Sep 13, 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
22 changes: 11 additions & 11 deletions packages/integrations/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,32 @@ export class Debug implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((event: Event, hint: EventHint) => {
const self = getCurrentHub().getIntegration(Debug);
if (self) {
if (self._options.debugger) {
public setupOnce(_addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void {
const client = getCurrentHub().getClient();

if (client && client.on) {
client.on('beforeSendEvent', (event: Event, hint?: EventHint) => {
if (this._options.debugger) {
// eslint-disable-next-line no-debugger
debugger;
}

/* eslint-disable no-console */
consoleSandbox(() => {
if (self._options.stringify) {
if (this._options.stringify) {
console.log(JSON.stringify(event, null, 2));
if (Object.keys(hint).length) {
if (hint && Object.keys(hint).length) {
console.log(JSON.stringify(hint, null, 2));
}
} else {
console.log(event);
if (Object.keys(hint).length) {
if (hint && Object.keys(hint).length) {
console.log(hint);
}
}
});
/* eslint-enable no-console */
}
return event;
});
});
}
}
}
83 changes: 48 additions & 35 deletions packages/integrations/test/debug.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import type { EventProcessor, Integration } from '@sentry/types';
import type { Client, Event, EventHint, Hub, Integration } from '@sentry/types';

import { Debug } from '../src/debug';

const mockGetCurrentHub = (getIntegrationResult: Integration) => ({
getIntegration: jest.fn(() => getIntegrationResult),
});
function testEventLogged(integration: Integration, testEvent?: Event, testEventHint?: EventHint) {
const callbacks: ((event: Event, hint?: EventHint) => void)[] = [];

const client: Client = {
on(hook: string, callback: (event: Event, hint?: EventHint) => void) {
expect(hook).toEqual('beforeSendEvent');
callbacks.push(callback);
},
} as Client;

function getCurrentHub() {
return {
getClient: jest.fn(() => {
return client;
}),
} as unknown as Hub;
}

integration.setupOnce(() => {}, getCurrentHub);

expect(callbacks.length).toEqual(1);

if (testEvent) {
callbacks[0](testEvent, testEventHint);
}
}

// Replace console log with a mock so we can check for invocations
const mockConsoleLog = jest.fn();
Expand All @@ -24,56 +47,46 @@ describe('Debug integration setup should register an event processor that', () =

it('logs an event', () => {
const debugIntegration = new Debug();
const testEvent = { event_id: 'some event' };

const captureEventProcessor = (eventProcessor: EventProcessor) => {
const testEvent = { event_id: 'some event' };
void eventProcessor(testEvent, {});
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(testEvent);
};
testEventLogged(debugIntegration, testEvent);

debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(testEvent);
});

it('logs an event hint if available', () => {
const debugIntegration = new Debug();

const captureEventProcessor = (eventProcessor: EventProcessor) => {
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
void eventProcessor(testEvent, testEventHint);
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(testEvent);
expect(mockConsoleLog).toBeCalledWith(testEventHint);
};
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };

debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
testEventLogged(debugIntegration, testEvent, testEventHint);

expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(testEvent);
expect(mockConsoleLog).toBeCalledWith(testEventHint);
});

it('logs events in stringified format when `stringify` option was set', () => {
const debugIntegration = new Debug({ stringify: true });
const testEvent = { event_id: 'some event' };

const captureEventProcessor = (eventProcessor: EventProcessor) => {
const testEvent = { event_id: 'some event' };
void eventProcessor(testEvent, {});
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2));
};
testEventLogged(debugIntegration, testEvent);

debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2));
});

it('logs event hints in stringified format when `stringify` option was set', () => {
const debugIntegration = new Debug({ stringify: true });

const captureEventProcessor = (eventProcessor: EventProcessor) => {
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
void eventProcessor(testEvent, testEventHint);
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
};
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };

testEventLogged(debugIntegration, testEvent, testEventHint);

debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
});
});