-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdatadog.js
242 lines (205 loc) · 6.2 KB
/
datadog.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
238
239
240
241
242
/*jshint node:true, laxcomma:true */
/*
* Flush stats to datadog (http://datadoghq.com/).
*
* To enable this backend, include 'statsd-datadog-backend' in the backends
* configuration array:
*
* backends: ['statsd-datadog-backend']
*
* This backend supports the following config options:
*
* datadogApiKey: Your DataDog API key
* datadogPrefix: A global prefix for all metrics
* datadogTags: A global set of tags for all metrics
*/
var net = require('net'),
os = require('os'),
request = require('request');
var logger;
var debug;
var flushInterval;
var hostname;
var datadogApiHost;
var datadogApiKey;
var datadogStats = {};
var datadogPrefix;
var datadogRemovePrefix;
var datadogTags;
var Datadog = function(api_key, options) {
options = options || {};
this.api_key = api_key;
this.api_host = options.api_host || 'https://app.datadoghq.com';
this.host_name = options.host_name || os.hostname();
this.pending_requests = 0;
};
Datadog.prototype.metrics = function(payload) {
var client = this;
var message = {
series: payload
};
client._post('series', message);
};
Datadog.prototype._post = function(controller, message) {
var client = this;
var body = JSON.stringify(message);
if (this.api_host.indexOf('https') == -1) {
logger.log('Warning! You are about to send unencrypted metrics.');
}
client.pending_requests += 1;
request.post({
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
},
url: this.api_host + '/api/v1/' + controller + '?api_key=' + client.api_key,
body: body
}, function(error) {
if (error) {
logger.log('Skipping, cannot send data to Datadog: ' + error.message);
}
client.pending_requests -= 1;
});
};
var post_stats = function datadog_post_stats(payload) {
try {
new Datadog(datadogApiKey, { api_host: datadogApiHost }).metrics(payload);
datadogStats.last_flush = Math.round(new Date().getTime() / 1000);
} catch(e){
if (debug) {
logger.log(e);
}
datadogStats.last_exception = Math.round(new Date().getTime() / 1000);
}
};
var flush_stats = function datadog_post_stats(ts, metrics) {
var counters = metrics.counters;
var gauges = metrics.gauges;
var timers = metrics.timers;
var pctThreshold = metrics.pctThreshold;
var host = hostname || os.hostname();
var payload = [];
var value;
var key;
// Send counters
for (key in counters) {
value = counters[key];
var valuePerSecond = value / (flushInterval / 1000); // calculate 'per second' rate
payload.push({
metric: get_prefix(key),
points: [[ts, valuePerSecond]],
type: 'gauge',
host: host,
tags: datadogTags
});
}
// Send gauges
for (key in gauges) {
value = gauges[key];
payload.push({
metric: get_prefix(key),
points: [[ts, value]],
type: 'gauge',
host: host,
tags: datadogTags
});
}
// Compute timers and send
for (key in timers) {
if (timers[key].length > 0) {
var values = timers[key].sort(function (a,b) { return a-b; });
var count = values.length;
var min = values[0];
var max = values[count - 1];
var mean = min;
var maxAtThreshold = max;
var i;
if (count > 1) {
var thresholdIndex = Math.round(((100 - pctThreshold) / 100) * count);
var numInThreshold = count - thresholdIndex;
var pctValues = values.slice(0, numInThreshold);
maxAtThreshold = pctValues[numInThreshold - 1];
// average the remaining timings
var sum = 0;
for (i = 0; i < numInThreshold; i++) {
sum += pctValues[i];
}
mean = sum / numInThreshold;
}
payload.push({
metric: get_prefix(key + '.mean'),
points: [[ts, mean]],
type: 'gauge',
host: host,
tags: datadogTags
});
payload.push({
metric: get_prefix(key + '.upper'),
points: [[ts, max]],
type: 'gauge',
host: host,
tags: datadogTags
});
payload.push({
metric: get_prefix(key + '.upper_' + pctThreshold),
points: [[ts, maxAtThreshold]],
type: 'gauge',
host: host,
tags: datadogTags
});
payload.push({
metric: get_prefix(key + '.lower'),
points: [[ts, min]],
type: 'gauge',
host: host,
tags: datadogTags
});
payload.push({
metric: get_prefix(key + '.count'),
points: [[ts, count]],
type: 'gauge',
host: host,
tags: datadogTags
});
}
}
post_stats(payload);
};
var get_prefix = function datadog_get_prefix(key) {
var new_key = key;
if (datadogRemovePrefix !== undefined) {
new_key = key.split(".").slice(datadogRemovePrefix).join('.');
}
if (datadogPrefix !== undefined) {
new_key = [datadogPrefix, new_key].join('.');
}
return new_key;
}
var backend_status = function datadog_status(writeCb) {
var stat;
for (stat in datadogStats) {
writeCb(null, 'datadog', stat, datadogStats[stat]);
}
};
exports.init = function datadog_init(startup_time, config, events, log) {
logger = log;
debug = config.debug;
hostname = config.hostname;
datadogApiKey = config.datadogApiKey;
datadogApiHost = config.datadogApiHost;
datadogPrefix = config.datadogPrefix;
datadogRemovePrefix = config.datadogRemovePrefix;
datadogTags = config.datadogTags;
if (datadogTags === undefined || datadogTags.constructor !== Array || datadogTags.length < 1) {
datadogTags = [];
}
if (!datadogApiHost) {
datadogApiHost = 'https://app.datadoghq.com';
}
datadogStats.last_flush = startup_time;
datadogStats.last_exception = startup_time;
flushInterval = config.flushInterval;
events.on('flush', flush_stats);
events.on('status', backend_status);
return true;
};