feat: database beanchmark (#5615)

* chore: db logging with sql benchmark

* chore: log response size
This commit is contained in:
ChengLei Shao 2024-11-08 12:13:43 +08:00 committed by GitHub
parent 2d41b2acbb
commit 8e1eed7c2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 2 deletions

View File

@ -238,7 +238,12 @@ export class Database extends EventEmitter implements AsyncEmitter {
}); });
} }
if (options.logging && process.env['DB_SQL_BENCHMARK'] == 'true') {
opts.benchmark = true;
}
this.options = opts; this.options = opts;
this.logger.debug( this.logger.debug(
`create database instance: ${safeJsonStringify( `create database instance: ${safeJsonStringify(
// remove sensitive information // remove sensitive information

View File

@ -66,6 +66,7 @@ export const requestLogger = (appName: string, requestLogger: Logger, options?:
cost, cost,
app: appName, app: appName,
reqId, reqId,
bodySize: ctx.response.length,
}; };
if (Math.floor(status / 100) == 5) { if (Math.floor(status / 100) == 5) {
requestLogger.error({ ...info, res: ctx.body?.['errors'] || ctx.body }); requestLogger.error({ ...info, res: ctx.body?.['errors'] || ctx.body });

View File

@ -1208,15 +1208,26 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
} }
protected createDatabase(options: ApplicationOptions) { protected createDatabase(options: ApplicationOptions) {
const logging = (msg: any) => { const logging = (...args) => {
let msg = args[0];
if (typeof msg === 'string') { if (typeof msg === 'string') {
msg = msg.replace(/[\r\n]/gm, '').replace(/\s+/g, ' '); msg = msg.replace(/[\r\n]/gm, '').replace(/\s+/g, ' ');
} }
if (msg.includes('INSERT INTO')) { if (msg.includes('INSERT INTO')) {
msg = msg.substring(0, 2000) + '...'; msg = msg.substring(0, 2000) + '...';
} }
this._sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId });
const content: any = { message: msg, app: this.name, reqId: this.context.reqId };
if (args[1] && typeof args[1] === 'number') {
content.executeTime = args[1];
}
this._sqlLogger.debug(content);
}; };
const dbOptions = options.database instanceof Database ? options.database.options : options.database; const dbOptions = options.database instanceof Database ? options.database.options : options.database;
const db = new Database({ const db = new Database({
...dbOptions, ...dbOptions,