npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dnlup/fastify-doc

v2.0.0

Published

A Fastify plugin for sampling process metrics.

Downloads

4

Readme

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
      • aggregate <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: false
      • flags <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). aggregate has to be true to enable this option. Default: true on Node version 12.17.0 and newer.
    • 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