Monitor
The @esmj/monitor
is module for collecting node metrics from native node API.
Requirements
Install
npm install @esmj/monitor
Usage
It works for both Javascript modules (ESM and CJS).
import { createMonitoring } from '@esmj/monitor';
const { monitor, metricsHistory } = new createMonitoring();
const { unsubscribe } = monitor.subscribe((metrics) => {
console.log(metrics);
});
monitor.start();
setTimeout(() => {
console.log(metricsHistory.percentile('cpuUsage.percent', 80));
console.log(metricsHistory.percentile('memoryUsage.rss', 80));
}, 5000);
setTimeout(() => {
unsubscribe();
monitor.stop();
console.log(metricsHistory.percentile('cpuUsage.percent', 80));
console.log(metricsHistory.percentile('memoryUsage.rss', 80));
}, 15000);
API
monitor = new Monitor(options?)
Create a new instance of Monitor.
options?
Type: object
Configure options for the new instance of Monitor.
options.interval?
Type: integer
Default: 1000
Measure interval metric.
start()
Monitoring start measure node metric.
stop()
Monitoring stop measure node metric.
subscribe(listener)
Subscribe listener for metrics.
Returns a subscription object with unsubscribe method.
unsubscribe(listener)
Remove subscription.
add(metric)
Monitoring add measure new metric.
metric
Type: Metric
Add new metric to monitoring.
remove(metric)
Remove defined metric from monitoring.
metricsHistory = new MetricsHistory(options.metricsHistory?)
Create a new instance of Monitor.
options?
Type: object
Configure options for the new instance of MetricsHistory.
options.limit?
Type: integer
Default: 60
FIFO size of array for calculating percentile and linear regressions.
percentile(key, number)
Returns defined percentile for measured metric
key
Type: String
Path in measured metric structure.
number
Type: Number
Percentile number for FIFO array
trend(key, limit)
Returns linear regression variables slope
, yIntercept
and predict
function for measured metric.
key
Type: String
Path in measured metric structure.
limit
Type: Number
Defined how much records use for calculating linear regression. Default is use all records from FIFO array.