diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 60a3fe8a4c76..851a2161d0fe 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -185,7 +185,7 @@ 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 = uuid4()); + const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); let finalHint = hint; // If there's no explicit hint provided, mimic the same thing that would happen @@ -216,7 +216,7 @@ export class Hub implements HubInterface { * @inheritDoc */ public captureMessage(message: string, level?: Severity, hint?: EventHint): string { - const eventId = (this._lastEventId = uuid4()); + const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); let finalHint = hint; // If there's no explicit hint provided, mimic the same thing that would happen @@ -247,7 +247,7 @@ export class Hub implements HubInterface { * @inheritDoc */ public captureEvent(event: Event, hint?: EventHint): string { - const eventId = uuid4(); + const eventId = hint && hint.event_id ? hint.event_id : uuid4(); if (event.type !== 'transaction') { this._lastEventId = eventId; } diff --git a/packages/hub/test/hub.test.ts b/packages/hub/test/hub.test.ts index 82c6ab22c987..98c8dcea0636 100644 --- a/packages/hub/test/hub.test.ts +++ b/packages/hub/test/hub.test.ts @@ -222,6 +222,14 @@ describe('Hub', () => { expect(testClient.captureException.mock.calls[0][1].event_id).toBeTruthy(); }); + test('should keep event_id from hint', () => { + const testClient = makeClient(); + const hub = new Hub(testClient); + const id = Math.random().toString(); + hub.captureException('a', { event_id: id }); + expect(testClient.captureException.mock.calls[0][1].event_id).toBe(id); + }); + test('should generate hint if not provided in the call', () => { const testClient = makeClient(); const hub = new Hub(testClient); @@ -248,6 +256,14 @@ describe('Hub', () => { expect(testClient.captureMessage.mock.calls[0][2].event_id).toBeTruthy(); }); + test('should keep event_id from hint', () => { + const testClient = makeClient(); + const hub = new Hub(testClient); + const id = Math.random().toString(); + hub.captureMessage('a', undefined, { event_id: id }); + expect(testClient.captureMessage.mock.calls[0][2].event_id).toBe(id); + }); + test('should generate hint if not provided in the call', () => { const testClient = makeClient(); const hub = new Hub(testClient); @@ -279,6 +295,17 @@ describe('Hub', () => { expect(testClient.captureEvent.mock.calls[0][1].event_id).toBeTruthy(); }); + test('should keep event_id from hint', () => { + const event: Event = { + extra: { b: 3 }, + }; + const testClient = makeClient(); + const hub = new Hub(testClient); + const id = Math.random().toString(); + hub.captureEvent(event, { event_id: id }); + expect(testClient.captureEvent.mock.calls[0][1].event_id).toBe(id); + }); + test('sets lastEventId', () => { const event: Event = { extra: { b: 3 },