Skip to content

Commit 21531ce

Browse files
authored
feat: Make event processor async (#1431)
1 parent df45f5f commit 21531ce

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

packages/core/src/base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ export abstract class BaseClient<B extends Backend, O extends Options>
275275
// This should be the last thing called, since we want that
276276
// {@link Hub.addEventProcessor} gets the finished prepared event.
277277
if (scope) {
278-
scope.applyToEvent(prepared, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS));
278+
await scope.applyToEvent(
279+
prepared,
280+
Math.min(maxBreadcrumbs, MAX_BREADCRUMBS),
281+
);
279282
}
280283

281284
return prepared;
@@ -334,7 +337,6 @@ export abstract class BaseClient<B extends Backend, O extends Options>
334337
}
335338

336339
// TODO: Handle duplicates and backoffs
337-
338340
if (afterSend) {
339341
afterSend(finalEvent, response);
340342
}

packages/hub/src/hub.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,12 @@ export class Hub {
216216
* This will be called to receive the event
217217
* @param callback will only be called if there is a bound client
218218
*/
219-
public addEventProcessor(callback: () => (event: SentryEvent) => void): void {
219+
public addEventProcessor(
220+
callback: (event: SentryEvent) => Promise<void>,
221+
): void {
220222
const top = this.getStackTop();
221223
if (top.scope && top.client) {
222-
top.scope.addEventProcessor(callback());
224+
top.scope.addEventProcessor(callback);
223225
}
224226
}
225227
}

packages/hub/src/scope.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Scope {
1212
protected scopeListeners: Array<(scope: Scope) => void> = [];
1313

1414
/** Callback list that will be called after {@link applyToEvent}. */
15-
protected eventProcessors: Array<(scope: SentryEvent) => void> = [];
15+
protected eventProcessors: Array<(scope: SentryEvent) => Promise<void>> = [];
1616

1717
/** Array of breadcrumbs. */
1818
protected breadcrumbs: Breadcrumb[] = [];
@@ -35,7 +35,9 @@ export class Scope {
3535
}
3636

3737
/** Add new event processor that will be called after {@link applyToEvent}. */
38-
public addEventProcessor(callback: (scope: SentryEvent) => void): void {
38+
public addEventProcessor(
39+
callback: (scope: SentryEvent) => Promise<void>,
40+
): void {
3941
this.eventProcessors.push(callback);
4042
}
4143

@@ -57,10 +59,11 @@ export class Scope {
5759
/**
5860
* This will be called after {@link applyToEvent} is finished.
5961
*/
60-
protected notifyEventProcessors(event: SentryEvent): void {
61-
this.eventProcessors.forEach(callback => {
62-
callback(event);
63-
});
62+
protected async notifyEventProcessors(event: SentryEvent): Promise<void> {
63+
return this.eventProcessors.reduce(async (prev, callback) => {
64+
await prev;
65+
return callback(event);
66+
}, Promise.resolve());
6467
}
6568

6669
/**
@@ -164,7 +167,10 @@ export class Scope {
164167
* @param event SentryEvent
165168
* @param maxBreadcrumbs number of max breadcrumbs to merged into event.
166169
*/
167-
public applyToEvent(event: SentryEvent, maxBreadcrumbs?: number): void {
170+
public async applyToEvent(
171+
event: SentryEvent,
172+
maxBreadcrumbs?: number,
173+
): Promise<void> {
168174
if (this.extra && Object.keys(this.extra).length) {
169175
event.extra = { ...this.extra, ...event.extra };
170176
}
@@ -189,6 +195,6 @@ export class Scope {
189195
: this.breadcrumbs;
190196
}
191197

192-
this.notifyEventProcessors(event);
198+
await this.notifyEventProcessors(event);
193199
}
194200
}

packages/hub/test/lib/hub.test.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,51 @@ describe('Hub', () => {
226226
});
227227
});
228228

229-
test('addEventProcessor', done => {
229+
test('addEventProcessor', async done => {
230230
expect.assertions(2);
231231
const event: SentryEvent = {
232232
extra: { b: 3 },
233233
};
234234
const localScope = new Scope();
235235
localScope.setExtra('a', 'b');
236236
const hub = new Hub({ a: 'b' }, localScope);
237-
hub.addEventProcessor(() => (processedEvent: SentryEvent) => {
237+
hub.addEventProcessor(async (processedEvent: SentryEvent) => {
238238
expect(processedEvent.extra).toEqual({ a: 'b', b: 3 });
239239
});
240-
hub.addEventProcessor(() => (processedEvent: SentryEvent) => {
240+
hub.addEventProcessor(async (processedEvent: SentryEvent) => {
241241
processedEvent.dist = '1';
242242
});
243-
hub.addEventProcessor(() => (processedEvent: SentryEvent) => {
243+
hub.addEventProcessor(async (processedEvent: SentryEvent) => {
244244
expect(processedEvent.dist).toEqual('1');
245245
done();
246246
});
247-
localScope.applyToEvent(event);
247+
await localScope.applyToEvent(event);
248+
});
249+
250+
test.only('addEventProcessor async', async done => {
251+
expect.assertions(2);
252+
const event: SentryEvent = {
253+
extra: { b: 3 },
254+
};
255+
const localScope = new Scope();
256+
localScope.setExtra('a', 'b');
257+
const hub = new Hub({ a: 'b' }, localScope);
258+
hub.addEventProcessor(async (processedEvent: SentryEvent) => {
259+
expect(processedEvent.extra).toEqual({ a: 'b', b: 3 });
260+
});
261+
hub.addEventProcessor(
262+
async (processedEvent: SentryEvent) =>
263+
new Promise<void>(resolve => {
264+
setImmediate(() => {
265+
processedEvent.dist = '1';
266+
resolve();
267+
});
268+
}),
269+
);
270+
hub.addEventProcessor(async (processedEvent: SentryEvent) => {
271+
expect(processedEvent.dist).toEqual('1');
272+
done();
273+
});
274+
await localScope.applyToEvent(event);
248275
});
249276
});

packages/hub/test/lib/scope.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ describe('Scope', () => {
6868
expect(listener.mock.calls[0][0].extra).toEqual({ a: 2 });
6969
});
7070

71-
test('applyToEvent', () => {
71+
test('applyToEvent', async () => {
7272
const scope = new Scope();
7373
scope.setExtra('a', 2);
7474
scope.setTag('a', 'b');
7575
scope.setUser({ id: '1' });
7676
scope.setFingerprint(['abcd']);
7777
scope.addBreadcrumb({ message: 'test' }, 100);
7878
const event: SentryEvent = {};
79-
scope.applyToEvent(event);
79+
await scope.applyToEvent(event);
8080
expect(event.extra).toEqual({ a: 2 });
8181
expect(event.tags).toEqual({ a: 'b' });
8282
expect(event.user).toEqual({ id: '1' });
8383
expect(event.fingerprint).toEqual(['abcd']);
8484
expect(event.breadcrumbs).toEqual([{ message: 'test' }]);
8585
});
8686

87-
test('applyToEvent merge', () => {
87+
test('applyToEvent merge', async () => {
8888
const scope = new Scope();
8989
scope.setExtra('a', 2);
9090
scope.setTag('a', 'b');
@@ -98,7 +98,7 @@ describe('Scope', () => {
9898
tags: { b: 'c' },
9999
user: { id: '3' },
100100
};
101-
scope.applyToEvent(event);
101+
await scope.applyToEvent(event);
102102
expect(event.extra).toEqual({ a: 2, b: 3 });
103103
expect(event.tags).toEqual({ a: 'b', b: 'c' });
104104
expect(event.user).toEqual({ id: '3' });

0 commit comments

Comments
 (0)