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 (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 * latest development version
- Usage * Importing with CommonJS * Importing with ESM * Note - Enable/disable metrics collection - Garbage collection - Active handles
- API
doc([options])- Class:
doc.Sampler - Class:
CpuMetric - Class:
ResourceUsageMetric - Class:
EventLoopDelayMetric - Class:
EventLoopUtilizationMetric - Class:
GCMetric gcMetric.pause- Class:
GCEntry - Class:
GCAggregatedEntrynew GCAggregatedEntry()gcAggregatedEntry.flagsgcAggregatedEntry.flags.nogcAggregatedEntry.flags.constructRetainedgcAggregatedEntry.flags.forcedgcAggregatedEntry.flags.synchronousPhantomProcessinggcAggregatedEntry.flags.allAvailableGarbagegcAggregatedEntry.flags.allExternalMemorygcAggregatedEntry.flags.scheduleIdle
doc.errors- Diagnostics Channel support
- License
$ npm i @dnlup/doc$ npm i @dnlup/doc@nextYou can import the module by using either CommonJS or ESM.
By default doc returns a Sampler instance that collects metrics about cpu, memory usage, event loop delay and event loop utilization.
const doc = require('@dnlup/doc')
const sampler = doc() // Use the default options
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.utilization) // Available only on Node versions that support it
})import doc from '@dnlup/doc'
const sampler = doc()
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.utilization) // Available only on Node versions that support it
})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.
You can disable the metrics that you don't need.
const doc = require('@dnlup/doc')
// Collect only the event loop delay
const sampler = doc({ collect: { cpu: false, memory: false } })
sampler.on('sample', () => {
// `sampler.cpu` will be `undefined`
// `sampler.memory` will be `undefined`
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.utilization) // Available only on Node versions that support it
})You can enable more metrics if you need them.
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.utilization) // Available only on Node versions that support it
doStuffWithGarbageCollectionDuration(sampler.gc.pause)
})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.utilization) // Available only on Node versions that support it
doStuffWithActiveHandles(sampler.activeHandles)
})You can find more examples in the examples folder.
It creates a metrics Sampler instance with the given options.
- Extends
EventEmitter.
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.
options<Object>sampleInterval<number>: sample interval (ms) to get a sample. On eachsampleIntervalms asampleevent is emitted. Default:1000Under the hood the package usesmonitorEventLoopDelayto track the event loop delay.autoStart<boolean>: start automatically to collect metrics. Default:true.unref<boolean>: unref the timer used to schedule the sampling interval. Default:true.gcOptions<Object>: Garbage collection optionsaggregate<boolean>: Track and aggregate statistics about each garbage collection operation (see https://nodejs.org/docs/latest-v18.x/api/perf_hooks.html#perf_hooks_performanceentry_kind). Default:falseflags<boolean>: , Track statistics about the flags of each (aggregated) garbage collection operation (see https://nodejs.org/docs/latest-v18.x/api/perf_hooks.html#perf_hooks_performanceentry_flags).aggregatehas to betrueto enable this option. Default:trueon Node version12.17.0and newer.
eventLoopDelayOptions<Object>: Options to setupmonitorEventLoopDelay. 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:trueon Node version12.19.0and newer.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.
Emitted every sampleInterval, it signals that new data the sampler has collected new data.
Start collecting metrics.
Stop collecting metrics.
Resource usage metric instance.
Resource usage metric instance.
Event loop delay metric instance.
Event loop utilization metric instance.
Garbage collector metric instance.
<number>
Number of active handles returned by process._getActiveHandles().
<object>
Object returned by process.memoryUsage().
It exposes both computed and raw values of the cpu usage.
<number>
Cpu usage in percentage.
<object>
Raw value returned by process.cpuUsage().
It exposes both computed and raw values of the process resource usage.
<number>
Cpu usage in percentage.
<object>
Raw value returned by process.resourceUsage().
It exposes both computed and raw values about the event loop delay.
<number>
Event loop delay in milliseconds. It computes this value using the mean of the Histogram instance.
<Histogram>
Exposes the Histogram instance.
raw<number>The raw value obtained using theHistogramAPI.- Returns
<number>The computed delay value.
It exposes statistics about the event loop utilization.
<number>
The idle value in the object returned by performance.eventLoopUtilization() during the sampleInterval window.
<number>
The active value in the object returned by performance.eventLoopUtilization() during the sampleInterval window.
<number>
The utilization value in the object returned by performance.eventLoopUtilization() during the sampleInterval window.
<object>
Raw value returned by performance.eventLoopUtilization() during the sampleInterval window.
It exposes the garbage collector activity statistics in the specified sampleInterval using hdr histograms.
options<object>: Configuration optionsaggregate<boolean>: SeegcOptions.aggregatein the Sampler options.flags<boolean>: SeegcOptions.flagsin the Sampler options.
It tracks the global activity of the garbage collector.
The activity of the operation of type major. It's present only if GCMetric has been created with the option aggregate equal to true.
The activity of the operation of type minor. It's present only if GCMetric has been created with the option aggregate equal to true.
The activity of the operation of type incremental. It's present only if GCMetric has been created with the option aggregate equal to true.
The activity of the operation of type weakCb. It's present only if GCMetric has been created with the option aggregate equal to true.
It contains garbage collection data, represented with an histogram. All timing values are expressed in nanoseconds.
The initialization doesn't require options. It is created internally by a GCMetric.
<number>
It is the total time of the entry in nanoseconds.
<number>
It is the total number of operations counted.
<number>
It is the mean value of the entry in nanoseconds.
<number>
It is the maximum value of the entry in nanoseconds.
<number>
It is the minimum value of the entry in nanoseconds.
<number>
It is the standard deviation of the entry in nanoseconds.
percentile<number>: Get a percentile from the histogram.- Returns
<number>The percentile
It extends GCEntry and contains garbage collection data plus the flags associated with it (see https://nodejs.org/dist/latest-v18.x/docs/api/perf_hooks.html#performanceentryflags).
The initialization doesn't require options. It is created internally by a GCMetric.
<object>
This object contains the various histograms of each flag.
In the errors object are exported all the custom errors used by the module.
| Error | Error Code | Description |
|---|---|---|
InvalidArgumentError |
DOC_ERR_INVALID_ARG |
An invalid option or argument was used |
NotSupportedError |
DOC_ERR_NOT_SUPPORTED |
A metric is not supported on the Node.js version used |
Node diagnostics channel are supported.
const diagnosticsChannel = require('diagnostics_channel')
const doc = require('@dnlup/doc)
diagnosticsChannel.subscribe(doc.constants.DOC_CHANNEL, s => {
console.log('A new instance', s)
})
diagnosticsChannel.subscribe(doc.constants.DOC_SAMPLES_CHANNEL, s => {
console.log('A new sample', s)
})
doc()