Skip to content
Snippets Groups Projects
Unverified Commit aa38e6af authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

feat: host stats (#3812)

Collects latency stats for hosts and dumps them at the end of each run.
parent f11a1f54
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,10 @@ const clone = input => JSON.parse(JSON.stringify(input));
module.exports = got.create({
options: {},
handler: (options, next) => {
// istanbul ignore if
if (!global.repoCache) {
return next(options);
}
if (options.method === 'GET') {
const cacheKey = crypto
.createHash('md5')
......
......@@ -3,6 +3,7 @@ const cacheGet = require('./cache-get');
const renovateAgent = require('./renovate-agent');
const hostRules = require('./host-rules');
const auth = require('./auth');
const stats = require('./stats');
/*
* This is the default got instance for Renovate.
......@@ -12,4 +13,10 @@ const auth = require('./auth');
* Important: always put the renovateAgent one last, to make sure the correct user agent is used
*/
module.exports = got.mergeInstances(cacheGet, renovateAgent, hostRules, auth);
module.exports = got.mergeInstances(
cacheGet,
renovateAgent,
hostRules,
auth,
stats.instance
);
const got = require('got');
let stats = {};
// istanbul ignore next
module.exports.resetStats = () => {
stats = {};
};
// istanbul ignore next
module.exports.printStats = () => {
logger.trace({ stats }, 'Host transfer stats (milliseconds)');
const hostStats = {};
for (const [hostname, entries] of Object.entries(stats)) {
const res = {};
res.requests = entries.length;
res.sum = 0;
entries.forEach(entry => {
res.sum += entry;
});
res.average = Math.round(res.sum / res.requests);
res.median = entries[Math.floor(entries.length / 2)];
hostStats[hostname] = res;
}
logger.debug({ hostStats }, 'Host request stats (milliseconds)');
};
module.exports.instance = got.create({
options: {},
handler: (options, next) => {
const start = new Date();
const nextPromise = next(options);
nextPromise.on('response', () => {
const elapsed = new Date() - start;
stats[options.hostname] = stats[options.hostname] || [];
stats[options.hostname].push(elapsed);
});
return nextPromise;
},
});
......@@ -10,6 +10,7 @@ const { appName } = require('../../config/app-strings');
const { autodiscoverRepositories } = require('./autodiscover');
const { initPlatform } = require('../../platform');
const hostRules = require('../../util/host-rules');
const { printStats } = require('../../util/got/stats');
module.exports = {
start,
......@@ -68,6 +69,7 @@ async function start() {
await repositoryWorker.renovateRepository(repoConfig);
}
logger.setMeta({});
printStats();
logger.info(`${appName} finished`);
} catch (err) /* istanbul ignore next */ {
if (err.message.startsWith('Init: ')) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment