doc
Get usage and health data about your Node.js process.
doc
is a small module that helps you collect health metrics about your Node.js process.
It does that by using only the API available on Node itself (i.e. no native dependencies).
It doesn't have any ties with an APM platform, so you are free to use anything you want for that purpose.
Its API lets you access both computed and raw values, where possible.
Installation
latest stable version
$ npm i @dnlup/doc
latest development version
$ npm i @dnlup/doc@next
Usage
By default doc
returns a Sampler
instance that collects metrics about cpu, memory usage, event loop delay and event loop utilization (only on Node versions that support it).
const doc = require('@dnlup/doc')
const sampler = doc()
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.raw)
})
A Sampler
holds a snapshot of the metrics taken at the specified sample interval.
This behavior makes the instance stateful. On every tick, a new snapshot will overwrite the previous one.
Enable/disable metrics collection
You can disable the metrics that you don't need.
const doc = require('@dnlup/doc')
const sampler = doc({ collect: { cpu: false, memory: false } })
sampler.on('sample', () => {
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.raw)
})
You can enable more metrics if you need them.
Garbage collection
const doc = require('@dnlup/doc')
const sampler = doc({ collect: { gc: true } })
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.raw)
doStuffWithGarbageCollectionDuration(sampler.gc)
})
Active handles
const doc = require('@dnlup/doc')
const sampler = doc({ collect: { activeHandles: true } })
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.raw)
doStuffWithActiveHandles(sampler.activeHandles)
})
API
doc([options])
It creates a metrics Sampler
instance with the given options.
Class: doc.Sampler
Metrics sampler.
It collects the selected metrics at a regular interval. A Sampler
instance is stateful so, on each tick,
only the values of the last sample are available. Each time the sampler emits the sample
event, it will overwrite the previous one.
new doc.Sampler([options])
options
<Object>
sampleInterval
<number>
: sample interval (ms) to get a sample. On each sampleInterval
ms a sample
event is emitted. Default: 500
on Node < 11.10.0, 1000
otherwise. Under the hood the package uses monitorEventLoopDelay
when available to track the event loop delay and this allows to increase the default sampleInterval
.autoStart
<boolean>
: start automatically to collect metrics. Default: true
.unref
<boolean>
: unref the timer used to schedule the sampling interval. Default: true
.eventLoopOptions
<Object>
: Options to setup monitorEventLoopDelay
. Default: { resolution: 10 }
collect
<Object>
: enable/disable the collection of specific metrics.
cpu
<boolean>
: enable cpu metric. Default: true
.resourceUsage
<boolean>
: enable resourceUsage metric. Default: false
.eventLoopDelay
<boolean>
: enable eventLoopDelay metric. Default: true
.eventLoopUtilization
<boolean>
: enable eventLoopUtilization metric. Default: true
on Node versions that support it.memory
<boolean>
: enable memory metric. Default: true
.gc
<boolean>
: enable garbage collection metric. Default: false
.activeHandles
<boolean>
: enable active handles collection metric. Default: false
.
If options.collect.resourceUsage
is set to true
, options.collect.cpu
will be set to false because the cpu metric is already available in the resource usage metric
.
Event: 'sample
'
Emitted every sampleInterval
, it signals that new data the sampler has collected new data.
sampler.start()
Start collecting metrics.
sampler.stop()
Stop collecting metrics.
sampler.cpu
Resource usage metric instance.
sampler.resourceUsage
Resource usage metric instance.
sampler.eventLoopDelay
Event loop delay metric instance.
sampler.eventLoopUtilization
Event loop utilization metric instance.
sampler.gc
Garbage collector metric instance.
sampler.activeHandles
Number of active handles returned by process._getActiveHandles()
.
sampler.memory
Object returned by process.memoryUsage()
.
Class: CpuMetric
It exposes both computed and raw values of the cpu usage.
cpuMetric.usage
Cpu usage in percentage.
cpuMetric.raw
Raw value returned by process.cpuUsage()
.
Class: ResourceUsageMetric
It exposes both computed and raw values of the process resource usage.
resourceUsage.cpu
Cpu usage in percentage.
resourceUsage.raw
Raw value returned by process.resourceUsage()
.
Class: EventLoopDelayMetric
It exposes both computed and raw values about the event loop delay.
eventLoopDelay.computed
Event loop delay in milliseconds. On Node versions that support monitorEventLoopDelay
, it computes this value using the mean
of the Histogram
instance. Otherwise, it uses a simple timer to calculate it.
eventLoopDelay.raw
On Node versions that support monitorEventLoopDelay
this exposes the Histogram
instance. Otherwise, it exposes the raw delay value in nanoseconds.
eventLoopDelay.compute(raw)
raw
<number>
The raw value obtained using the Histogram
API.- Returns
<number>
The computed delay value.
This function works only on node versions that support monitorEventLoopDelay
. It allows to get computed values of the event loop delay from statistics other than the mean
of the Histogram
instance.
Class: EventLoopUtilizationMetric
It exposes raw values about the event loop utilization.
eventLoopUtilization.raw
Raw value returned by performance.eventLoopUtilization()
during the sampleInterval
window.
Class: GCMetric
It exposes the garbage collector activity only with computed values. It calculates the rolling average of each type of operation during the specified sampleInterval
.
gcMetric.major
The activity of the operation of type major
.
See performanceEntry.kind
.
gcMetric.minor
The activity of the operation of type minor
.
See performanceEntry.kind
.
gcMetric.incremental
The activity of the operation of type incremental
.
See performanceEntry.kind
.
gcMetric.weakCb
The activity of the operation of type weakCb
.
See performanceEntry.kind
.
Class: GCAggregatedEntry
It contains aggregated data about a specific garbage collector operation.
gcAggregatedEntry.count
The number of times the operation occurred.
gcAggregatedEntry.total
The total time (in milliseconds) spent on the operation.
gcAggregatedEntry.average
The average time (in milliseconds) spent each time in the operation.
gcAggregatedEntry.flags
On Node versions that support flags
this Map
is populated with additional metrics about the number of times it encounters a specific flag and the total time (in milliseconds) spent on the operation with this flag.
Each key of the Map
is one of these strings:
'no'
'constructRetained'
'forced'
'synchronousPhantomProcessing'
'allAvailableGarbage'
'allExternalMemory'
'scheduleIdle'
See performanceEntry.flags
.
Each value of the Map
is an <object>
with the following properties:
count
The number of times that it has encountered the flag.
total
The total time (in milliseconds) spent on the operations with this flag.
doc.eventLoopUtilizationSupported
Whether the Node.js version in use supports the eventLoopUtilization metric.
doc.resourceUsageSupported
Whether the Node.js version in use supports the resourceUsage metric.
doc.gcFlagsSupported
Whether the Node.js version in use supports GC flags.
Credits
When writing this module, I took a lot of inspiration from the fantastic Node Clinic Doctor package.