Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@dnlup/doc
Advanced tools
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 provided by Node itself.
It is not coupled with any APM platform so you are free to use anything you want for that purpose.
Its API is designed to let you access both computed and raw values, where possible.
$ npm i @dnlup/doc
$ npm i @dnlup/doc@next
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(); // Use the default options
sampler.on('sample', () => {
doStuffWithCpuUsage(sampler.cpu.usage)
doStuffWithMemoryUsage(sampler.memory)
doStuffWithEventLoopDelay(sampler.eventLoopDelay.computed)
doStuffWithEventLoopUtilization(sampler.eventLoopUtilization.raw) // Available only on Node versions that support it
})
A Sampler
holds a snapshot of the metrics taken at the specified sample interval.
This 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)
})
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)
doStuffWithGarbageCollectionDuration(sampler.gc)
})
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)
doStuffWithActiveHandles(sampler.activeHandles)
})
Create a new metrics Sampler
instance with the given options.
doc.Sampler
EventEmitter
.Metrics sampler.
It collects the selected metrics on a regular interval. A Sampler
instance is stateful so, on each tick,
only the values of the last sample are available. The old ones are overwritten each time a new sample
event is emited.
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
.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
.sample
'Emitted every sampleInterval
, it signals that new data has been sampled.
sampler.start()
Start collecting metrics.
sampler.stop()
Stop collecting metrics.
sampler.cpu
Cpu 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>
Number of active handles returned by process._getActiveHandles()
.
sampler.memory
<object>
Object returned by process.memoryUsage()
.
CpuMetric
Exposes both computed and raw values of the cpu usage.
cpuMetric.usage
<number>
Cpu usage in percentage.
cpuMetric.raw
<object>
Raw value returned by process.cpuUsage()
.
EventLoopDelayMetric
Exposes both computed and raw values about the event loop delay.
eventLoopDelay.computed
<number>
Event loop delay in milliseconds. On Node versions that support monitorEventLoopDelay
this value is computed using the mean
of the Histogram
instance, otherwise a simple timer is used to calculate it.
eventLoopDelay.raw
<Histogram|number>
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.<number>
The computed delay value.This method is meant to be used only on node versions that supports monitorEventLoopDelay
. It allows to get computed values of the event loop delay from other values than the mean
of the Histogram
instance.
EventLoopUtilizationMetric
Exposes raw values about the event loop utilization.
eventLoopUtilization.raw
<object>
Raw value returned by performance.eventLoopUtilization()
during the sampleInterval
window.
GCMetric
Exposes the garbage collector activity only with computed values. The rolling average of each type of operation is calculated during the specified sampleInterval
.
gcMetric.major
Activity of the operation of type major
.
gcMetric.minor
Activity of the operation of type minor
.
gcMetric.incremental
Activity of the operation of type incremental
.
gcMetric.weakCb
Activity of the operation of type weakCb
.
GCAggregatedEntry
Entry containing aggregated data about a specific garbage collector operation.
gcAggregatedEntry.count
<number>
The number of times the operation occurred.
gcAggregatedEntry.total
<number>
The total time (in milliseconds) spent on the operation.
gcAggregatedEntry.average
<number>
The average time (in milliseconds) spent each time in the operation.
gcAggregatedEntry.flags
<Map>
On Node versions that support flags
this Map
is populated with additional metrics about the number of times a specific flag was encountered 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'
Each value of the Map
is an <object>
with the following properties:
count
<number>
The number of time that the flag was encountered.
total
<number>
The total time (in milliseconds) spent on the operations with this flag.
When writing this module a lot of inspiration was taken from the awesome Node Clinic Doctor package.
FAQs
Get usage and health data about your Node.js process
The npm package @dnlup/doc receives a total of 280 weekly downloads. As such, @dnlup/doc popularity was classified as not popular.
We found that @dnlup/doc demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.