Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Client: Constructor supports createSocket option #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ var dgram = require('dgram'),
* @option cacheDns {boolean} An optional option to only lookup the hostname -> ip address once
* @option mock {boolean} An optional boolean indicating this Client is a mock object, no stats are sent.
* @option global_tags {Array=} Optional tags that will be added to every metric
* @option createSocket {Function} Optional function that will be used to initialize the client's socket
* @constructor
*/
var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, global_tags) {
var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, global_tags, createSocket) {
var options = host || {},
self = this;

Expand All @@ -27,15 +28,21 @@ var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, gl
globalize : globalize,
cacheDns : cacheDns,
mock : mock === true,
global_tags : global_tags
global_tags : global_tags,
createSocket: createSocket
};
}

var createSocket = dgram.createSocket.bind(null, 'udp4');
if (typeof options.createSocket === 'function') {
createSocket = options.createSocket;
}

this.host = options.host || 'localhost';
this.port = options.port || 8125;
this.prefix = options.prefix || '';
this.suffix = options.suffix || '';
this.socket = dgram.createSocket('udp4');
this.socket = createSocket();
this.mock = options.mock;
this.global_tags = options.global_tags || [];

Expand Down
9 changes: 9 additions & 0 deletions test/test_statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ describe('StatsD', function(){
assert.ok(statsd.socket instanceof dgram.Socket);
});

it('should create a socket when custom createSocket function is provided in options', function(){
var statsd = new StatsD({
createSocket: function() {
return 'customSocket';
}
});
assert.equal('customSocket', statsd.socket);
});

});

describe('#global_tags', function(){
Expand Down