Skip to content

Commit

Permalink
feat(dats): converted afterSend into onError function
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso authored and simonecorsi committed Oct 28, 2021
1 parent 5f2581c commit d010e70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ It automatically collects Node.js process metrics along with routes hit count, t

<!-- toc -->

- [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)

<!-- tocstop -->

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = () => {};
}
Expand Down Expand Up @@ -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
) {
Expand All @@ -108,7 +110,7 @@ module.exports = fp(
bufferFlushTimeout,
udpDnsCache,
udpDnsCacheTTL,
afterSend: afterSend,
onError: onError,
})
: clientMock();

Expand Down
2 changes: 1 addition & 1 deletion tests/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit d010e70

Please sign in to comment.