Skip to content

Commit

Permalink
Add udpSocketOptions as a StatsD option (#231)
Browse files Browse the repository at this point in the history
This options allows the library user to control how the UDP socket is
created. If not specified, we maintain existing compatibility by
defaulting to a udp4 socket family with no other options.

Fixes #223
  • Loading branch information
hjr3 authored Jul 30, 2022
1 parent 7cc8a50 commit a399dda
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Parameters (specified as one object passed into hot-shots):
* `udsGracefulErrorHandling`: Used only when the protocol is `uds`. Boolean indicating whether to handle socket errors gracefully. Defaults to true.
* `udsGracefulRestartRateLimit`: Used only when the protocol is `uds`. Time (ms) between re-creating the socket. Defaults to `1000`.
* `closingFlushInterval`: Before closing, StatsD will check for inflight messages. Time (ms) between each check. Defaults to `50`.
* `udpSocketOptions`: Specify these options to control how StatsD will create a UDP (dgram) socket.

### StatsD methods
All StatsD methods other than `event`, `close`, and `check` have the same API:
Expand Down
2 changes: 2 additions & 0 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const Client = function (host, port, prefix, suffix, globalize, cacheDns, mock,
this.udsGracefulRestartRateLimit = options.udsGracefulRestartRateLimit || UDS_DEFAULT_GRACEFUL_RESTART_LIMIT; // only recreate once per second
this.isChild = options.isChild;
this.closingFlushInterval = options.closingFlushInterval || 50;
this.udpSocketOptions = options.udpSocketOptions || { type: 'udp4' };

// If we're mocking the client, create a buffer to record the outgoing calls.
if (this.mock) {
Expand Down Expand Up @@ -623,5 +624,6 @@ function trySetNewSocket(client) {
port: client.port,
protocol: client.protocol,
stream: client.stream,
udpSocketOptions: client.udpSocketOptions,
});
}
2 changes: 1 addition & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const createTcpTransport = args => {
};

const createUdpTransport = args => {
const socket = dgram.createSocket('udp4');
const socket = dgram.createSocket(args.udpSocketOptions);
// do not block node from shutting down
socket.unref();

Expand Down
49 changes: 49 additions & 0 deletions test/udpSocketOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const assert = require('assert');
const helpers = require('./helpers/helpers.js');
const dns = require('dns');
const dgram = require('dgram');

const closeAll = helpers.closeAll;
const createServer = helpers.createServer;
const createHotShotsClient = helpers.createHotShotsClient;

describe('#udpSocketOptions', () => {
const udpServerType = 'udp';
const originalDnsLookup = dns.lookup;
const originalDgramCreateSocket = dgram.createSocket;
let server;
let statsd;

afterEach(done => {
dns.lookup = originalDnsLookup;
dgram.createSocket = originalDgramCreateSocket;
closeAll(server, statsd, false, done);
});

it('should use custom DNS lookup function', done => {
const resolvedHostAddress = '127.0.0.1';
let dnsLookupCount = 0;
const customDnsLookup = (host, options, callback) => {
dnsLookupCount++;
callback(undefined, resolvedHostAddress);
};

server = createServer(udpServerType, opts => {
statsd = createHotShotsClient(Object.assign(opts, {
cacheDns: true,
udpSocketOptions: {
type: 'udp4',
lookup: customDnsLookup,
},
}), 'client');

statsd.send('test title', {}, (error) => {
assert.strictEqual(error, null);
setTimeout(() => {
assert.strictEqual(dnsLookupCount, 2);
done();
}, 1000);
});
});
});
});
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare module "hot-shots" {
udsGracefulErrorHandling?: boolean;
udsGracefulRestartRateLimit?: number;
closingFlushInterval?: number;
udpSocketOptions?: dgram.SocketOptions;
}

export interface ChildClientOptions {
Expand Down

0 comments on commit a399dda

Please sign in to comment.