feat: log outstandingRequests

This commit is contained in:
Rhys Arkins 2020-03-12 21:41:51 +01:00
parent eab96b2d6e
commit eee910a512

View file

@ -10,9 +10,12 @@ interface HostStats {
let stats: Record<string, number[]> = {}; let stats: Record<string, number[]> = {};
let outstandingRequests = {};
// istanbul ignore next // istanbul ignore next
export const resetStats = (): void => { export const resetStats = (): void => {
stats = {}; stats = {};
outstandingRequests = {};
}; };
// istanbul ignore next // istanbul ignore next
@ -30,15 +33,22 @@ export const printStats = (): void => {
res.median = entries[Math.floor(entries.length / 2)]; res.median = entries[Math.floor(entries.length / 2)];
hostStats[hostname] = res; hostStats[hostname] = res;
} }
logger.debug({ hostStats }, 'Host request stats (milliseconds)'); logger.debug(
{ hostStats, outstandingRequests },
'Host request stats (milliseconds)'
);
}; };
export const instance = create({ export const instance = create({
options: {}, options: {},
handler: (options, next) => { handler: (options, next) => {
const request = `${options.method} ${options.href}`;
logger.info(request);
outstandingRequests[request] = true;
const start = new Date(); const start = new Date();
const nextPromise = next(options); const nextPromise = next(options);
nextPromise.on('response', () => { nextPromise.on('response', () => {
delete outstandingRequests[request];
const elapsed = new Date().getTime() - start.getTime(); const elapsed = new Date().getTime() - start.getTime();
stats[options.hostname] = stats[options.hostname] || []; stats[options.hostname] = stats[options.hostname] || [];
stats[options.hostname].push(elapsed); stats[options.hostname].push(elapsed);