feat: debug info for hot route and path

This commit is contained in:
DIYgod
2019-06-19 18:39:40 +08:00
parent 638c613f0b
commit 7c2f4777af
4 changed files with 24 additions and 4 deletions

View File

@@ -57,6 +57,7 @@ if (cluster.isMaster && !config.disableCluster && process.env.NODE_ENV !== 'test
app.context.debug = { app.context.debug = {
hitCache: 0, hitCache: 0,
request: 0, request: 0,
paths: [],
routes: [], routes: [],
ips: [], ips: [],
}; };

View File

@@ -1,8 +1,8 @@
module.exports = async (ctx, next) => { module.exports = async (ctx, next) => {
if (!ctx.debug.routes[ctx.request.path]) { if (!ctx.debug.paths[ctx.request.path]) {
ctx.debug.routes[ctx.request.path] = 0; ctx.debug.paths[ctx.request.path] = 0;
} }
ctx.debug.routes[ctx.request.path]++; ctx.debug.paths[ctx.request.path]++;
const ip = ctx.ips[0] || ctx.ip; const ip = ctx.ips[0] || ctx.ip;
if (!ctx.debug.ips[ip]) { if (!ctx.debug.ips[ip]) {
@@ -13,6 +13,11 @@ module.exports = async (ctx, next) => {
await next(); await next();
if (!ctx.debug.routes[ctx._matchedRoute]) {
ctx.debug.routes[ctx._matchedRoute] = 0;
}
ctx.debug.routes[ctx._matchedRoute]++;
if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) {
ctx.debug.hitCache++; ctx.debug.hitCache++;
} }

View File

@@ -16,12 +16,19 @@ module.exports = async (ctx) => {
}); });
const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]); const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]);
const hotRoutes = routes.slice(0, 50); const hotRoutes = routes.slice(0, 30);
let hotRoutesValue = ''; let hotRoutesValue = '';
hotRoutes.forEach((item) => { hotRoutes.forEach((item) => {
hotRoutesValue += `${ctx.debug.routes[item]}&nbsp;&nbsp;${item}<br>`; hotRoutesValue += `${ctx.debug.routes[item]}&nbsp;&nbsp;${item}<br>`;
}); });
const paths = Object.keys(ctx.debug.paths).sort((a, b) => ctx.debug.paths[b] - ctx.debug.paths[a]);
const hotPaths = paths.slice(0, 30);
let hotPathsValue = '';
hotPaths.forEach((item) => {
hotPathsValue += `${ctx.debug.paths[item]}&nbsp;&nbsp;${item}<br>`;
});
const ips = Object.keys(ctx.debug.ips).sort((a, b) => ctx.debug.ips[b] - ctx.debug.ips[a]); const ips = Object.keys(ctx.debug.ips).sort((a, b) => ctx.debug.ips[b] - ctx.debug.ips[a]);
const hotIPs = ips.slice(0, 50); const hotIPs = ips.slice(0, 50);
let hotIPsValue = ''; let hotIPsValue = '';
@@ -76,6 +83,10 @@ module.exports = async (ctx) => {
name: '热门路由', name: '热门路由',
value: hotRoutesValue, value: hotRoutesValue,
}, },
{
name: '热门路径',
value: hotPathsValue,
},
{ {
name: '热门IP', name: '热门IP',
value: hotIPsValue, value: hotIPsValue,

View File

@@ -41,6 +41,9 @@ describe('debug', () => {
expect(value).toBe('6'); expect(value).toBe('6');
break; break;
case '热门路由:': case '热门路由:':
expect(value).toBe(`5&nbsp;&nbsp;/test/:id<br>`);
break;
case '热门路径:':
expect(value).toBe(`3&nbsp;&nbsp;/test/1<br>2&nbsp;&nbsp;/test/2<br>1&nbsp;&nbsp;/<br>`); expect(value).toBe(`3&nbsp;&nbsp;/test/1<br>2&nbsp;&nbsp;/test/2<br>1&nbsp;&nbsp;/<br>`);
break; break;
case '热门IP:': case '热门IP:':