Use metrics with Node.js's `cluster` module
mainWhen using the cluster module, metrics from individual workers are isolated. To provide a unified view, use ClusterRegistry to aggregate metrics in the primary process.
Key requirements:
- Initialization: Instantiate
ClusterRegistrybefore branching oncluster.isPrimary. - IPC Listener: The
ClusterRegistryconstructor automatically installs the necessary IPC listeners in each process. If you only instantiate it in the primary process, workers will not be able to respond to aggregation requests, causingclusterMetrics()to time out. - Aggregation Methods:
- Custom metrics are summed across workers by default.
- You can change the aggregation method by setting the
aggregatorproperty in the metric configuration to one of:'sum','first','min','max','average', or'omit'. - Note: Default metrics (like event loop lag) use sensible aggregation, but mean and percentiles are averaged, which may not be perfectly accurate.
- Worker-specific metrics: To track individual workers, include a unique identifier (like
cluster.worker.idor the process ID) as a label. - Registry Configuration: Metrics are aggregated from the global registry by default. To use different registries, call
client.AggregatorRegistry.setRegistries(registryOrArrayOfRegistries)from the worker processes.