node-statsd = function (host, port, prefix, suffix, globalize, cacheDns, mock, global_tags) { var options = host || {}, self = this; if(arguments.length > 1 || typeof(host) === 'string'){ options = { host : host, port : port, prefix : prefix, suffix : suffix, globalize : globalize, cacheDns : cacheDns, mock : mock === true, global_tags : global_tags }; } this.host = options.host || 'localhost'; this.port = options.port || 8125; this.prefix = options.prefix || ''; this.suffix = options.suffix || ''; this.socket = dgram.createSocket('udp4'); this.mock = options.mock; this.global_tags = options.global_tags || []; if(options.cacheDns === true){ dns.lookup(options.host, function(err, address, family){ if(err == null){ self.host = address; } }); } if(options.globalize){ global.statsd = this; } }
n/a
StatsD = function (host, port, prefix, suffix, globalize, cacheDns, mock, global_tags) { var options = host || {}, self = this; if(arguments.length > 1 || typeof(host) === 'string'){ options = { host : host, port : port, prefix : prefix, suffix : suffix, globalize : globalize, cacheDns : cacheDns, mock : mock === true, global_tags : global_tags }; } this.host = options.host || 'localhost'; this.port = options.port || 8125; this.prefix = options.prefix || ''; this.suffix = options.suffix || ''; this.socket = dgram.createSocket('udp4'); this.mock = options.mock; this.global_tags = options.global_tags || []; if(options.cacheDns === true){ dns.lookup(options.host, function(err, address, family){ if(err == null){ self.host = address; } }); } if(options.globalize){ global.statsd = this; } }
n/a
StatsD = function (host, port, prefix, suffix, globalize, cacheDns, mock, global_tags) { var options = host || {}, self = this; if(arguments.length > 1 || typeof(host) === 'string'){ options = { host : host, port : port, prefix : prefix, suffix : suffix, globalize : globalize, cacheDns : cacheDns, mock : mock === true, global_tags : global_tags }; } this.host = options.host || 'localhost'; this.port = options.port || 8125; this.prefix = options.prefix || ''; this.suffix = options.suffix || ''; this.socket = dgram.createSocket('udp4'); this.mock = options.mock; this.global_tags = options.global_tags || []; if(options.cacheDns === true){ dns.lookup(options.host, function(err, address, family){ if(err == null){ self.host = address; } }); } if(options.globalize){ global.statsd = this; } }
n/a
close = function (){ this.socket.close(); }
...
}
};
/**
* Close the underlying socket and stop listening for data on it.
*/
Client.prototype.close = function(){
this.socket.close();
}
exports = module.exports = Client;
exports.StatsD = Client;
...
decrement = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, -value || -1, 'c', sampleRate, tags, callback); }
...
// Timing: sends a timing command with the specified milliseconds
client.timing('response_time', 42);
// Increment: Increments a stat by a value (default is 1)
client.increment('my_counter');
// Decrement: Decrements a stat by a value (default is -1)
client.decrement('my_counter');
// Histogram: send data for histogram stat
client.histogram('my_histogram', 42);
// Gauge: Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
...
gauge = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, value, 'g', sampleRate, tags, callback); }
...
// Decrement: Decrements a stat by a value (default is -1)
client.decrement('my_counter');
// Histogram: send data for histogram stat
client.histogram('my_histogram', 42);
// Gauge: Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
// Set: Counts unique occurrences of a stat (alias of unique)
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
// Incrementing multiple items
client.increment(['these', 'are', 'different', 'stats']);
...
histogram = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, value, 'h', sampleRate, tags, callback); }
...
// Increment: Increments a stat by a value (default is 1)
client.increment('my_counter');
// Decrement: Decrements a stat by a value (default is -1)
client.decrement('my_counter');
// Histogram: send data for histogram stat
client.histogram('my_histogram', 42);
// Gauge: Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
// Set: Counts unique occurrences of a stat (alias of unique)
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
...
increment = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, value || 1, 'c', sampleRate, tags, callback); }
...
var StatsD = require('node-statsd'),
client = new StatsD();
// Timing: sends a timing command with the specified milliseconds
client.timing('response_time', 42);
// Increment: Increments a stat by a value (default is 1)
client.increment('my_counter');
// Decrement: Decrements a stat by a value (default is -1)
client.decrement('my_counter');
// Histogram: send data for histogram stat
client.histogram('my_histogram', 42);
...
send = function (stat, value, type, sampleRate, tags, callback) { var message = this.prefix + stat + this.suffix + ':' + value + '|' + type, buf, merged_tags = []; if(sampleRate && sampleRate < 1){ if(Math.random() < sampleRate){ message += '|@' + sampleRate; } else { //don't want to send if we don't meet the sample ratio return; } } if(tags && Array.isArray(tags)){ merged_tags = merged_tags.concat(tags); } if(this.global_tags && Array.isArray(this.global_tags)){ merged_tags = merged_tags.concat(this.global_tags); } if(merged_tags.length > 0){ message += '|#' + merged_tags.join(','); } // Only send this stat if we're not a mock Client. if(!this.mock) { buf = new Buffer(message); this.socket.send(buf, 0, buf.length, this.port, this.host, callback); } else { if(typeof callback === 'function'){ callback(null, 0); } } }
...
if(completed === stat.length){
callback(null, sentBytes);
}
}
if(Array.isArray(stat)){
stat.forEach(function(item){
self.send(item, value, type, sampleRate, tags, onSend);
});
} else {
this.send(stat, value, type, sampleRate, tags, callback);
}
};
/**
...
sendAll = function (stat, value, type, sampleRate, tags, callback){
var completed = 0,
calledback = false,
sentBytes = 0,
self = this;
if(sampleRate && typeof sampleRate !== 'number'){
callback = tags;
tags = sampleRate;
sampleRate = undefined;
}
if(tags && !Array.isArray(tags)){
callback = tags;
tags = undefined;
}
/**
* Gets called once for each callback, when all callbacks return we will
* call back from the function
* @private
*/
function onSend(error, bytes){
completed += 1;
if(calledback || typeof callback !== 'function'){
return;
}
if(error){
calledback = true;
return callback(error);
}
sentBytes += bytes;
if(completed === stat.length){
callback(null, sentBytes);
}
}
if(Array.isArray(stat)){
stat.forEach(function(item){
self.send(item, value, type, sampleRate, tags, onSend);
});
} else {
this.send(stat, value, type, sampleRate, tags, callback);
}
}
...
* @param stat {String|Array} The stat(s) to send
* @param time {Number} The time in milliseconds to send
* @param sampleRate {Number=} The Number of times to sample (0 to 1). Optional.
* @param tags {Array=} The Array of tags to add to metrics. Optional.
* @param callback {Function=} Callback when message is done being delivered. Optional.
*/
Client.prototype.timing = function (stat, time, sampleRate, tags, callback) {
this.sendAll(stat, time, 'ms', sampleRate, tags, callback);
};
/**
* Increments a stat by a specified amount
* @param stat {String|Array} The stat(s) to send
* @param value The value to send
* @param sampleRate {Number=} The Number of times to sample (0 to 1). Optional.
...
set = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, value, 's', sampleRate, tags, callback); }
...
// Histogram: send data for histogram stat
client.histogram('my_histogram', 42);
// Gauge: Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
// Set: Counts unique occurrences of a stat (alias of unique)
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
// Incrementing multiple items
client.increment(['these', 'are', 'different', 'stats']);
// Sampling, this will sample 25% of the time the StatsD Daemon will compensate for sampling
client.increment('my_counter', 1, 0.25);
...
timing = function (stat, time, sampleRate, tags, callback) { this.sendAll(stat, time, 'ms', sampleRate, tags, callback); }
...
If an array is specified as the `name` parameter each item in that array will be sent along with the specified value.
```javascript
var StatsD = require('node-statsd'),
client = new StatsD();
// Timing: sends a timing command with the specified milliseconds
client.timing('response_time', 42);
// Increment: Increments a stat by a value (default is 1)
client.increment('my_counter');
// Decrement: Decrements a stat by a value (default is -1)
client.decrement('my_counter');
...
unique = function (stat, value, sampleRate, tags, callback) { this.sendAll(stat, value, 's', sampleRate, tags, callback); }
...
client.histogram('my_histogram', 42);
// Gauge: Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
// Set: Counts unique occurrences of a stat (alias of unique)
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
// Incrementing multiple items
client.increment(['these', 'are', 'different', 'stats']);
// Sampling, this will sample 25% of the time the StatsD Daemon will compensate for sampling
client.increment('my_counter', 1, 0.25);
...