-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathmetrics-helper.ts
41 lines (38 loc) · 1.04 KB
/
metrics-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type EventEmitter from 'events';
import timer from './timer';
// wrapTimer keeps track of the timing of a async operation and emits
// a event on the given eventBus once the operation is complete
//
// the returned function is designed to stop the timer and emit the event
//
// Usage:
// Define the timer function. It can be done once per class.
// this.timer = (action: string) =>
// metricsHelper.wrapTimer(eventBus, DB_TIME, {
// store: 'client-feature-toggle-read-model',
// action,
// });
//
// Before performing an operation, start the timer:
// const stopTimer = this.timer(`timer-name`);
// // perform operation and then stop timer
// stopTimer();
const wrapTimer = (
eventBus: EventEmitter,
event: string,
args: Record<string, unknown> = {},
) => {
const t = timer.new();
return (data: unknown) => {
args.time = t();
eventBus.emit(event, args);
return data;
};
};
const metricsHelper = {
wrapTimer,
};
export default metricsHelper;
module.exports = {
wrapTimer,
};