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

Support incrementing or decrementing Counters by 0 #65

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
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