From 4c757363f7bdd822e31d571be6f6fb92d5ea343f Mon Sep 17 00:00:00 2001 From: Brian Deitte Date: Sun, 17 Jan 2016 13:50:00 -0500 Subject: [PATCH 1/2] Fix an increment and close bug --- CHANGES.md | 4 ++++ README.md | 11 +++++++---- lib/statsd.js | 11 ++++++++++- test/test_statsd.js | 13 +++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 693a12d..211c088 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,10 @@ none -------------------- +## 2.3.0 (2015-1-17) +* @bdeitte Fix increment(name, 0) to send a 0 count instead of 1 +* @bdeitte Flush the queue when needed on close() + ## 2.2.0 (2015-1-10) * @bdeitte Document and expand on close API * @bdeitte Catch more error cases for callbacks diff --git a/README.md b/README.md index 66179c2..7f7208a 100644 --- a/README.md +++ b/README.md @@ -149,10 +149,13 @@ client.socket.on('error', function(error) { Thanks for considering making any updates to this project! Here are the steps to take in your fork: -1. Make sure you've added any new tests needed -2. Run "npm install && npm test" -3. Update the HEAD section in CHANGES.md with a description of what you have done -4. Push your changes and create the PR, and we'll try to get this merged in right away +1. Run "npm install" +2. Add your changes in your fork as well as any new tests needed +3. Run "npm test" +4. Update the HEAD section in CHANGES.md with a description of what you have done +5. Push your changes and create the PR + +When you've done all this we're happy to try to get this merged in right away. ## Name diff --git a/lib/statsd.js b/lib/statsd.js index be3ccfa..a3fe8ef 100644 --- a/lib/statsd.js +++ b/lib/statsd.js @@ -88,7 +88,12 @@ Client.prototype.timing = function (stat, time, sampleRate, tags, callback) { * @param callback {Function=} Callback when message is done being delivered. Optional. */ Client.prototype.increment = function (stat, value, sampleRate, tags, callback) { - this.sendAll(stat, value || 1, 'c', sampleRate, tags, callback); + // we explitcly check for undefined and null (and don't do a "! value" check) + // so that 0 values are allowed and sent through as-is + if (value === undefined || value === null) { + value = 1; + } + this.sendAll(stat, value, 'c', sampleRate, tags, callback); }; /** @@ -376,6 +381,10 @@ Client.prototype.close = function(callback){ clearInterval(this.intervalHandle); } + if(this.buffer.length >= 0) { + this.flushQueue(); + } + if (callback) { // use the close event rather than adding a callback to close() // because that API is not available in older Node versions diff --git a/test/test_statsd.js b/test/test_statsd.js index 0fdb454..9973c5b 100644 --- a/test/test_statsd.js +++ b/test/test_statsd.js @@ -504,6 +504,19 @@ describe('StatsD', function(){ }); }); + it('should use when increment is 0', function(finished){ + udpTest(function(message, server){ + assert.equal(message, 'test:0|c'); + server.close(); + finished(); + }, function(server){ + var address = server.address(), + statsd = new StatsD(address.address, address.port); + + statsd.increment('test', 0); + }); + }); + it('should send proper count format with tags', function(finished){ udpTest(function(message, server){ assert.equal(message, 'test:42|c|#foo,bar'); From 133089388be0fa7ec8b0342e6d75e9393f4ccfa4 Mon Sep 17 00:00:00 2001 From: Brian Deitte Date: Sun, 17 Jan 2016 13:52:01 -0500 Subject: [PATCH 2/2] Fix spelling --- lib/statsd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/statsd.js b/lib/statsd.js index a3fe8ef..5ecdbce 100644 --- a/lib/statsd.js +++ b/lib/statsd.js @@ -88,7 +88,7 @@ Client.prototype.timing = function (stat, time, sampleRate, tags, callback) { * @param callback {Function=} Callback when message is done being delivered. Optional. */ Client.prototype.increment = function (stat, value, sampleRate, tags, callback) { - // we explitcly check for undefined and null (and don't do a "! value" check) + // we explicitly check for undefined and null (and don't do a "! value" check) // so that 0 values are allowed and sent through as-is if (value === undefined || value === null) { value = 1;