proactive-deps
    Preparing search index...

    Class DependencyMonitor

    DependencyMonitor is a class that monitors the status of various dependencies (e.g., databases, APIs) and provides methods to check their health and latency. It uses a cache to store the results of the checks and can be configured to refresh the cache at specified intervals. It also provides a method to get Prometheus metrics for the monitored dependencies. DependencyMonitor

    Implements

    Index

    Constructors

    • Creates an instance of DependencyMonitor.

      Parameters

      Returns DependencyMonitor

      { cacheDurationMs: 60000, refreshThresholdMs: 5000, checkIntervalMs: 15000 }
      
      const monitor = new DependencyMonitor({
      cacheDurationMs: 60000, // Cache duration of 1 minute
      refreshThresholdMs: 5000, // Refresh threshold of 5 seconds
      checkIntervalMs: 15000, // Check interval of 15 seconds
      });

    Properties

    checkIntervalStarted: boolean = false

    A flag indicating whether the check interval has started.

    const monitor = new DependencyMonitor();
    console.log(monitor.checkIntervalStarted); // false
    monitor.startDependencyCheckInterval();
    console.log(monitor.checkIntervalStarted); // true

    Methods

    • Retrieves the statuses of all registered dependencies.

      Returns Promise<DependencyStatus[]>

      • A promise that resolves to an array of dependency statuses.
      const statuses = await monitor.getAllStatuses();
      console.log(statuses);
      // Output:
      // [
      // { name: 'Database', healthy: true, code: 0, status: 'OK', ... },
      // { name: 'API', healthy: false, code: 1, status: 'ERROR', ... },
      // ]
    • Retrieves the Prometheus metrics for all dependencies.

      Returns Promise<string>

      • A promise that resolves to a string containing the Prometheus metrics.
      const prometheusMetrics = await monitor.getPrometheusMetrics();
      console.log(prometheusMetrics);
      // Output:
      // # HELP dependency_health The status of the database connection (0 = Healthy, 1 = Unhealthy)
      // # TYPE dependency_health gauge
      // dependency_health{dependency="Database", impact="Database info will not be available."} 0
      // # HELP dependency_latency The latency of the database connection check
      // # TYPE dependency_latency gauge
      // dependency_latency{dependency="Database"} 50
    • Retrieves the status of a specific dependency by name.

      Parameters

      • dependencyName: string

        The name of the dependency to check.

      Returns Promise<DependencyStatus>

      • A promise that resolves to the status of the specified dependency.
      const status = await monitor.getStatus('Database');
      console.log(status);
      // Output:
      // { name: 'Database', healthy: true, code: 0, status: 'OK', ... }
    • Registers a new dependency to be monitored.

      Parameters

      Returns void

      monitor.register({
      name: 'Database',
      description: 'Database connection check',
      impact: 'Database data will be unavailable.',
      check: async () => {
      // Perform some check (e.g., ping a database)
      return SUCCESS_STATUS_CODE;
      },
      });