@stephen-shopopop/node-metrics
    Preparing search index...

    Class Metrics<T>

    Singleton class for collecting and managing application metrics.

    The Metrics class provides a centralized mechanism to register metric plugins, periodically sample metrics, and expose collected data. It is designed to be instantiated only once via the static start method, enforcing a singleton pattern.

    • Use Metrics.start(options) to initialize and retrieve the singleton instance.
    • Register custom metric plugins using the register method.
    • Call measures() to retrieve the current metrics as JSON.
    • Use destroy() to clean up resources and stop metric collection.
    const metrics = Metrics.start({ resolution: 1000 });
    metrics.register(new CustomPlugin());
    const data = metrics.measures();

    By default, the class registers memory usage, event loop delay event loop utilization plugins, process uptime and process cpu usage. The sampling interval and resolution can be customized via the options parameter.

    Type Parameters

    Index

    Properties

    observer: MetricsObservable = ...

    Methods

    • Closes the web server used for exposing metrics, if it exists.

      This method asynchronously destroys the internal HTTP server instance that serves metrics endpoints. It is safe to call even if the server was never started.

      Returns Promise<void>

      A promise that resolves when the server has been closed.

    • Cleans up resources used by the Metrics instance.

      Specifically, this method clears the internal timer to prevent any further scheduled metric collection or processing. Should be called when the Metrics instance is no longer needed to avoid memory leaks or unintended behavior.

      Returns void

    • Initializes and starts the Metrics singleton instance with the provided options.

      • If the Metrics instance does not already exist, it creates a new one.
      • Sets the Node.js version information as a metric.
      • Optionally starts a metrics web server if webServerMetricsPort is specified in the options.

      Parameters

      • options: Readonly<Partial<Options>>

        Partial configuration options for initializing the Metrics instance.

      Returns Metrics<MetricsValues>

      The singleton instance of Metrics<MetricsValues>.