Skip to content

Commit

Permalink
Merge pull request #13 from brightcove/fix_increment_close
Browse files Browse the repository at this point in the history
Fix an increment and close bug
  • Loading branch information
Brian Deitte committed Jan 17, 2016
2 parents 95c2210 + 1330893 commit 977f8c7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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;
}
this.sendAll(stat, value, 'c', sampleRate, tags, callback);
};

/**
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/test_statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 977f8c7

Please sign in to comment.