feat: server reliability and observability improvements

This commit is contained in:
Pedro Farias
2026-01-17 13:08:40 -03:00
parent ed68f4b21e
commit 6a6c4829ca
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;
} 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 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);

View File

@@ -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
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(`[${req.method}] ${req.path}`);
logger.debug(logMsg);
}
} else {
logger.info(`[${req.method}] ${req.path}`);
// Colorize status code
if (status >= 500) {
logger.error(logMsg);
} else if (status >= 400) {
logger.warn(logMsg);
} else {
logger.info(logMsg);
}
}
});
next();
});