Skip to content

Commit 62375f9

Browse files
authored
Revert "fix(core): Only generate eventIds in client (#6247)"
This reverts commit 3fe5f7a.
1 parent f09ec36 commit 62375f9

File tree

3 files changed

+68
-57
lines changed

3 files changed

+68
-57
lines changed

packages/core/src/baseclient.ts

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

130-
let eventId: string | undefined;
130+
let eventId: string | undefined = hint && hint.event_id;
131+
131132
this._process(
132133
this.eventFromException(exception, hint)
133134
.then(event => this._captureEvent(event, hint, scope))
@@ -149,7 +150,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
149150
hint?: EventHint,
150151
scope?: Scope,
151152
): string | undefined {
152-
let eventId: string | undefined;
153+
let eventId: string | undefined = hint && hint.event_id;
153154

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

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

181182
this._process(
182183
this._captureEvent(event, hint, scope).then(result => {

packages/core/src/hub.ts

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

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

35-
const NIL_EVENT_ID = '00000000000000000000000000000000';
36-
3736
/**
3837
* API compatibility version of this hub.
3938
*
@@ -185,20 +184,21 @@ export class Hub implements HubInterface {
185184
*/
186185
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
187186
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._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;
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;
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());
213214
const syntheticException = new Error(message);
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;
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;
228229
}
229230

230231
/**
231232
* @inheritDoc
232233
*/
233234
public captureEvent(event: Event, hint?: EventHint): string {
234-
const clientId =
235-
this._withClient((client, scope) => {
236-
return client.captureEvent(event, { ...hint }, scope);
237-
}) || NIL_EVENT_ID;
238-
235+
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
239236
if (event.type !== 'transaction') {
240-
this._lastEventId = clientId;
237+
this._lastEventId = eventId;
241238
}
242239

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

246246
/**
@@ -469,9 +469,11 @@ 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<T>(callback: (client: Client, scope: Scope | undefined) => T): T | undefined {
472+
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
473473
const { scope, client } = this.getStackTop();
474-
return client && callback(client, scope);
474+
if (client) {
475+
callback(client, scope);
476+
}
475477
}
476478

477479
/**

packages/hub/test/hub.test.ts

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

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

10-
const MOCK_EVENT_ID = '7bab5137428b4de29891fb8bd34a31cb';
11-
1210
function makeClient() {
1311
return {
1412
getOptions: jest.fn(),
1513
captureEvent: jest.fn(),
16-
captureException: jest.fn().mockReturnValue(MOCK_EVENT_ID),
14+
captureException: jest.fn(),
1715
close: jest.fn(),
1816
flush: jest.fn(),
1917
getDsn: jest.fn(),
@@ -226,12 +224,14 @@ describe('Hub', () => {
226224
expect(args[0]).toBe('a');
227225
});
228226

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

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

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

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

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

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

319-
test('should get event_id from client', () => {
321+
test('should set event_id in hint', () => {
320322
const event: Event = {
321323
extra: { b: 3 },
322324
};
323325
const testClient = makeClient();
324326
const hub = new Hub(testClient);
325327

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

330334
test('should keep event_id from hint', () => {
@@ -348,8 +352,10 @@ describe('Hub', () => {
348352
const testClient = makeClient();
349353
const hub = new Hub(testClient);
350354

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

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

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

0 commit comments

Comments
 (0)