All files index.js

68.25% Statements 43/63
51.28% Branches 20/39
72.72% Functions 16/22
72.72% Lines 40/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181          2x       55x 55x               55x           55x 55x     55x                                                                 52x       52x       32x       32x       28x       28x       29x       29x             129x 129x       117x 117x       126x 126x             1x 1x                         2x         55x 55x     55x 55x 55x 55x     55x   55x     55x           55x 5x 5x         55x 55x   55x             3x                  
/**
 * @onlineapps/conn-base-monitoring
 * Service wrapper connector for monitoring
 */
 
const monitoringCore = require('@onlineapps/monitoring-core');
 
class MonitoringConnector {
  constructor() {
    this.core = null;
    this.initialized = false;
  }
 
  /**
   * Initialize monitoring with service defaults
   */
  async init(options = {}) {
    // Service-specific defaults
    const config = {
      mode: process.env.MONITORING_MODE || 'light',
      ...options
    };
 
    // Create new instance using factory
    this.core = await monitoringCore.init(config);
    this.initialized = true;
 
    // Return convenient API
    return {
      // Logger functions (replaces old logger)
      log: this.log.bind(this),
      info: this.info.bind(this),
      error: this.error.bind(this),
      warn: this.warn.bind(this),
      debug: this.debug.bind(this),
 
      // Workflow tracking
      startWorkflow: this.startWorkflow.bind(this),
      stepWorkflow: this.stepWorkflow.bind(this),
      endWorkflow: this.endWorkflow.bind(this),
 
      // Metrics
      createMetric: this.createMetric.bind(this),
 
      // Core access if needed
      core: this.core
    };
  }
 
  /**
   * Logger methods (backward compatible with old logger)
   */
  log(level, message, data) {
    if (!this.initialized) {
      console.log(`[${level}] ${message}`, data);
      return;
    }
    this.core.logger.log(level, message, data);
  }
 
  info(message, data) {
    Iif (!this.initialized) {
      console.log(message, data);
      return;
    }
    this.core.logger.info(message, data);
  }
 
  error(message, data) {
    Iif (!this.initialized) {
      console.error(message, data);
      return;
    }
    this.core.logger.error(message, data);
  }
 
  warn(message, data) {
    Iif (!this.initialized) {
      console.warn(message, data);
      return;
    }
    this.core.logger.warn(message, data);
  }
 
  debug(message, data) {
    Iif (!this.initialized) {
      console.debug(message, data);
      return;
    }
    this.core.logger.debug(message, data);
  }
 
  /**
   * Workflow tracking
   */
  startWorkflow(id, type, attributes) {
    Iif (!this.initialized) return;
    return this.core.workflow.start(id, type, attributes);
  }
 
  stepWorkflow(id, stepName, attributes) {
    Iif (!this.initialized) return;
    return this.core.workflow.step(id, stepName, attributes);
  }
 
  endWorkflow(id, status, attributes) {
    Iif (!this.initialized) return;
    return this.core.workflow.end(id, status, attributes);
  }
 
  /**
   * Metrics
   */
  createMetric(name, type) {
    Iif (!this.initialized) return { add: () => {}, record: () => {} };
    return this.core.createMetric(name, type);
  }
 
  /**
   * Graceful shutdown
   */
  async shutdown() {
    if (!this.initialized) return;
    await this.core.shutdown();
  }
}
 
// Export API similar to monitoring-core
module.exports = {
  /**
   * Initialize monitoring with factory pattern
   */
  init: async (options) => {
    const connector = new MonitoringConnector();
    const api = await connector.init(options);
 
    // Add core references
    api.logger = connector.core.logger;
    api.workflow = connector.core.workflow;
    api.config = connector.core.config;
    api.shutdown = () => connector.core.shutdown();
 
    // Add workflow helpers for testing
    Eif (api.workflow && connector.core.workflow) {
      // Expose activeWorkflows directly
      api.workflow.activeWorkflows = connector.core.workflow.activeWorkflows;
 
      // Only override getWorkflow if not already defined
      Iif (!api.workflow.getWorkflow && connector.core.workflow.getWorkflow) {
        api.workflow.getWorkflow = connector.core.workflow.getWorkflow;
      }
    }
 
    // Workflow helpers
    api.errorWorkflow = (id, error, attrs) => {
      Eif (connector.core.workflow && connector.core.workflow.error) {
        connector.core.workflow.error(id, error, attrs);
      }
    };
 
    // Metric helpers
    api.createCounter = (name, opts) => connector.core.createCounter ? connector.core.createCounter(name, opts) : { add: () => {} };
    api.createHistogram = (name, opts) => connector.core.createHistogram ? connector.core.createHistogram(name, opts) : { record: () => {} };
 
    return api;
  },
 
  /**
   * Get singleton instance (for backward compatibility)
   */
  getInstance: () => {
    return monitoringCore.getInstance();
  },
 
  // Export helpers
  createLogger: monitoringCore.createLogger,
  WorkflowTracker: monitoringCore.WorkflowTracker,
 
  // Export class for testing
  MonitoringConnector
};