2021-04-22 10:07:10 +02:00
|
|
|
import * as responseTime from 'response-time';
|
|
|
|
import EventEmitter from 'events';
|
2021-04-29 10:21:29 +02:00
|
|
|
import { REQUEST_TIME } from '../metric-events';
|
2022-09-30 15:28:50 +02:00
|
|
|
import { IFlagResolver } from '../types/experimental';
|
2023-01-12 11:26:59 +01:00
|
|
|
import { InstanceStatsService } from 'lib/services';
|
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
|
|
|
|
2023-08-04 15:03:21 +02:00
|
|
|
const appNameReportingThreshold = 1000;
|
2023-01-12 11:26:59 +01:00
|
|
|
|
2022-09-30 15:28:50 +02:00
|
|
|
export function responseTimeMetrics(
|
|
|
|
eventBus: EventEmitter,
|
|
|
|
flagResolver: IFlagResolver,
|
2023-01-12 11:26:59 +01:00
|
|
|
instanceStatsService: Pick<InstanceStatsService, 'getAppCountSnapshot'>,
|
2022-09-30 15:28:50 +02:00
|
|
|
): 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
|
|
|
|
2024-01-12 10:25:59 +01:00
|
|
|
let appName: string | undefined;
|
2023-01-12 11:26:59 +01:00
|
|
|
if (
|
2023-02-22 10:10:06 +01:00
|
|
|
!flagResolver.isEnabled('responseTimeWithAppNameKillSwitch') &&
|
2023-02-01 17:30:35 +01:00
|
|
|
(instanceStatsService.getAppCountSnapshot('7d') ??
|
2023-01-12 11:26:59 +01:00
|
|
|
appNameReportingThreshold) < appNameReportingThreshold
|
|
|
|
) {
|
2022-12-16 15:12:36 +01:00
|
|
|
appName = req.headers['unleash-appname'] ?? req.query.appName;
|
2022-09-30 15:28:50 +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,
|
2022-09-30 15:28:50 +02:00
|
|
|
appName,
|
2017-11-16 15:41:33 +01:00
|
|
|
};
|
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
|
|
|
}
|