-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic perf time, udpate fastify version, support log.addFie…
…ld, better fatal log
- Loading branch information
Showing
21 changed files
with
207 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"watch": ["*.ts", "*.tpl"], | ||
"watch": ["*.ts", "src/*.tpl"], | ||
"ignore": ["*.test.ts", "node_modules/*", "*.d.ts"], | ||
"ext": "ts,js,tpl", | ||
"exec": "npm run build && NODE_ENV=development hoth start --app-dir='dist' --app-name='hoth-quickstart' --app-prefix='/hoth-quickstart'", | ||
"exec": "npm run build && NODE_ENV=development node ../../node_modules/.bin/hoth start --app-dir='dist' --app-name='hoth-quickstart' --app-prefix='/hoth-quickstart'", | ||
"legacyWatch": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {performance} from 'perf_hooks'; | ||
import {FastifyReply, FastifyRequest} from 'fastify'; | ||
import {reqStartTimeSym} from './onRequestFactory'; | ||
import {parseStartTimeSym} from './preParsing'; | ||
import {validateStartTimeSym} from './preValidation'; | ||
import {handlerStartTimeSym} from './preHandlerFactory'; | ||
import {serializationTimeSym} from './preSerialization'; | ||
import {sendStartTimeSym} from './onSend'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyLoggerInstance { | ||
notice: (...args: any[]) => void; | ||
} | ||
|
||
interface FastifyRequest { | ||
[reqStartTimeSym]: number; | ||
[parseStartTimeSym]: number; | ||
[validateStartTimeSym]: number; | ||
[handlerStartTimeSym]: number; | ||
[serializationTimeSym]: number; | ||
[sendStartTimeSym]: number; | ||
} | ||
} | ||
|
||
// Attention: https://www.fastify.io/docs/v3.19.x/Server/#disablerequestlogging | ||
// 因为禁用disablerequestlogging,onResponse Hook的报错,会被吞掉 | ||
export default async function (req: FastifyRequest, reply: FastifyReply) { | ||
const now = performance.now(); | ||
// 非post,不会执行preParse hook | ||
const parseStartTime = req[parseStartTimeSym] || req[reqStartTimeSym]; | ||
// text响应不会执行preSerialization hook | ||
const serializationStartTime = req[serializationTimeSym] || req[sendStartTimeSym]; | ||
|
||
req.log.addNotice('initTime', (parseStartTime - req[reqStartTimeSym]).toFixed(1)); | ||
req.log.addNotice('parseTime', (req[validateStartTimeSym] - parseStartTime).toFixed(1)); | ||
req.log.addNotice('validateTime', (req[handlerStartTimeSym] - req[validateStartTimeSym]).toFixed(1)); | ||
req.log.addNotice('handleTime', (serializationStartTime - req[handlerStartTimeSym]).toFixed(1)); | ||
req.log.addNotice('serialTime', (req[sendStartTimeSym] - serializationStartTime).toFixed(1)); | ||
req.log.addNotice('sendTime', (now - req[sendStartTimeSym]).toFixed(1)); | ||
|
||
reply.log.notice({ | ||
req, | ||
res: reply, | ||
responseTime: reply.getResponseTime().toFixed(1), | ||
}, 'request completed'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
import {FastifyReply, FastifyRequest} from 'fastify'; | ||
import {performance} from 'perf_hooks'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyLoggerInstance { | ||
notice: (...args: any[]) => void; | ||
} | ||
} | ||
export const sendStartTimeSym = Symbol.for('hoth.send-start-time'); | ||
|
||
export default async function (req: FastifyRequest, reply: FastifyReply) { | ||
const responseTime = reply.getResponseTime(); | ||
reply.log.notice({ | ||
req, | ||
res: reply, | ||
responseTime: responseTime.toFixed(1), | ||
}, 'request completed'); | ||
reply.header('X-Response-Time', responseTime); | ||
const resTime = reply.getResponseTime(); | ||
req[sendStartTimeSym] = performance.now(); | ||
reply.header('X-Response-Time', resTime); | ||
reply.removeHeader('X-Powered-By'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,51 @@ | ||
import {FastifyReply, FastifyRequest} from 'fastify'; | ||
import {performance} from 'perf_hooks'; | ||
import {parseStartTimeSym} from './onRequestFactory'; | ||
import {validateStartTimeSym} from './preValidation'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyRequest { | ||
[validateStartTimeSym]: number; | ||
[parseStartTimeSym]: number; | ||
} | ||
} | ||
export const handlerStartTimeSym = Symbol.for('hoth.handler-start-time'); | ||
|
||
export default function (app: string) { | ||
return async function (req: FastifyRequest, reply: FastifyReply) { | ||
req[handlerStartTimeSym] = performance.now(); | ||
req.log = reply.log = reply.log.child({req, app}); | ||
req.log.addNotice('parseTime', (req[validateStartTimeSym] - req[parseStartTimeSym]).toFixed(1)); | ||
req.log.addNotice('validationTime', (performance.now() - req[validateStartTimeSym]).toFixed(1)); | ||
|
||
// 业务需要 req.log.fatal({req, err}, 'msg'); 才能触发serializers对应的req序列化,才能在fatal里拿到notices信息 | ||
// 为了简化成 req.log.fatal(err) 或者 req.log.fatal({err}, 'msg'); | ||
['warn', 'fatal', 'error'].forEach((level: string) => { | ||
const ori = (req.log as any)[level]; | ||
(req.log as any)[level] = (...args: any[]) => { | ||
const obj = args[0]; | ||
// 从对象提取出非 req、res、err、app字段,放置extraInfo,会自动加入日志 | ||
if (obj && obj.constructor === Object) { | ||
const extraInfo: any = {}; | ||
const resOb: any = {}; | ||
for (const key of Object.keys(obj)) { | ||
if (['req', 'res', 'err', 'app'].includes(key)) { | ||
resOb[key] = obj[key]; | ||
} | ||
else { | ||
extraInfo[key] = obj[key]; | ||
} | ||
} | ||
// 默认增加req | ||
resOb.req = resOb.req || req; | ||
args[0] = resOb; | ||
} | ||
// 默认增加req | ||
else if (obj instanceof Error) { | ||
args[0] = { | ||
err: obj, | ||
req, | ||
}; | ||
} | ||
// 默认增加req | ||
else if (typeof obj === 'string') { | ||
args[0] = { | ||
msg: obj, | ||
req, | ||
}; | ||
} | ||
ori.apply(req.log, args); | ||
}; | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {FastifyRequest} from 'fastify'; | ||
import {performance} from 'perf_hooks'; | ||
|
||
export const parseStartTimeSym = Symbol.for('hoth.parse-start-time'); | ||
|
||
export default async function (req: FastifyRequest) { | ||
req[parseStartTimeSym] = performance.now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {FastifyRequest} from 'fastify'; | ||
import {performance} from 'perf_hooks'; | ||
|
||
export const serializationTimeSym = Symbol.for('hoth.serialization-time'); | ||
|
||
export default async function (req: FastifyRequest) { | ||
req[serializationTimeSym] = performance.now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.