1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/middleware/response-time-metrics.ts

33 lines
963 B
TypeScript
Raw Normal View History

import * as responseTime from 'response-time';
import EventEmitter from 'events';
2021-04-29 10:21:29 +02:00
import { REQUEST_TIME } from '../metric-events';
import { IFlagResolver } from '../types/experimental';
// eslint-disable-next-line @typescript-eslint/naming-convention
const _responseTime = responseTime.default;
export function responseTimeMetrics(
eventBus: EventEmitter,
flagResolver: IFlagResolver,
): any {
return _responseTime((req, res, time) => {
const { statusCode } = res;
const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)';
let appName;
if (flagResolver.isEnabled('responseTimeWithAppName')) {
appName = req.headers['unleash-appname'] ?? req.query.appName;
}
const timingInfo = {
2017-12-18 15:12:44 +01:00
path: pathname,
method: req.method,
statusCode,
time,
appName,
};
eventBus.emit(REQUEST_TIME, timingInfo);
});
}