@metrics/process
v2.2.1
Published
Module for collecting different process and system metrics and providing them as a metric stream
Downloads
4,229
Readme
@metrics/process
Module for collecting different process and system metrics providing them as a metric stream.
Installation
$ npm install @metrics/process
Example
const stream = require('stream');
const Process = require('@metrics/process');
const proc = new Process();
proc.pipe(new stream.Writable({
objectMode: true,
write(chunk, encoding, callback) {
console.log(chunk);
callback();
},
}));
proc.start();
Description
This module collects different process and system metrics on a scheduled frequency. The module is a readable stream which each metrics is emitted on as an metric object on each schedule.
The stream of metrics can be piped into ex the @metrics/client or the @metrics/emitter for further distribution.
Example:
const Process = require('@metrics/process');
const Client = require('@metrics/client');
const Emitter = require('@metrics/emitter');
const proc = new Process();
const client = new Client();
const emitter = new Emitter('udp', { port: 45000 });
proc.pipe(client).pipe(emitter);
proc.start();
Each metric is collected by a collector. Most collectors will provide metrics on each scheduled run or when the underlaying feature for generating the metric emits to build the metric out of, but some metrics will only run once due to its nature. Some metrics will not be collected on some operating systems. Please see collectors for further detail.
On not staying alive
The internal scheduler is a defered interval which prevents kicking off the collection of a new set of metrics before any previous collection of metrics has finished. This prevents duplicate metrics and possible memory leaks if any of the async operations of collecting the metrics get stale for some weird reason.
The scheduler is by default unrefered so it will not hold up your node.js process.
Constructor
Create a new Process instance.
const Process = require('@metrics/process');
const proc = new Process(options);
options (optional)
An Object containing misc configuration. The following values can be provided:
- interval -
Number
- Time between each collection of process metrics in milliseconds. Default: 10000ms. - prefix -
String
- A prefix to be added to each metrics name.
returns
Returns a Readable stream in object mode.
API
The Process instance has the following API:
.start(options)
Starts the scheduling of metric collection. The first run of metric collection will run immediately upon calling this method.
options (optional)
An Object containing misc configuration. The following values can be provided:
- gc -
Boolean
- Turns collection of gc metrics on or off. Default: false.
.stop()
Stops the scheduling of metric collection. Calling this method will not break the stream pipeline.
Events
An instance of the object will emit the following events:
drop
Emitted when the process is dropping metrics. Will emit the dropped metric.
Example
const process = new Process();
process.on('drop', metric => {
console.log('dropped metric', metric);
});
collect:start
Emitted when the process starts collecting metrics.
Example
const process = new Process();
process.on('collect:start', () => {
console.log('Started collecting metrics');
});
process.start();
collect:end
Emitted when the process is done collecting metrics.
Example
const process = new Process();
process.on('collect:end', () => {
console.log('Ended collecting metrics');
});
process.start();
Collectors
These are the following mertics collected by this module:
Version
The Version collector emits a metric with the node.js version used to run the process.
- metric name: nodejs_version_info
- collected when: Only once
- collected on: All operating systems
V8 heap
The V8 Heap collector emits metrics about the V8 heap spaces.
Metric I:
- metric name: nodejs_heap_space_size_total_bytes
- collected when: On every collect
- collected on: All operating systems
Metric II:
- metric name: nodejs_heap_space_size_used_bytes
- collected when: On every collect
- collected on: All operating systems
Metric III:
- metric name: nodejs_heap_space_size_available_bytes
- collected when: On every collect
- collected on: All operating systems
Process start time
The Process start time collector emits a metric for when the node.js process started.
- metric name: process_start_time_seconds
- collected when: Only once
- collected on: All operating systems
Resident memory
The Resident memory collector emits metrics with resident memory in bytes, virtual memory in bytes and process heap size in bytes.
Metric I:
- metric name: process_resident_memory_bytes
- collected when: On every collect
- collected on: All operating systems
Metric II:
- metric name: process_virtual_memory_bytes
- collected when: On every collect
- collected on: Linux only
Metric III:
- metric name: process_heap_bytes
- collected when: On every collect
- collected on: Linux only
Open file descriptors
The Open file descriptor collector emits a metric with the number of open file descriptors.
- metric name: process_open_fds
- collected when: On every collect
- collected on: Linux only
Max file descriptors
The max file descriptor collector emits a metric with the maximum number of file descriptors that can be opened.
- metric name: process_max_fds
- collected when: Only once
- collected on: Linux only
Heap used and size
The Heap used and size collector emits metrics about the memory usage of the Node.js process.
Metric I:
- metric name: nodejs_heap_size_total_bytes
- collected when: On every collect
- collected on: All operating systems
Metric II:
- metric name: nodejs_heap_size_used_bytes
- collected when: On every collect
- collected on: All operating systems
Metric III:
- metric name: nodejs_external_memory_bytes
- collected when: On every collect
- collected on: All operating systems
Eventloop lag
The Eventloop lag collector emits a metric with the lag of the eventloop in seconds.
- metric name: nodejs_eventloop_lag_seconds
- collected when: On every collect
- collected on: All operating systems
CPU total
The CPU total collector emits a metric with the user and system CPU time usage of the current process. Values are in seconds.
Metric I:
- metric name: process_cpu_user_seconds_total
- collected when: On every collect
- collected on: All operating systems
Metric II:
- metric name: process_cpu_system_seconds_total
- collected when: On every collect
- collected on: All operating systems
Metric III:
- metric name: process_cpu_seconds_total
- collected when: On every collect
- collected on: All operating systems
Active requests
The Active requests collector emits a metric with the number of open network requests held by the node.js working queue.
- metric name: nodejs_active_requests_total
- collected when: On every collect
- collected on: All operating systems if
process._getActiveRequests()
is available
Active handles
The Active handles collector emits a metric with the number of open handles (such as
setTimeout
etc) held by the node.js working queue.
- metric name: nodejs_active_handles_total
- collected when: On every collect
- collected on: All operating systems if
process._getActiveRequests()
is available
Garbage collection
The garbage collection (GC) collector emits a metric timing the length of the GC everytime a GC has run.
Do note that collecting metrics on GC has a performance impact on the system in it self. Its adviced that collecting GC metrics should be done in moderation and for small periods of time.
Collecting GC metrics is by default turned off. It can be enabled by the gc
argument on
the .start()
method.
- metric name: nodejs_gc_duration_seconds
- collected when: When enabled, every time a GC is done
- collected on: All operating systems
Attribution
Most of the metric collectors in this module originate in the process collectors in prom-client. prom-client is licensed under a Apache License 2.0, which is included in our license.
Due to the above fact, most metrics in this module bear the same name as the process metrics in prom-client. If one uses this module to provide metrics to Prometheus, these metrics will provide the same metrics as by prom-client.