2021-04-22 10:07:10 +02:00
|
|
|
import * as responseTime from 'response-time';
|
|
|
|
import EventEmitter from 'events';
|
2021-02-16 14:30:08 +01:00
|
|
|
import { REQUEST_TIME } from '../events';
|
2017-11-16 15:41:33 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
const _responseTime = responseTime.default;
|
2017-11-16 15:41:33 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export function responseTimeMetrics(eventBus: EventEmitter): any {
|
2021-02-16 14:30:08 +01:00
|
|
|
return _responseTime((req, res, time) => {
|
2020-04-30 23:04:06 +02:00
|
|
|
const { statusCode } = res;
|
2021-02-15 13:04:56 +01:00
|
|
|
|
|
|
|
const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)';
|
2020-04-30 23:04:06 +02:00
|
|
|
|
2017-11-16 15:41:33 +01:00
|
|
|
const timingInfo = {
|
2017-12-18 15:12:44 +01:00
|
|
|
path: pathname,
|
2017-11-16 15:41:33 +01:00
|
|
|
method: req.method,
|
2020-04-30 23:04:06 +02:00
|
|
|
statusCode,
|
2017-11-16 15:41:33 +01:00
|
|
|
time,
|
|
|
|
};
|
2021-04-22 10:07:10 +02:00
|
|
|
eventBus.emit(REQUEST_TIME, timingInfo);
|
2017-11-16 15:41:33 +01:00
|
|
|
});
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|