codecrucible-synth
Version:
Production-Ready AI Development Platform with Multi-Voice Synthesis, Smithery MCP Integration, Enterprise Security, and Zero-Timeout Reliability
124 lines • 4.14 kB
JavaScript
/**
* Observability and Monitoring System
* Provides enterprise-grade monitoring, metrics, and observability features
*/
import { EventEmitter } from 'events';
import { logger } from '../../core/logger.js';
export class ObservabilitySystem extends EventEmitter {
metrics = new Map();
config;
healthMetrics = new Map();
constructor(config = {}) {
super();
this.config = {
metricsEnabled: true,
tracingEnabled: true,
loggingLevel: 'info',
retentionDays: 7,
...config,
};
}
recordMetric(metric) {
if (!this.config.metricsEnabled)
return;
const metricHistory = this.metrics.get(metric.name) || [];
metricHistory.push(metric);
// Keep only recent metrics based on retention policy
const cutoffTime = new Date(Date.now() - this.config.retentionDays * 24 * 60 * 60 * 1000);
const filteredHistory = metricHistory.filter(m => m.timestamp > cutoffTime);
this.metrics.set(metric.name, filteredHistory);
this.emit('metric-recorded', metric);
}
recordHealthMetric(healthMetric) {
this.healthMetrics.set(healthMetric.component, healthMetric);
this.emit('health-metric-recorded', healthMetric);
}
getMetric(name) {
return this.metrics.get(name) || [];
}
getHealthMetric(component) {
return this.healthMetrics.get(component);
}
getAllMetrics() {
return Object.fromEntries(this.metrics.entries());
}
getAllHealthMetrics() {
return Object.fromEntries(this.healthMetrics.entries());
}
getSystemOverview() {
const totalMetrics = Array.from(this.metrics.values()).reduce((sum, metrics) => sum + metrics.length, 0);
const healthMetrics = Array.from(this.healthMetrics.values());
const healthyComponents = healthMetrics.filter(m => m.status === 'healthy').length;
const unhealthyComponents = healthMetrics.filter(m => m.status === 'unhealthy').length;
const averageResponseTime = healthMetrics.length > 0
? healthMetrics.reduce((sum, m) => sum + m.responseTime, 0) / healthMetrics.length
: 0;
return {
totalMetrics,
healthyComponents,
unhealthyComponents,
averageResponseTime,
};
}
startMonitoring() {
logger.info('Observability system started');
this.emit('monitoring-started');
}
stopMonitoring() {
logger.info('Observability system stopped');
this.emit('monitoring-stopped');
}
clearMetrics() {
this.metrics.clear();
this.healthMetrics.clear();
logger.info('All metrics cleared');
}
}
// Export singleton instance
export const observabilitySystem = new ObservabilitySystem();
// Export metrics and logging interfaces for compatibility
export const metrics = {
apiRequestDuration: {
observe: (labels, value) => {
observabilitySystem.recordMetric({
name: 'api_request_duration',
value,
timestamp: new Date(),
tags: labels,
});
},
},
memoryUsage: {
set: (value) => {
observabilitySystem.recordMetric({
name: 'memory_usage',
value,
timestamp: new Date(),
tags: { type: 'heap' },
});
},
},
};
export const logging = {
error: (message, error) => {
console.error(message, error);
if (error) {
observabilitySystem.recordMetric({
name: 'error_count',
value: 1,
timestamp: new Date(),
tags: { level: 'error', message },
});
}
},
info: (message) => {
console.info(message);
observabilitySystem.recordMetric({
name: 'info_log_count',
value: 1,
timestamp: new Date(),
tags: { level: 'info', message },
});
},
};
//# sourceMappingURL=observability.js.map