UNPKG

il2cpp-dump-analyzer-mcp

Version:

Agentic RAG system for analyzing IL2CPP dump.cs files from Unity games

336 lines 11.2 kB
"use strict"; /** * Monitoring integration service for IL2CPP dump analyzer MCP system * Coordinates health monitoring, metrics collection, and lifecycle management */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MonitoringIntegration = void 0; const events_1 = require("events"); const health_service_1 = require("./health-service"); const metrics_service_1 = require("./metrics-service"); const lifecycle_manager_1 = require("./lifecycle-manager"); /** * Comprehensive monitoring integration service */ class MonitoringIntegration extends events_1.EventEmitter { constructor(config) { super(); this.isInitialized = false; this.isRunning = false; this.config = { enabled: true, healthChecks: { enabled: true, interval: 30000, timeout: 10000 }, metrics: { enabled: true, exportInterval: 60000, prometheusEnabled: true }, lifecycle: { enabled: true, restartPolicy: 'on-failure', maxRestarts: 5 }, alerts: { enabled: false, emailEnabled: false }, ...config }; this.startTime = new Date(); this.initializeServices(); } /** * Initialize monitoring services */ initializeServices() { // Initialize health service this.healthService = new health_service_1.HealthService({ interval: this.config.healthChecks.interval, timeout: this.config.healthChecks.timeout, components: { database: true, vectorStore: true, embeddings: true, mcp: true } }); // Initialize metrics service this.metricsService = new metrics_service_1.MetricsService({ enabled: this.config.metrics.enabled, exportInterval: this.config.metrics.exportInterval, prometheusEnabled: this.config.metrics.prometheusEnabled }); // Initialize lifecycle manager this.lifecycleManager = new lifecycle_manager_1.LifecycleManager({ restartPolicy: this.config.lifecycle.restartPolicy, maxRestarts: this.config.lifecycle.maxRestarts, healthCheckInterval: this.config.healthChecks.interval }); this.setupEventHandlers(); } /** * Setup event handlers between services */ setupEventHandlers() { // Health service events this.healthService.on('healthCheck', (status) => { this.emit('healthStatusChanged', status); // Log significant health changes if (status.status !== 'healthy') { console.warn(`Health status: ${status.status}`, { components: status.components.filter((c) => c.status !== 'healthy') }); } }); this.healthService.on('started', () => { console.log('Health monitoring started'); }); this.healthService.on('stopped', () => { console.log('Health monitoring stopped'); }); // Metrics service events this.metricsService.on('metricsExported', (data) => { this.emit('metricsExported', data); }); this.metricsService.on('started', () => { console.log('Metrics collection started'); }); this.metricsService.on('stopped', () => { console.log('Metrics collection stopped'); }); // Lifecycle manager events this.lifecycleManager.on('started', () => { console.log('Application lifecycle started'); this.emit('applicationStarted'); }); this.lifecycleManager.on('stopped', () => { console.log('Application lifecycle stopped'); this.emit('applicationStopped'); }); this.lifecycleManager.on('restarting', (data) => { console.log(`Application restarting: ${data.reason} (attempt ${data.attempt})`); this.emit('applicationRestarting', data); }); this.lifecycleManager.on('unhealthy', (status) => { console.error('Application became unhealthy:', status); this.emit('applicationUnhealthy', status); // Trigger alerts if enabled if (this.config.alerts.enabled) { this.sendAlert('unhealthy', status); } }); this.lifecycleManager.on('healthy', (status) => { console.log('Application became healthy'); this.emit('applicationHealthy', status); }); } /** * Initialize monitoring with application components */ async initialize(components) { if (this.isInitialized) { console.warn('Monitoring integration already initialized'); return; } console.log('Initializing monitoring integration...'); try { // Initialize health service with components await this.healthService.initialize(components); // Initialize metrics service with health service this.metricsService.initialize(this.healthService); // Initialize lifecycle manager with services this.lifecycleManager.initialize({ healthService: this.healthService, metricsService: this.metricsService }); this.isInitialized = true; console.log('Monitoring integration initialized successfully'); this.emit('initialized'); } catch (error) { console.error('Failed to initialize monitoring integration:', error); throw error; } } /** * Start all monitoring services */ async start() { if (!this.isInitialized) { throw new Error('Monitoring integration not initialized'); } if (this.isRunning) { console.warn('Monitoring integration already running'); return; } console.log('Starting monitoring integration...'); try { // Start lifecycle manager (which will start other services) await this.lifecycleManager.start(); this.isRunning = true; console.log('Monitoring integration started successfully'); this.emit('started'); } catch (error) { console.error('Failed to start monitoring integration:', error); throw error; } } /** * Stop all monitoring services */ async stop() { if (!this.isRunning) { console.warn('Monitoring integration not running'); return; } console.log('Stopping monitoring integration...'); try { // Stop lifecycle manager (which will stop other services) await this.lifecycleManager.stop(); this.isRunning = false; console.log('Monitoring integration stopped successfully'); this.emit('stopped'); } catch (error) { console.error('Failed to stop monitoring integration:', error); throw error; } } /** * Get comprehensive monitoring status */ async getStatus() { const healthStatus = await this.healthService.getHealthStatus(); const metricsStatus = this.metricsService.getMetricsSummary(); const lifecycleStatus = this.lifecycleManager.getState(); return { overall: healthStatus.status, services: { health: this.healthService.getStats().isRunning, metrics: metricsStatus.isRunning, lifecycle: lifecycleStatus.phase === 'running' }, uptime: Date.now() - this.startTime.getTime(), lastUpdate: new Date() }; } /** * Get health status */ async getHealthStatus() { return this.healthService.getHealthStatus(); } /** * Get metrics in Prometheus format */ getPrometheusMetrics() { return this.metricsService.getPrometheusMetrics(); } /** * Get lifecycle state */ getLifecycleState() { return this.lifecycleManager.getState(); } /** * Force application restart */ async forceRestart(reason = 'manual trigger') { console.log(`Force restart requested: ${reason}`); await this.lifecycleManager.forceRestart(reason); } /** * Update MCP metrics */ updateMCPMetrics(metrics) { this.healthService.updateMCPMetrics(metrics); } /** * Record custom metric */ recordMetric(metric) { this.metricsService.recordMetric(metric); } /** * Send alert notification */ async sendAlert(type, data) { try { const alert = { type, timestamp: new Date().toISOString(), service: 'il2cpp-dump-analyzer-mcp', data, severity: type === 'unhealthy' ? 'critical' : 'warning' }; // Log alert console.warn('ALERT:', alert); // Send webhook if configured if (this.config.alerts.webhookUrl) { await this.sendWebhookAlert(alert); } this.emit('alertSent', alert); } catch (error) { console.error('Failed to send alert:', error); } } /** * Send webhook alert */ async sendWebhookAlert(alert) { if (!this.config.alerts.webhookUrl) { return; } try { const response = await fetch(this.config.alerts.webhookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'User-Agent': 'IL2CPP-MCP-Monitor/1.0' }, body: JSON.stringify(alert) }); if (!response.ok) { throw new Error(`Webhook failed: ${response.status} ${response.statusText}`); } console.log('Alert webhook sent successfully'); } catch (error) { console.error('Failed to send webhook alert:', error); } } /** * Register shutdown handler */ onShutdown(handler) { this.lifecycleManager.onShutdown(handler); } /** * Register startup handler */ onStartup(handler) { this.lifecycleManager.onStartup(handler); } /** * Get monitoring configuration */ getConfig() { return { ...this.config }; } /** * Update monitoring configuration */ updateConfig(updates) { this.config = { ...this.config, ...updates }; console.log('Monitoring configuration updated'); this.emit('configUpdated', this.config); } } exports.MonitoringIntegration = MonitoringIntegration; //# sourceMappingURL=monitoring-integration.js.map