Repository: immobiliare/fastify-metrics
Pull request #44 (2021-11-16 09:57:25)

Feat expose doc
Expose the process metrics sampler. I think it can be useful to perform custom logics using the process status.


Patch:
diff --git a/README.md b/README.md
index d6a8aaf..3690bd5 100644
--- a/README.md
+++ b/README.md
@@ -110,9 +110,10 @@ These are the metrics that are collected automatically.
 The plugin adds the following decorators:
 
 -   `fastify.stats`: A [dats](https://github.com/immobiliare/dats) instance
--   `fastify.hrtime2ns`: A utility function to convert `process.hrtime([time])` to nanoseconds
--   `fastify.hrtime2ms`: A utility function to convert `process.hrtime([time])` to milliseconds
--   `fastify.hrtime2s`: A utility function to convert `process.hrtime([time])` to seconds
+-   `fastify.doc`: A [doc](https://github.com/dnlup/doc) instance used to sample process metrics, if `options.collect.health` is `true
+-   `fastify.hrtime2ns`: A utility function to convert the legacy `process.hrtime([time])` value to nanoseconds
+-   `fastify.hrtime2ms`: A utility function to convert the legacy `process.hrtime([time])` value to milliseconds
+-   `fastify.hrtime2s`: A utility function to convert the legacy `process.hrtime([time])` value to seconds
 
 ## Hooks
 
diff --git a/index.d.ts b/index.d.ts
index 0425237..73e535b 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,7 +1,7 @@
 import { FastifyPluginCallback, FastifyPluginAsync } from 'fastify';
 
-import Client from '@immobiliarelabs/dats';
-import { Options } from '@immobiliarelabs/dats';
+import Client, { Options } from '@immobiliarelabs/dats';
+import { Sampler } from '@dnlup/doc';
 
 export interface MetricsPluginOptions extends Options {
     sampleInterval?: number;
@@ -21,6 +21,7 @@ export default trapsPluginCallback;
 declare module 'fastify' {
     interface FastifyInstance {
         stats: Client;
+        doc?: Sampler;
         hrtime2ns: (time: [number, number]) => number;
         hrtime2ms: (time: [number, number]) => number;
         hrtime2s: (time: [number, number]) => number;
diff --git a/index.js b/index.js
index 47acede..81f50e6 100644
--- a/index.js
+++ b/index.js
@@ -7,7 +7,9 @@ const { hrtime2ns, hrtime2ms, hrtime2s } = require('@dnlup/hrtime-utils');
 
 function clientMock() {
     const mock = {
-        socket: { onError: () => {} },
+        socket: {
+            onError: /* istanbul ignore next */ () => {},
+        },
     };
     for (const method of ['on', 'counter', 'timing', 'gauge', 'set']) {
         mock[method] = () => {};
@@ -118,6 +120,7 @@ module.exports = fp(
 
         if (enabledMetrics.health) {
             const sampler = doc({ sampleInterval });
+            fastify.decorate('doc', sampler);
             const onSample = function () {
                 sendHealthData(
                     {
diff --git a/package.json b/package.json
index 4234212..87fc7b4 100644
--- a/package.json
+++ b/package.json
@@ -26,7 +26,7 @@
     ],
     "scripts": {
         "lint": "eslint --fix --ignore-path .gitignore .",
-        "test": "tsd && nyc --reporter=lcov --reporter=text-summary ava && nyc check-coverage --lines 90 --branches 90 --functions 90",
+        "test": "tsd && nyc --reporter=lcov --reporter=text-summary ava && nyc check-coverage --lines 100 --branches 100 --functions 100",
         "bench:base": "concurrently -k -s first \"node benchmarks/fixtures/base.js\" \"node -e 'setTimeout(() => {}, 1000)' && npx autocannon -c 100 -d 30 http://localhost:3000\"",
         "bench": "concurrently -k -s first \"node benchmarks/fixtures/withMetrics.js\" \"node benchmarks/fixtures/statsd.js\" \"node -e 'setTimeout(() => {}, 1000)' && npx autocannon -c 100 -d 30 http://localhost:3001\"",
         "toc": "markdown-toc -i README.md",


Test patch:
diff --git a/index.test-d.ts b/index.test-d.ts
index 3cdd767..549828d 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -2,6 +2,7 @@ import { expectType } from 'tsd';
 
 import Fastify from 'fastify';
 import Client from '@immobiliarelabs/dats';
+import { Sampler } from '@dnlup/doc';
 import plugin from './index';
 
 const fastify = Fastify();
@@ -12,4 +13,5 @@ fastify.register(plugin).after((err) => {
     expectType<(time: [number, number]) => number>(fastify.hrtime2ms);
     expectType<(time: [number, number]) => number>(fastify.hrtime2ms);
     expectType<Client>(fastify.stats);
+    expectType<Sampler | undefined>(fastify.doc);
 });
diff --git a/tests/plugin.test.js b/tests/plugin.test.js
index f1756ad..d41c518 100644
--- a/tests/plugin.test.js
+++ b/tests/plugin.test.js
@@ -23,6 +23,7 @@ async function setup(options) {
 async function configMacro(t, options) {
     const server = await setup(options);
     t.true(server.hasDecorator('stats'));
+    t.true(server.hasDecorator('doc'));
     t.true(server.hasDecorator('hrtime2ns'));
     t.true(server.hasDecorator('hrtime2ms'));
     t.true(server.hasDecorator('hrtime2s'));
diff --git a/tests/util.test.js b/tests/util.test.js
deleted file mode 100644
index 60ad7b0..0000000
--- a/tests/util.test.js
+++ /dev/null
@@ -1,6 +0,0 @@
-'use strict';
-
-const test = require('ava');
-
-test.todo('hrtimeToNanoseconds');
-test.todo('hrtimeToMilliseconds');