Skip to content

Commit 3fe5f7a

Browse files
authored
fix(core): Only generate eventIds in client (#6247)
This changes moves all generation of eventId uuids to the client. Since we can't change the signature of `hub.captureException` and similar, we return a event id of zeros, `00000000000000000000000000000000`, if the client method returns undefined.
1 parent 72c6855 commit 3fe5f7a

File tree

3 files changed

+57
-68
lines changed

3 files changed

+57
-68
lines changed

packages/core/src/baseclient.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
127127
return;
128128
}
129129

130-
let eventId: string | undefined = hint && hint.event_id;
131-
130+
let eventId: string | undefined;
132131
this._process(
133132
this.eventFromException(exception, hint)
134133
.then(event => this._captureEvent(event, hint, scope))
@@ -150,7 +149,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
150149
hint?: EventHint,
151150
scope?: Scope,
152151
): string | undefined {
153-
let eventId: string | undefined = hint && hint.event_id;
152+
let eventId: string | undefined;
154153

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

180-
let eventId: string | undefined = hint && hint.event_id;
179+
let eventId: string | undefined;
181180

182181
this._process(
183182
this._captureEvent(event, hint, scope).then(result => {

packages/core/src/hub.ts

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ import {
2727
GLOBAL_OBJ,
2828
isNodeEnv,
2929
logger,
30-
uuid4,
3130
} from '@sentry/utils';
3231

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

35+
const NIL_EVENT_ID = '00000000000000000000000000000000';
36+
3637
/**
3738
* API compatibility version of this hub.
3839
*
@@ -184,21 +185,20 @@ export class Hub implements HubInterface {
184185
*/
185186
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
186187
public captureException(exception: any, hint?: EventHint): string {
187-
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
188188
const syntheticException = new Error('Sentry syntheticException');
189-
this._withClient((client, scope) => {
190-
client.captureException(
191-
exception,
192-
{
193-
originalException: exception,
194-
syntheticException,
195-
...hint,
196-
event_id: eventId,
197-
},
198-
scope,
199-
);
200-
});
201-
return eventId;
189+
this._lastEventId =
190+
this._withClient((client, scope) => {
191+
return client.captureException(
192+
exception,
193+
{
194+
originalException: exception,
195+
syntheticException,
196+
...hint,
197+
},
198+
scope,
199+
);
200+
}) || NIL_EVENT_ID;
201+
return this._lastEventId;
202202
}
203203

204204
/**
@@ -210,37 +210,37 @@ export class Hub implements HubInterface {
210210
level?: Severity | SeverityLevel,
211211
hint?: EventHint,
212212
): string {
213-
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
214213
const syntheticException = new Error(message);
215-
this._withClient((client, scope) => {
216-
client.captureMessage(
217-
message,
218-
level,
219-
{
220-
originalException: message,
221-
syntheticException,
222-
...hint,
223-
event_id: eventId,
224-
},
225-
scope,
226-
);
227-
});
228-
return eventId;
214+
this._lastEventId =
215+
this._withClient((client, scope) => {
216+
return client.captureMessage(
217+
message,
218+
level,
219+
{
220+
originalException: message,
221+
syntheticException,
222+
...hint,
223+
},
224+
scope,
225+
);
226+
}) || NIL_EVENT_ID;
227+
return this._lastEventId;
229228
}
230229

231230
/**
232231
* @inheritDoc
233232
*/
234233
public captureEvent(event: Event, hint?: EventHint): string {
235-
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
234+
const clientId =
235+
this._withClient((client, scope) => {
236+
return client.captureEvent(event, { ...hint }, scope);
237+
}) || NIL_EVENT_ID;
238+
236239
if (event.type !== 'transaction') {
237-
this._lastEventId = eventId;
240+
this._lastEventId = clientId;
238241
}
239242

240-
this._withClient((client, scope) => {
241-
client.captureEvent(event, { ...hint, event_id: eventId }, scope);
242-
});
243-
return eventId;
243+
return clientId;
244244
}
245245

246246
/**
@@ -469,11 +469,9 @@ export class Hub implements HubInterface {
469469
* @param method The method to call on the client.
470470
* @param args Arguments to pass to the client function.
471471
*/
472-
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
472+
private _withClient<T>(callback: (client: Client, scope: Scope | undefined) => T): T | undefined {
473473
const { scope, client } = this.getStackTop();
474-
if (client) {
475-
callback(client, scope);
476-
}
474+
return client && callback(client, scope);
477475
}
478476

479477
/**

packages/hub/test/hub.test.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { getCurrentHub, Hub, Scope } from '../src';
77

88
const clientFn: any = jest.fn();
99

10+
const MOCK_EVENT_ID = '7bab5137428b4de29891fb8bd34a31cb';
11+
1012
function makeClient() {
1113
return {
1214
getOptions: jest.fn(),
1315
captureEvent: jest.fn(),
14-
captureException: jest.fn(),
16+
captureException: jest.fn().mockReturnValue(MOCK_EVENT_ID),
1517
close: jest.fn(),
1618
flush: jest.fn(),
1719
getDsn: jest.fn(),
@@ -224,14 +226,12 @@ describe('Hub', () => {
224226
expect(args[0]).toBe('a');
225227
});
226228

227-
test('should set event_id in hint', () => {
229+
test('should get event_id from client', () => {
228230
const testClient = makeClient();
229231
const hub = new Hub(testClient);
230232

231-
hub.captureException('a');
232-
const args = getPassedArgs(testClient.captureException);
233-
234-
expect(args[1].event_id).toBeTruthy();
233+
const id = hub.captureException('a');
234+
expect(id).toBeDefined();
235235
});
236236

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

273-
test('should set event_id in hint', () => {
273+
test('should get event_id from client', () => {
274274
const testClient = makeClient();
275275
const hub = new Hub(testClient);
276276

277-
hub.captureMessage('a');
278-
const args = getPassedArgs(testClient.captureMessage);
279-
280-
expect(args[2].event_id).toBeTruthy();
277+
const id = hub.captureMessage('a');
278+
expect(id).toBeDefined();
281279
});
282280

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

321-
test('should set event_id in hint', () => {
319+
test('should get event_id from client', () => {
322320
const event: Event = {
323321
extra: { b: 3 },
324322
};
325323
const testClient = makeClient();
326324
const hub = new Hub(testClient);
327325

328-
hub.captureEvent(event);
329-
const args = getPassedArgs(testClient.captureEvent);
330-
331-
expect(args[1].event_id).toBeTruthy();
326+
const id = hub.captureEvent(event);
327+
expect(id).toBeDefined();
332328
});
333329

334330
test('should keep event_id from hint', () => {
@@ -352,10 +348,8 @@ describe('Hub', () => {
352348
const testClient = makeClient();
353349
const hub = new Hub(testClient);
354350

355-
hub.captureEvent(event);
356-
const args = getPassedArgs(testClient.captureEvent);
357-
358-
expect(args[1].event_id).toEqual(hub.lastEventId());
351+
const id = hub.captureEvent(event);
352+
expect(id).toEqual(hub.lastEventId());
359353
});
360354

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

369-
hub.captureEvent(event);
370-
const args = getPassedArgs(testClient.captureEvent);
371-
372-
expect(args[1].event_id).not.toEqual(hub.lastEventId());
363+
const id = hub.captureEvent(event);
364+
expect(id).not.toEqual(hub.lastEventId());
373365
});
374366
});
375367

0 commit comments

Comments
 (0)