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

Commit

Permalink
Support incrementing or decrementing by 0
Browse files Browse the repository at this point in the history
  • Loading branch information
todd committed Dec 13, 2016
1 parent f9fac42 commit b47c00c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ 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);
value = (value === undefined || value === null) ? 1 : value;

this.sendAll(stat, value, 'c', sampleRate, tags, callback);
};

/**
Expand All @@ -85,7 +87,9 @@ Client.prototype.increment = function (stat, value, sampleRate, tags, callback)
* @param callback {Function=} Callback when message is done being delivered. Optional.
*/
Client.prototype.decrement = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, -value || -1, 'c', sampleRate, tags, callback);
value = (value === undefined || value === null) ? 1 : value;

this.sendAll(stat, -value, 'c', sampleRate, tags, callback);
};

/**
Expand Down
26 changes: 26 additions & 0 deletions test/test_statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,19 @@ describe('StatsD', function(){
it('should send no increment stat when a mock Client is used', function(finished){
assertMockClientMethod('increment', finished);
});

it('should send 0 when value 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);
});
});
});

describe('#decrement', function(finished){
Expand Down Expand Up @@ -603,6 +616,19 @@ describe('StatsD', function(){
it('should send no decrement stat when a mock Client is used', function(finished){
assertMockClientMethod('decrement', finished);
});

it('should send 0 when value 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.decrement('test', 0);
});
});
});

describe('#set', function(finished){
Expand Down

0 comments on commit b47c00c

Please sign in to comment.