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

fix(core): Only generate eventIds in client #6247

Merged
merged 4 commits into from Nov 22, 2022
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
7 changes: 3 additions & 4 deletions packages/core/src/baseclient.ts
Expand Up @@ -127,8 +127,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
return;
}

let eventId: string | undefined = hint && hint.event_id;

let eventId: string | undefined;
this._process(
this.eventFromException(exception, hint)
.then(event => this._captureEvent(event, hint, scope))
Expand All @@ -150,7 +149,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
hint?: EventHint,
scope?: Scope,
): string | undefined {
let eventId: string | undefined = hint && hint.event_id;
let eventId: string | undefined;

const promisedEvent = isPrimitive(message)
? this.eventFromMessage(String(message), level, hint)
Expand All @@ -177,7 +176,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
return;
}

let eventId: string | undefined = hint && hint.event_id;
let eventId: string | undefined;

this._process(
this._captureEvent(event, hint, scope).then(result => {
Expand Down
78 changes: 38 additions & 40 deletions packages/core/src/hub.ts
Expand Up @@ -27,12 +27,13 @@ import {
GLOBAL_OBJ,
isNodeEnv,
logger,
uuid4,
} from '@sentry/utils';

import { Scope } from './scope';
import { closeSession, makeSession, updateSession } from './session';

const NIL_EVENT_ID = '00000000000000000000000000000000';

/**
* API compatibility version of this hub.
*
Expand Down Expand Up @@ -184,21 +185,20 @@ export class Hub implements HubInterface {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
public captureException(exception: any, hint?: EventHint): string {
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
const syntheticException = new Error('Sentry syntheticException');
this._withClient((client, scope) => {
client.captureException(
exception,
{
originalException: exception,
syntheticException,
...hint,
event_id: eventId,
},
scope,
);
});
return eventId;
this._lastEventId =
this._withClient((client, scope) => {
return client.captureException(
exception,
{
originalException: exception,
syntheticException,
...hint,
},
scope,
);
}) || NIL_EVENT_ID;
return this._lastEventId;
}

/**
Expand All @@ -210,37 +210,37 @@ export class Hub implements HubInterface {
level?: Severity | SeverityLevel,
hint?: EventHint,
): string {
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
const syntheticException = new Error(message);
this._withClient((client, scope) => {
client.captureMessage(
message,
level,
{
originalException: message,
syntheticException,
...hint,
event_id: eventId,
},
scope,
);
});
return eventId;
this._lastEventId =
this._withClient((client, scope) => {
return client.captureMessage(
message,
level,
{
originalException: message,
syntheticException,
...hint,
},
scope,
);
}) || NIL_EVENT_ID;
return this._lastEventId;
}

/**
* @inheritDoc
*/
public captureEvent(event: Event, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
const clientId =
this._withClient((client, scope) => {
return client.captureEvent(event, { ...hint }, scope);
}) || NIL_EVENT_ID;

if (event.type !== 'transaction') {
this._lastEventId = eventId;
this._lastEventId = clientId;
}

this._withClient((client, scope) => {
client.captureEvent(event, { ...hint, event_id: eventId }, scope);
});
return eventId;
return clientId;
}

/**
Expand Down Expand Up @@ -469,11 +469,9 @@ export class Hub implements HubInterface {
* @param method The method to call on the client.
* @param args Arguments to pass to the client function.
*/
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
private _withClient<T>(callback: (client: Client, scope: Scope | undefined) => T): T | undefined {
const { scope, client } = this.getStackTop();
if (client) {
callback(client, scope);
}
return client && callback(client, scope);
}

/**
Expand Down
40 changes: 16 additions & 24 deletions packages/hub/test/hub.test.ts
Expand Up @@ -7,11 +7,13 @@ import { getCurrentHub, Hub, Scope } from '../src';

const clientFn: any = jest.fn();

const MOCK_EVENT_ID = '7bab5137428b4de29891fb8bd34a31cb';

function makeClient() {
return {
getOptions: jest.fn(),
captureEvent: jest.fn(),
captureException: jest.fn(),
captureException: jest.fn().mockReturnValue(MOCK_EVENT_ID),
Copy link
Member

Choose a reason for hiding this comment

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

Where are we using this value? Couldn't find usage in the changed tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

MOCK_EVENT_ID is what hub.lastEventId() is being set as. It's not being directly asserted on, just used as a return value from captureException (so we can differentiate between undefined)

close: jest.fn(),
flush: jest.fn(),
getDsn: jest.fn(),
Expand Down Expand Up @@ -224,14 +226,12 @@ describe('Hub', () => {
expect(args[0]).toBe('a');
});

test('should set event_id in hint', () => {
test('should get event_id from client', () => {
const testClient = makeClient();
const hub = new Hub(testClient);

hub.captureException('a');
const args = getPassedArgs(testClient.captureException);

expect(args[1].event_id).toBeTruthy();
const id = hub.captureException('a');
expect(id).toBeDefined();
});

test('should keep event_id from hint', () => {
Expand Down Expand Up @@ -270,14 +270,12 @@ describe('Hub', () => {
expect(args[0]).toBe('a');
});

test('should set event_id in hint', () => {
test('should get event_id from client', () => {
const testClient = makeClient();
const hub = new Hub(testClient);

hub.captureMessage('a');
const args = getPassedArgs(testClient.captureMessage);

expect(args[2].event_id).toBeTruthy();
const id = hub.captureMessage('a');
expect(id).toBeDefined();
});

test('should keep event_id from hint', () => {
Expand Down Expand Up @@ -318,17 +316,15 @@ describe('Hub', () => {
expect(args[0]).toBe(event);
});

test('should set event_id in hint', () => {
test('should get event_id from client', () => {
const event: Event = {
extra: { b: 3 },
};
const testClient = makeClient();
const hub = new Hub(testClient);

hub.captureEvent(event);
const args = getPassedArgs(testClient.captureEvent);

expect(args[1].event_id).toBeTruthy();
const id = hub.captureEvent(event);
expect(id).toBeDefined();
});

test('should keep event_id from hint', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Is this ("from hint") still correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup it is

Expand All @@ -352,10 +348,8 @@ describe('Hub', () => {
const testClient = makeClient();
const hub = new Hub(testClient);

hub.captureEvent(event);
const args = getPassedArgs(testClient.captureEvent);

expect(args[1].event_id).toEqual(hub.lastEventId());
const id = hub.captureEvent(event);
expect(id).toEqual(hub.lastEventId());
});

test('transactions do not set lastEventId', () => {
Expand All @@ -366,10 +360,8 @@ describe('Hub', () => {
const testClient = makeClient();
const hub = new Hub(testClient);

hub.captureEvent(event);
const args = getPassedArgs(testClient.captureEvent);

expect(args[1].event_id).not.toEqual(hub.lastEventId());
const id = hub.captureEvent(event);
expect(id).not.toEqual(hub.lastEventId());
});
});

Expand Down