医生
获取关于您节点js过程的使用情况和健康数据 .
doc 是一个小模块,帮助您收集有关您节点js过程的健康指标.
它只使用节点本身(没有本土依赖)上可用的API来做到这一点.
它与APM平台没有任何联系,因此你可以随意使用任何你想要的用于此目的.
它的 API 允许您在可能时访问计算值和原始值.
- 安装 * 联合国 最新稳定版本 * 联合国 最新开发版本
- 使用量 * 联合国 正在用普通JS导入 * 联合国 采用无害环境管理的进口 * 联合国 说明 - 说吧 启用/ 无效的计量集合 - 说吧 垃圾收集 - 说吧 活动控点
- API 密码
doc([options])- 类 :
doc.Sampler - 类 :
CpuMetric - 类 :
ResourceUsageMetric - 类 :
EventLoopDelayMetric - 类 :
EventLoopUtilizationMetric - 类 :
GCMetric gcMetric.pause- 类 :
GCEntry - 类 :
GCAggregatedEntrynew GCAggregatedEntry()gcAggregatedEntry.flagsgcAggregatedEntry.flags.nogcAggregatedEntry.flags.constructRetainedgcAggregatedEntry.flags.forcedgcAggregatedEntry.flags.synchronousPhantomProcessinggcAggregatedEntry.flags.allAvailableGarbagegcAggregatedEntry.flags.allExternalMemorygcAggregatedEntry.flags.scheduleIdle
doc.errors- 诊断通道支持
- 许可证
安装
最新稳定版本
$ npm i @dnlup/doc
最新开发版本
$ npm i @dnlup/doc@next
使用量
您可以使用普通JS或无害环境管理导入模块.
默认 doc 返回 a Sampler 实例,用于收集cpu、内存使用、事件循环延迟和事件循环利用的度量.
正在用普通JS导入
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 保存在指定样本间隔取自的度量标准快照.
这种行为使案件变得非常活跃. 每次勾选时,都会有新的快照覆盖上一张.
启用/ 无效的计量集合
你可以禁用你不需要的度量衡.
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
})
需要时可以启用更多度量衡.
垃圾收集
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)
})
实例
您可以在 examples 文件夹.
API 密码
doc([options])
它创造了一个指标 Sampler 选项.
类 : doc.Sampler
- 扩展
EventEmitter。 。 。 .
量子标本.
它在正常间隔中收集所选的度量衡. A 类 Sampler 因此,每个勾选时,
只有最后一个样本的值。 每次样本都发出 sample 事件,它将覆盖上一个事件.
新设 doc.Sampler([options])
options<Object>sampleInterval<number>:样本间隔(ms)以获得样本. 每个sampleInterval编号 asample事件已释放. 默认 :1000在套件的盖子下monitorEventLoopDelay以跟踪事件循环延迟.autoStart<boolean>: 自动开始收集度量衡. 默认 :true。 。 。 .unref<boolean>数字 : 未读 用于安排采样间隔的计时器. 默认 :true。 。 。 .gcOptions<Object>: 垃圾收集选项aggregate<boolean>: 追踪和汇总每次垃圾收集作业的统计数据(见 https://nodejs.org/docs/latest-v18.x/api/perf hooks.html#perf hooks 性能进取 kind) (中文(简体) ). 默认 :falseflags<boolean>:,关于每个(汇总)垃圾收集作业的旗帜的轨迹统计(见 https://nodejs.org/docs/latest-v18.x/api/perf hooks.html#perf hooks 性能进取 flags) (中文(简体) ).aggregate必须是true以启用此选项. 默认 :true节点版本12.17.0更新鲜.
eventLoopDelayOptions<Object>设置选项monitorEventLoopDelay。 。 。 . 默认 :{ resolution: 10 }collect<Object>: 启用/禁用特定指标的收集.cpu<boolean>: 启用 cpu 度量衡. 默认 :true。 。 。 .resourceUsage<boolean>: 启用 资源用户 标准. 默认 :false。 。 。 .eventLoopDelay<boolean>: 启用事件LoopDelay 度量标准. 默认 :true。 。 。 .eventLoopUtilization<boolean>: 启用 事件循环 标准. 默认 :true节点版本12.19.0更新鲜.memory<boolean>: 启用内存度量衡. 默认 :true。 。 。 .gc<boolean>:启用垃圾收集度量标准. 默认 :false。 。 。 .activeHandles<boolean>: 启用活动手柄收集度量. 默认 :false。 。 。 .
若为 options.collect.resourceUsage 设置为 true, (中文(简体) ), options.collect.cpu 将会被设定为错误,因为 cpu 度量在 resource usage metric。 。 。 .
活动: 'sample'
每一次发件 sampleInterval,它表示采样者收集了新的数据.
sampler.start()
开始收集度量衡.
sampler.stop()
停止收集度量衡.
sampler.cpu
资源使用情况.
sampler.resourceUsage
资源使用情况.
sampler.eventLoopDelay
事件循环延后度量实例 .
sampler.eventLoopUtilization
事件循环利用度例.
sampler.gc
垃圾收集器的例子.
sampler.activeHandles
<number>
返回的有效手柄数量 process._getActiveHandles()。 。 。 .
sampler.memory
<object>
对象返回 process.memoryUsage()。 。 。 .
类 : CpuMetric
它暴露了cpu用法的计算值和原始值.
cpuMetric.usage
<number>
使用率(百分比).
cpuMetric.raw
<object>
返回的原始值 process.cpuUsage()。 。 。 .
类 : ResourceUsageMetric
它揭示了流程资源使用情况的计算值和原始值.
resourceUsage.cpu
<number>
使用率(百分比).
resourceUsage.raw
<object>
返回的原始值 process.resourceUsage()。 。 。 .
类 : EventLoopDelayMetric
它揭示了事件循环延迟的计算值和原始值.
eventLoopDelay.computed
<number>
事件循环延迟以毫秒计 。 它使用 mean 会 议 日 程 和 议 程 Histogram 举个例子.
eventLoopDelay.raw
<Histogram>
显示 Histogram 举个例子.
eventLoopDelay.compute(raw)
raw<number>使用HistogramAPI (英语).- 回返
<number>计算出延迟值 .
类 : EventLoopUtilizationMetric
它揭露了关于事件循环利用的统计数据.
eventLoopUtilization.idle
<number>
这个 idle 返回对象中的值 performance.eventLoopUtilization() 期间 sampleInterval 窗口.
eventLoopUtilization.active
<number>
这个 active 返回对象中的值 performance.eventLoopUtilization() 期间 sampleInterval 窗口.
eventLoopUtilization.utilization
<number>
这个 utilization 返回对象中的值 performance.eventLoopUtilization() 期间 sampleInterval 窗口.
eventLoopUtilization.raw
<object>
返回的原始值 performance.eventLoopUtilization() 期间 sampleInterval 窗口.
类 : GCMetric
它揭示了垃圾收集器活动统计数据 sampleInterval 使用hdr直方图.
new GCMetric(options)
options<object>配置选项
gcMetric.pause
它跟踪垃圾收集器的全球活动.
gcMetric.major
类型操作的活动 major。它只有在 GCMetric 已使用选项创建 aggregate 等于 true。 。 。 .
见 performanceEntry.kind。 。 。 .
gcMetric.minor
类型操作的活动 minor。它只有在 GCMetric 已使用选项创建 aggregate 等于 true。 。 。 .
见 performanceEntry.kind。 。 。 .
gcMetric.incremental
类型操作的活动 incremental。它只有在 GCMetric 已使用选项创建 aggregate 等于 true。 。 。 .
见 performanceEntry.kind。 。 。 .
gcMetric.weakCb
类型操作的活动 weakCb。它只有在 GCMetric 已使用选项创建 aggregate 等于 true。 。 。 .
见 performanceEntry.kind。 。 。 .
类 : GCEntry
它包含垃圾收集数据,以 直方图。所有计时值以纳秒表示.
new GCEntry()
初始化不需要选项. 它由内部生成 GCMetric。 。 。 .
gcEntry.totalDuration
<number>
它是以纳秒计的输入总时间.
gcEntry.totalCount
<number>
这是计算的行动总数.
gcEntry.mean
<number>
它是以纳秒计的条目的平均值.
gcEntry.max
<number>
它是该条目以纳秒计的最大值.
gcEntry.min
<number>
它是该条目以纳秒计的最小值.
gcEntry.stdDeviation
<number>
它是以纳秒计的条目的标准偏差.
gcEntry.getPercentile(percentile)
percentile<number>: 从直方图中取一个百分点.- 回返
<number>百分位数
类 : GCAggregatedEntry
它延伸 GCEntry 并包含垃圾收集数据加上与之相关的旗帜(参见 https://nodejs.org/dist/latest-v18.x/docs/api/perf hooks.html#性能登场活佛旗) (中文(简体) ).
new GCAggregatedEntry()
初始化不需要选项. 它由内部生成 GCMetric。 。 。 .
gcAggregatedEntry.flags
<object>
此天体包含每个旗帜的各种直方图.
gcAggregatedEntry.flags.no
gcAggregatedEntry.flags.constructRetained
gcAggregatedEntry.flags.forced
gcAggregatedEntry.flags.synchronousPhantomProcessing
gcAggregatedEntry.flags.allAvailableGarbage
gcAggregatedEntry.flags.allExternalMemory
gcAggregatedEntry.flags.scheduleIdle
doc.errors
在那个 errors 对象导出模块使用的所有自定义错误.
| 错误 | 错误代码 | 说明 |
|---|---|---|
InvalidArgumentError | DOC_ERR_INVALID_ARG | 使用了无效的选项或参数 |
NotSupportedError | DOC_ERR_NOT_SUPPORTED | 在所使用的节点.js版本上不支持一个参数 |
诊断通道支持
节点 诊断通道 被支持.
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()