Merge pull request #142 from pedrofariasx/feat/observability-reliability
feat: enhance server reliability and observability (graceful shutdown + request logging)
This commit is contained in:
@@ -60,7 +60,7 @@ function loadConfig() {
|
||||
if (process.env.DEBUG === 'true') config.debug = true;
|
||||
|
||||
} catch (error) {
|
||||
console.error('[Config] Error loading config:', error);
|
||||
logger.error('[Config] Error loading config:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
src/index.js
20
src/index.js
@@ -34,7 +34,7 @@ const PORT = process.env.PORT || DEFAULT_PORT;
|
||||
const HOME_DIR = os.homedir();
|
||||
const CONFIG_DIR = path.join(HOME_DIR, '.antigravity-claude-proxy');
|
||||
|
||||
app.listen(PORT, () => {
|
||||
const server = app.listen(PORT, () => {
|
||||
// Clear console for a clean start
|
||||
console.clear();
|
||||
|
||||
@@ -105,3 +105,21 @@ ${border} ${align4(`export ANTHROPIC_BASE_URL=http://localhost:${PORT}`)}${bo
|
||||
logger.warn('Running in DEBUG mode - verbose logs enabled');
|
||||
}
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
const shutdown = () => {
|
||||
logger.info('Shutting down server...');
|
||||
server.close(() => {
|
||||
logger.success('Server stopped');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// Force close if it takes too long
|
||||
setTimeout(() => {
|
||||
logger.error('Could not close connections in time, forcefully shutting down');
|
||||
process.exit(1);
|
||||
}, 10000);
|
||||
};
|
||||
|
||||
process.on('SIGTERM', shutdown);
|
||||
process.on('SIGINT', shutdown);
|
||||
@@ -28,6 +28,9 @@ const FALLBACK_ENABLED = args.includes('--fallback') || process.env.FALLBACK ===
|
||||
|
||||
const app = express();
|
||||
|
||||
// Disable x-powered-by header for security
|
||||
app.disable('x-powered-by');
|
||||
|
||||
// Initialize account manager (will be fully initialized on first request or startup)
|
||||
const accountManager = new AccountManager();
|
||||
|
||||
@@ -150,14 +153,31 @@ function parseError(error) {
|
||||
|
||||
// Request logging middleware
|
||||
app.use((req, res, next) => {
|
||||
// Skip logging for event logging batch unless in debug mode
|
||||
if (req.path === '/api/event_logging/batch') {
|
||||
if (logger.isDebugEnabled) {
|
||||
logger.debug(`[${req.method}] ${req.path}`);
|
||||
const start = Date.now();
|
||||
|
||||
// Log response on finish
|
||||
res.on('finish', () => {
|
||||
const duration = Date.now() - start;
|
||||
const status = res.statusCode;
|
||||
const logMsg = `[${req.method}] ${req.path} ${status} (${duration}ms)`;
|
||||
|
||||
// Skip standard logging for event logging batch unless in debug mode
|
||||
if (req.path === '/api/event_logging/batch') {
|
||||
if (logger.isDebugEnabled) {
|
||||
logger.debug(logMsg);
|
||||
}
|
||||
} else {
|
||||
// Colorize status code
|
||||
if (status >= 500) {
|
||||
logger.error(logMsg);
|
||||
} else if (status >= 400) {
|
||||
logger.warn(logMsg);
|
||||
} else {
|
||||
logger.info(logMsg);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.info(`[${req.method}] ${req.path}`);
|
||||
}
|
||||
});
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user