@dnlup/fastify-doc v2.0.0
fastify-doc
A Fastify plugin for sampling process metrics.
It uses the module @dnlup/doc behind the scenes.
It decorates the Fastify instance with a Sampler
instance that you can use to get process metrics.
A new sample of the metrics is available when the Sampler emits a sample event.
See @dnlup/doc
documentation for more details.
Install
npm i @dnlup/fastify-docUsage
Example 1
const fastify = require('fastify')()
const metrics = require('@dnlup/fastify-doc')
fastify.register(metrics)
fastify.register(function myReporter (instance, opts, next) {
instance.metrics.on('sample', () => {
// sendCpuUsage(instance.metrics.cpu.usage)
// ...send other metrics as well
})
next()
})
fastify.get('/', (request, reply) => {
reply.send({ ok: true })
})
fastify.listen(3000)Example 2
const fastify = require('fastify')()
const metrics = require('@dnlup/fastify-doc')
fastify.register(async function myReporter (instance, opts) {
await instance.register(metrics)
instance.metrics.on('sample', () => {
// sendCpuUsage(instance.metrics.cpu.usage)
// ...send other metrics as well
})
})
fastify.get('/', (request, reply) => {
reply.send({ ok: true })
})
fastify.listen(3000)Plugin options
The options are the same of @dnlup/doc (reported here for convenience):
options<Object>sampleInterval<number>: sample interval (ms) to get a sample. On eachsampleIntervalms asampleevent is emitted. Default:500on Node < 11.10.0,1000otherwise. Under the hood the package usesmonitorEventLoopDelaywhen available to track the event loop delay and this allows to increase the defaultsampleInterval.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-v12.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-v12.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.
Decorators
metrics
A Sampler instance.
eventLoopUtilizationSupported
<boolean>
Whether the Node.js version in use supports the eventLoopUtilization metric.
resourceUsageSupported
<boolean>
Whether the Node.js version in use supports the resourceUsage metric.
gcFlagsSupported
<boolean>
Whether the Node.js version in use supports GC flags.
Hooks
onClose
Stops the Sampler instance when closing the server.