-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphite.js
55 lines (40 loc) · 1.07 KB
/
graphite.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
"use strict"
var dgram = require('dgram');
var Graphite = function(apiKey) {
this.apiKey = apiKey;
this.port = 2003;
this.host = 'carbon.hostedgraphite.com';
this.client = dgram.createSocket('udp4');
};
Graphite.prototype.sendMetric = function(name, count, timestamp) {
var metrics = {
name: name,
count: count,
timestamp: timestamp,
};
this.sendMetrics(metrics);
};
Graphite.prototype.sendMetrics = function(metrics) {
if (!Array.isArray(metrics)) {
var metricsArray = [];
var metricNames = Object.keys(metrics);
metricNames.forEach(function(key) {
metricsArray.push({
name: key,
count: metrics[key],
});
});
metrics = metricsArray;
}
var message = '',
apiKey = this.apiKey;
metrics.forEach(function(metricObject) {
var key = metricObject.name,
value = metricObject.count,
timestamp = metricObject.timestamp || '';
message += apiKey + '.' + key + ' ' + value + ' ' + timestamp + '\n';
});
message = new Buffer(message);
this.client.send(message, 0, message.length, this.port, this.host);
};
module.exports = Graphite;