diff --git a/README.md b/README.md index a84e073..5db6a5b 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,15 @@ It automatically collects Node.js process metrics along with routes hit count, t -- [fastify-metrics](#fastify-metrics) - - [Installation](#installation) - - [Usage](#usage) - - [Notes](#notes) - - [Metrics collected](#metrics-collected) - - [Decorators](#decorators) - - [Hooks](#hooks) - - [API](#api) - - [Configuration `options`](#configuration-options) - - [Contributing](#contributing) +- [Installation](#installation) +- [Usage](#usage) + - [Notes](#notes) +- [Metrics collected](#metrics-collected) +- [Decorators](#decorators) +- [Hooks](#hooks) +- [API](#api) + - [Configuration `options`](#configuration-options) +- [Contributing](#contributing) @@ -118,7 +117,7 @@ This module exports a [plugin registration function](https://github.com/fastify/ - `bufferSize`: Number. Metrics buffer size. See [dats](https://github.com/immobiliare/dats#new-clientoptions). - `bufferFlushTimeout`: Number. Metrics buffer flush timeout. See [dats](https://github.com/immobiliare/dats#new-clientoptions). - `sampleInterval`: Number. Optional. Sample interval in `ms` used to gather process stats. Defaults to `1000`. -- `afterSend`: Function: `(err) => void`. Optional. This function is called after each metric send. Default: `(err) => err && log(err)` +- `onError`: Function: `(err) => void`. Optional. This function to handle possible Dats errors. Default: `(err) => log(err)` - `udpDnsCache`: Boolean. Optional. Activate udpDnsCache. Default `true`. - `udpDnsCacheTTL`: Number. Optional. DNS cache Time to live of an entry in seconds. Default `120`. - `collect`: Object. Optional. Which metrics the plugin should track. diff --git a/index.js b/index.js index b9e4f44..cb9d7d1 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,9 @@ const doc = require('@dnlup/doc'); const { hrtime2ns, hrtime2ms, hrtime2s } = require('@dnlup/hrtime-utils'); function clientMock() { - const mock = {}; + const mock = { + socket: { onError: () => {} }, + }; for (const method of ['on', 'counter', 'timing', 'gauge', 'set']) { mock[method] = () => {}; } @@ -88,7 +90,7 @@ module.exports = fp( udpDnsCache, udpDnsCacheTTL, collect = {}, - afterSend = (error) => void (error && fastify.log.error(error)), + onError = (error) => void fastify.log.error(error), }, next ) { @@ -108,7 +110,7 @@ module.exports = fp( bufferFlushTimeout, udpDnsCache, udpDnsCacheTTL, - afterSend: afterSend, + onError: onError, }) : clientMock(); diff --git a/tests/plugin.test.js b/tests/plugin.test.js index 9a6732e..f1756ad 100644 --- a/tests/plugin.test.js +++ b/tests/plugin.test.js @@ -171,7 +171,7 @@ test.serial('should log statsd client errors', async (t) => { namespace: 'ns', }); const spy = sinon.spy(server.log, 'error'); - server.stats.afterSend(new Error('test')); + server.stats.socket.onError(new Error('test')); t.is('test', spy.getCall(0).firstArg.message); });