Merge pull request #142 from pedrofariasx/feat/observability-reliability

feat: enhance server reliability and observability (graceful shutdown + request logging)
This commit is contained in:
Badri Narayanan S
2026-01-18 01:00:41 +05:30
committed by GitHub
3 changed files with 48 additions and 10 deletions

View File

@@ -60,7 +60,7 @@ function loadConfig() {
if (process.env.DEBUG === 'true') config.debug = true; if (process.env.DEBUG === 'true') config.debug = true;
} catch (error) { } catch (error) {
console.error('[Config] Error loading config:', error); logger.error('[Config] Error loading config:', error);
} }
} }

View File

@@ -34,7 +34,7 @@ const PORT = process.env.PORT || DEFAULT_PORT;
const HOME_DIR = os.homedir(); const HOME_DIR = os.homedir();
const CONFIG_DIR = path.join(HOME_DIR, '.antigravity-claude-proxy'); const CONFIG_DIR = path.join(HOME_DIR, '.antigravity-claude-proxy');
app.listen(PORT, () => { const server = app.listen(PORT, () => {
// Clear console for a clean start // Clear console for a clean start
console.clear(); 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'); 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);

View File

@@ -28,6 +28,9 @@ const FALLBACK_ENABLED = args.includes('--fallback') || process.env.FALLBACK ===
const app = express(); 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) // Initialize account manager (will be fully initialized on first request or startup)
const accountManager = new AccountManager(); const accountManager = new AccountManager();
@@ -150,14 +153,31 @@ function parseError(error) {
// Request logging middleware // Request logging middleware
app.use((req, res, next) => { app.use((req, res, next) => {
// Skip logging for event logging batch unless in debug mode const start = Date.now();
if (req.path === '/api/event_logging/batch') {
if (logger.isDebugEnabled) { // Log response on finish
logger.debug(`[${req.method}] ${req.path}`); 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(); next();
}); });
@@ -802,4 +822,4 @@ app.use('*', (req, res) => {
}); });
}); });
export default app; export default app;