Skip to content
Snippets Groups Projects
Commit 31a1c116 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

feat(github): dump request stats per-repo

Closes #1516
parent c13c4986
Branches
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ const parseLinkHeader = require('parse-link-header'); ...@@ -5,6 +5,7 @@ const parseLinkHeader = require('parse-link-header');
const endpoints = require('../../util/endpoints'); const endpoints = require('../../util/endpoints');
let cache = {}; let cache = {};
let stats = {};
async function get(path, options, retries = 5) { async function get(path, options, retries = 5) {
const { host } = URL.parse(path); const { host } = URL.parse(path);
...@@ -19,6 +20,7 @@ async function get(path, options, retries = 5) { ...@@ -19,6 +20,7 @@ async function get(path, options, retries = 5) {
return cache[path]; return cache[path];
} }
logger.debug(`${method.toUpperCase()} ${path}`); logger.debug(`${method.toUpperCase()} ${path}`);
stats.requestCount = (stats.requestCount || 0) + 1;
try { try {
if (appMode) { if (appMode) {
const appAccept = 'application/vnd.github.machine-man-preview+json'; const appAccept = 'application/vnd.github.machine-man-preview+json';
...@@ -37,6 +39,10 @@ async function get(path, options, retries = 5) { ...@@ -37,6 +39,10 @@ async function get(path, options, retries = 5) {
} }
} }
const res = await ghGot(path, opts); const res = await ghGot(path, opts);
if (res && res.headers) {
stats.rateLimit = res.headers['x-ratelimit-limit'];
stats.rateLimitRemaining = res.headers['x-ratelimit-remaining'];
}
if (opts.paginate) { if (opts.paginate) {
// Check if result is paginated // Check if result is paginated
const pageLimit = opts.pageLimit || 10; const pageLimit = opts.pageLimit || 10;
...@@ -181,6 +187,11 @@ get.setAppMode = function setAppMode(val) { ...@@ -181,6 +187,11 @@ get.setAppMode = function setAppMode(val) {
get.reset = function reset() { get.reset = function reset() {
cache = null; cache = null;
cache = {}; cache = {};
// istanbul ignore if
if (stats.requestCount) {
logger.info({ stats }, 'Request stats');
}
stats = {};
}; };
module.exports = get; module.exports = get;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment