UNPKG

@vtex/diagnostics-nodejs

Version:

Diagnostics library for Node.js applications

157 lines (155 loc) • 5.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const file_1 = require("./providers/file"); const s3_1 = require("./providers/s3"); class ConfigManager { constructor(applicationID, serviceName, options) { this.configProvider = null; this.pollTimer = null; this.configListeners = []; this.configChangeTimeout = null; this.applicationID = applicationID; this.serviceName = serviceName; this.options = options; this.config = this.createDefaultConfig(); if (options.configPath) { this.configProvider = new file_1.FileConfigProvider(options.configPath); } else if (options.s3Config) { this.configProvider = new s3_1.S3ConfigProvider(options.s3Config.bucket, options.s3Config.key, options.s3Config.region); } } async initialize() { if (this.configProvider) { try { const loadedConfig = await this.configProvider.load(); if (loadedConfig) { this.config = { ...this.createDefaultConfig(), ...loadedConfig, traces: { ...this.createDefaultConfig().traces, ...loadedConfig.traces }, metrics: { ...this.createDefaultConfig().metrics, ...loadedConfig.metrics }, logs: { ...this.createDefaultConfig().logs, ...loadedConfig.logs }, }; } } catch (error) { console.warn('[ConfigManager] Failed to load config from external source, using defaults:', error); } if (this.options.enableHotReload !== false) { this.startPolling(); } } } getConfig() { return this.config; } getTracesConfig() { return this.config.traces || {}; } getApplicationID() { return this.applicationID; } getServiceName() { return this.serviceName; } onConfigChange(listener) { this.configListeners.push(listener); return () => { const index = this.configListeners.indexOf(listener); if (index > -1) { this.configListeners.splice(index, 1); } }; } startPolling() { const interval = this.options.pollIntervalMs || 30000; this.pollTimer = setInterval(async () => { await this.checkForConfigUpdates(); }, interval); } async checkForConfigUpdates() { if (!this.configProvider) return; try { const newConfig = await this.configProvider.load(); if (newConfig) { if (newConfig.applicationID || newConfig.serviceName) { console.error(` āŒ CONFIGURATION ERROR: applicationID and serviceName cannot be set via config files. These are immutable identities that must be set in code. Remove from config file: - applicationID - serviceName Set them explicitly in NewTelemetryClient() call instead. `.trim()); return; } const newConfigStr = JSON.stringify(newConfig); const currentConfigStr = JSON.stringify(this.config); if (newConfigStr !== currentConfigStr) { const mergedConfig = { ...this.config, ...newConfig }; this.config = mergedConfig; this.debounceConfigChange(mergedConfig); } } } catch (error) { console.warn('[ConfigManager] Failed to check for config updates:', error); } } debounceConfigChange(config) { if (this.configChangeTimeout) { clearTimeout(this.configChangeTimeout); } this.configChangeTimeout = setTimeout(() => { this.configListeners.forEach(listener => { try { listener(config); } catch (error) { console.error('[ConfigManager] Error in config change listener:', error); } }); this.configChangeTimeout = null; }, 1000); } createDefaultConfig() { return { traces: { sampling: { defaultRate: 0.1, parentBased: true, rules: [] }, exporters: { console: { enabled: true } } }, metrics: { exporters: { console: { enabled: true } } }, logs: { exporters: { console: { enabled: true } } } }; } async shutdown() { if (this.pollTimer) { clearInterval(this.pollTimer); this.pollTimer = null; } if (this.configChangeTimeout) { clearTimeout(this.configChangeTimeout); this.configChangeTimeout = null; } this.configListeners = []; } } exports.ConfigManager = ConfigManager; //# sourceMappingURL=manager.js.map