Add request and SQL performance logging features to backend
- Implement request timing middleware in app.js to log slow requests and all requests based on environment variables. - Enhance sequelize.js with optional SQL query timing and logging capabilities, allowing for performance monitoring of database queries.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import crypto from 'crypto';
|
||||
import chatRouter from './routers/chatRouter.js';
|
||||
import authRouter from './routers/authRouter.js';
|
||||
import navigationRouter from './routers/navigationRouter.js';
|
||||
@@ -25,6 +26,25 @@ const __dirname = path.dirname(__filename);
|
||||
|
||||
const app = express();
|
||||
|
||||
// Request-Timing (aktivierbar per ENV)
|
||||
// - LOG_SLOW_REQ_MS=200: Logge Requests, die länger dauern als X ms (Default 500)
|
||||
// - LOG_ALL_REQ=1: Logge alle Requests
|
||||
const LOG_ALL_REQ = process.env.LOG_ALL_REQ === '1';
|
||||
const LOG_SLOW_REQ_MS = Number.parseInt(process.env.LOG_SLOW_REQ_MS || '500', 10);
|
||||
app.use((req, res, next) => {
|
||||
const reqId = req.headers['x-request-id'] || (crypto.randomUUID ? crypto.randomUUID() : crypto.randomBytes(8).toString('hex'));
|
||||
req.reqId = reqId;
|
||||
res.setHeader('x-request-id', reqId);
|
||||
const t0 = Date.now();
|
||||
res.on('finish', () => {
|
||||
const ms = Date.now() - t0;
|
||||
if (LOG_ALL_REQ || ms >= LOG_SLOW_REQ_MS) {
|
||||
console.log(`⏱️ REQ ${ms}ms ${res.statusCode} ${req.method} ${req.originalUrl} rid=${reqId}`);
|
||||
}
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
const corsOptions = {
|
||||
origin: ['http://localhost:3000', 'http://localhost:5173', 'http://127.0.0.1:3000', 'http://127.0.0.1:5173'],
|
||||
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
|
||||
|
||||
@@ -3,6 +3,25 @@ import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// Optionales Performance-Logging (aktivierbar per ENV)
|
||||
// - SQL_BENCHMARK=1: Sequelize liefert Query-Timing (ms) an logger
|
||||
// - SQL_SLOW_MS=200: Logge nur Queries ab dieser Dauer (wenn SQL_LOG_ALL nicht gesetzt)
|
||||
// - SQL_LOG_ALL=1: Logge alle Queries (auch ohne benchmark)
|
||||
const SQL_BENCHMARK = process.env.SQL_BENCHMARK === '1';
|
||||
const SQL_LOG_ALL = process.env.SQL_LOG_ALL === '1';
|
||||
const SQL_SLOW_MS = Number.parseInt(process.env.SQL_SLOW_MS || '200', 10);
|
||||
const sqlLogger = (sql, timing) => {
|
||||
// Sequelize ruft logging(sql) oder logging(sql, timing) abhängig von benchmark auf.
|
||||
if (!SQL_BENCHMARK) {
|
||||
if (SQL_LOG_ALL) console.log(sql);
|
||||
return;
|
||||
}
|
||||
const ms = typeof timing === 'number' ? timing : 0;
|
||||
if (SQL_LOG_ALL || ms >= SQL_SLOW_MS) {
|
||||
console.log(`🛢️ SQL ${ms}ms: ${sql}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Validiere Umgebungsvariablen
|
||||
const dbName = process.env.DB_NAME;
|
||||
const dbUser = process.env.DB_USER;
|
||||
@@ -26,6 +45,8 @@ const sequelize = new Sequelize(dbName, dbUser, dbPass, {
|
||||
timestamps: false,
|
||||
underscored: true // WICHTIG: Alle Datenbankfelder im snake_case Format
|
||||
},
|
||||
benchmark: SQL_BENCHMARK,
|
||||
logging: sqlLogger,
|
||||
});
|
||||
|
||||
const createSchemas = async () => {
|
||||
|
||||
Reference in New Issue
Block a user