feat: Phase 0+1 — repo sync, pino, lint fixes, core components
Phase 0: - Synced latest live-status.js from workspace (9928 bytes) - Fixed 43 lint issues: empty catch blocks, console statements - Added pino dependency - Created src/tool-labels.json with all known tool mappings - make check passes Phase 1 (Core Components): - src/config.js: env-var config with validation, throws on missing required vars - src/logger.js: pino singleton with child loggers, level validation - src/circuit-breaker.js: CLOSED/OPEN/HALF_OPEN state machine with callbacks - src/tool-labels.js: exact/prefix/regex tool->label resolver with external override - src/status-box.js: Mattermost post manager (keepAlive, throttle, retry, circuit breaker) - src/status-formatter.js: pure SessionState->text formatter (nested, compact) - src/health.js: HTTP health endpoint + metrics - src/status-watcher.js: JSONL file watcher (inotify, compaction detection, idle detection) Tests: - test/unit/config.test.js: 7 tests - test/unit/circuit-breaker.test.js: 12 tests - test/unit/logger.test.js: 5 tests - test/unit/status-formatter.test.js: 20 tests - test/unit/tool-labels.test.js: 15 tests All 59 unit tests pass. make check clean.
This commit is contained in:
118
src/health.js
Normal file
118
src/health.js
Normal file
@@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* health.js — HTTP health endpoint + metrics.
|
||||
*
|
||||
* GET /health -> JSON { status, activeSessions, uptime, lastError, metrics }
|
||||
* GET /metrics -> JSON { detailed metrics }
|
||||
*/
|
||||
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const http = require('http');
|
||||
|
||||
class HealthServer {
|
||||
/**
|
||||
* @param {object} opts
|
||||
* @param {number} opts.port - Port to listen on (0 = disabled)
|
||||
* @param {Function} opts.getMetrics - Callback that returns metrics object
|
||||
* @param {object} [opts.logger] - pino logger
|
||||
*/
|
||||
constructor(opts) {
|
||||
this.port = opts.port;
|
||||
this.getMetrics = opts.getMetrics;
|
||||
this.logger = opts.logger || null;
|
||||
this.server = null;
|
||||
this.startTime = Date.now();
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.port === 0) {
|
||||
if (this.logger) this.logger.info('Health server disabled (port=0)');
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server = http.createServer((req, res) => {
|
||||
this._handleRequest(req, res);
|
||||
});
|
||||
|
||||
this.server.on('error', (err) => {
|
||||
if (this.logger) {
|
||||
this.logger.error({ err }, 'Health server error');
|
||||
} else {
|
||||
console.error('Health server error:', err.message);
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
|
||||
this.server.listen(this.port, '127.0.0.1', () => {
|
||||
if (this.logger) {
|
||||
this.logger.info({ port: this.port }, 'Health server listening');
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
return new Promise((resolve) => {
|
||||
if (!this.server) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
this.server.close(() => resolve());
|
||||
});
|
||||
}
|
||||
|
||||
_handleRequest(req, res) {
|
||||
const url = new URL(req.url, `http://localhost:${this.port}`);
|
||||
|
||||
if (req.method !== 'GET') {
|
||||
res.writeHead(405, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Method not allowed' }));
|
||||
return;
|
||||
}
|
||||
|
||||
let body;
|
||||
switch (url.pathname) {
|
||||
case '/health':
|
||||
body = this._buildHealthResponse();
|
||||
break;
|
||||
case '/metrics':
|
||||
body = this._buildMetricsResponse();
|
||||
break;
|
||||
default:
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not found' }));
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(body, null, 2));
|
||||
}
|
||||
|
||||
_buildHealthResponse() {
|
||||
const metrics = this.getMetrics();
|
||||
const status = metrics.circuit && metrics.circuit.state === 'open' ? 'degraded' : 'healthy';
|
||||
|
||||
return {
|
||||
status,
|
||||
uptime: Math.floor((Date.now() - this.startTime) / 1000),
|
||||
activeSessions: metrics.activeSessions || 0,
|
||||
lastError: metrics.lastError || null,
|
||||
metrics: {
|
||||
updates_sent: metrics.updatesSent || 0,
|
||||
updates_failed: metrics.updatesFailed || 0,
|
||||
circuit_state: metrics.circuit ? metrics.circuit.state : 'unknown',
|
||||
queue_depth: metrics.queueDepth || 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
_buildMetricsResponse() {
|
||||
return this.getMetrics();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HealthServer };
|
||||
Reference in New Issue
Block a user