Skip to content

Commit

Permalink
perf(app-autoload): logid beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Aug 9, 2021
1 parent 77d5ac8 commit 2b6d9a6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
9 changes: 7 additions & 2 deletions packages/app-autoload/src/hook/onRequestFactory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {performance} from 'perf_hooks';
import {FastifyInstance, FastifyRequest} from 'fastify';
import uuid from 'uuid-random';

export const parseStartTimeSym = Symbol.for('hoth.parse-start-time');

export default function (appConfig: FastifyRequest["$appConfig"], fastify: FastifyInstance) {
export default function (appConfig: FastifyRequest['$appConfig'], fastify: FastifyInstance) {
return async function (req: FastifyRequest) {
req[parseStartTimeSym] = performance.now();
req.logid = req.logid
|| (req.headers.x_bd_logid as string)
|| (req.headers.logid as string)
|| uuid();
req.$appConfig = appConfig;
req.$service = fastify;
req[parseStartTimeSym] = performance.now();
};
}
7 changes: 1 addition & 6 deletions packages/app-autoload/src/hook/preHandlerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import {FastifyReply, FastifyRequest} from 'fastify';
import uuid from 'uuid-random';
import {performance} from 'perf_hooks';
import {parseStartTimeSym} from './onRequestFactory';
import {validateStartTimeSym} from './preValidation';

declare module 'fastify' {
interface FastifyRequest {
[validateStartTimeSym]: number;
[parseStartTimeSym]: number
[parseStartTimeSym]: number;
}
}

export default function (app: string) {
return async function (req: FastifyRequest, reply: FastifyReply) {
req.logid = req.logid
|| (req.headers.x_bd_logid as string)
|| (req.headers.logid as string)
|| uuid();
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));
Expand Down
4 changes: 2 additions & 2 deletions packages/app-autoload/src/hook/preValidation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {FastifyReply, FastifyRequest} from 'fastify';
import type {FastifyRequest} from 'fastify';
import {performance} from 'perf_hooks';

export const validateStartTimeSym = Symbol.for('hoth.validate-start-time');

export default async function (req: FastifyRequest, reply: FastifyReply) {
export default async function (req: FastifyRequest) {
req[validateStartTimeSym] = performance.now();
}

0 comments on commit 2b6d9a6

Please sign in to comment.