This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
statsd.js
237 lines (212 loc) · 7.85 KB
/
statsd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var dgram = require('dgram'),
dns = require('dns');
/**
* The UDP Client for StatsD
* @param options
* @option host {String} The host to connect to default: localhost
* @option port {String|Integer} The port to connect to default: 8125
* @option prefix {String} An optional prefix to assign to each stat name sent
* @option suffix {String} An optional suffix to assign to each stat name sent
* @option globalize {boolean} An optional boolean to add "statsd" as an object in the global namespace
* @option cacheDns {boolean} An optional option to only lookup the hostname -> ip address once
* @option mock {boolean} An optional boolean indicating this Client is a mock object, no stats are sent.
* @option global_tags {Array=} Optional tags that will be added to every metric
* @constructor
*/
var Client = 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;
}
};
/**
* Represents the timing stat
* @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.
* @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.increment = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, value || 1, 'c', sampleRate, tags, callback);
};
/**
* Decrements 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.
* @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.decrement = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, -value || -1, 'c', sampleRate, tags, callback);
};
/**
* Represents the histogram stat
* @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.
* @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.histogram = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, value, 'h', sampleRate, tags, callback);
};
/**
* Gauges 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.
* @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.gauge = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, value, 'g', sampleRate, tags, callback);
};
/**
* Counts unique values 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.
* @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.unique =
Client.prototype.set = function (stat, value, sampleRate, tags, callback) {
this.sendAll(stat, value, 's', sampleRate, tags, callback);
};
/**
* Checks if stats is an array and sends all stats calling back once all have sent
* @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.
* @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.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);
}
};
/**
* Sends a stat across the wire
* @param stat {String|Array} The stat(s) to send
* @param value The value to send
* @param type {String} The type of message to send to statsd
* @param sampleRate {Number} The Number of times to sample (0 to 1)
* @param tags {Array} The Array of tags to add to metrics
* @param callback {Function=} Callback when message is done being delivered. Optional.
*/
Client.prototype.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);
}
}
};
/**
* 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;