Skip to content

dnlup/fastify-doc

Repository files navigation

fastify-doc

npm version Tests codecov Known Vulnerabilities

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-doc

Usage

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 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.
    • gcOptions <Object>: Garbage collection options
    • eventLoopDelayOptions <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 version 12.19.0 and 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.

License

ISC

About

A Fastify plugin for sampling process metrics.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published