diff --git a/src/lib/features/frontend-api/client-feature-toggle-read-model.ts b/src/lib/features/frontend-api/client-feature-toggle-read-model.ts index 5d81898da9a9..9a97f3ef83b8 100644 --- a/src/lib/features/frontend-api/client-feature-toggle-read-model.ts +++ b/src/lib/features/frontend-api/client-feature-toggle-read-model.ts @@ -22,7 +22,7 @@ export default class ClientFeatureToggleReadModel constructor(db: Db, eventBus: EventEmitter) { this.db = db; - this.timer = (action) => + this.timer = (action: string) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'client-feature-toggle-read-model', action, diff --git a/src/lib/features/frontend-api/frontend-api-controller.ts b/src/lib/features/frontend-api/frontend-api-controller.ts index de067273150c..62e594709673 100644 --- a/src/lib/features/frontend-api/frontend-api-controller.ts +++ b/src/lib/features/frontend-api/frontend-api-controller.ts @@ -49,7 +49,7 @@ export default class FrontendAPIController extends Controller { this.logger = config.getLogger('frontend-api-controller.ts'); this.services = services; - this.timer = (functionName) => + this.timer = (functionName: string) => metricsHelper.wrapTimer(config.eventBus, FUNCTION_TIME, { className: 'FrontendAPIController', functionName, diff --git a/src/lib/util/metrics-helper.ts b/src/lib/util/metrics-helper.ts index c422bc5dcc5b..b4aa4ebd07b9 100644 --- a/src/lib/util/metrics-helper.ts +++ b/src/lib/util/metrics-helper.ts @@ -1,20 +1,31 @@ -/* eslint-disable no-param-reassign */ +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 be used as a .then() argument. -// It transparently passes the data to the following .then() +// the returned function is designed to stop the timer and emit the event // -// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' })) -const wrapTimer: (EventEmitter, string, object) => (any) => any = ( - eventBus, - event, - args = {}, +// 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 = {}, ) => { const t = timer.new(); - return (data) => { + return (data: unknown) => { args.time = t(); eventBus.emit(event, args); return data;