-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
166 lines (158 loc) · 3.49 KB
/
index.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
const os = require('os');
const URI = require('url');
const http = require('http');
function Falcon(options) {
if (!(this instanceof Falcon)) {
return new Falcon(options);
}
if (typeof options == 'string') {
options = {
api: options
};
}
var self = this,
defaults = {
tags: '',
step: 60,
value: 0,
counterType: 'GAUGE',
endpoint: os.hostname(),
api: 'http://127.0.0.1:1988/v1/push',
timestamp: function () {
return Math.floor(+new Date() / 1000);
},
};
//
for (var key in options) {
defaults[key] = options[key];
}
//
this.data = {};
this.queue = [];
this.options = defaults;
return this;
}
Falcon.COUNTER_TYPE = {
GAUGE: 'GAUGE',
COUNTER: 'COUNTER'
};
Falcon.prototype.use = function (middleware) {
middleware.apply(this, [this]);
return this;
};
Falcon.prototype.set = function (key, value) {
this.data[key] = value;
return this;
};
Falcon.prototype.metric = function (name, value) {
if (name) {
this.set('metric', name);
}
if (value) {
this.set('value', value);
}
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.endpoint = function (value) {
this.set.apply(this, ['endpoint', value]);
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.timestamp = function (value) {
this.set.apply(this, ['timestamp', value]);
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.value = function (value) {
this.set.apply(this, ['value', value]);
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.step = function (value) {
this.set.apply(this, ['step', value]);
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.type = function (value) {
this.set.apply(this, ['counterType', value.toUpperCase()]);
return this;
};
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.tags = function (tags) {
var arr = [];
Object.keys(tags || {}).map(function (key) {
arr.push([key, tags[key]].join('='));
});
this.data['tags'] = arr.join(',');
return this;
};
Falcon.prototype.end = function () {
var self = this;
([
'step',
'tags',
'value',
'metric',
'endpoint',
'timestamp',
'counterType'
]).map(function (key) {
if (
typeof self.data[key] == 'undefined' &&
typeof self.options[key] == 'undefined'
) throw new Error(`'${key}' is required .`);
//
if (typeof self.data[key] == 'undefined') {
if (typeof self.options[key] == 'function') {
self.data[key] = self.options[key](self);
} else if (typeof self.options[key] != 'undefined') {
self.data[key] = self.options[key];
}
}
});
this.queue.push(this.data);
this.data = {};
return this;
}
/**
* [function description]
* @return {[type]} [description]
*/
Falcon.prototype.send = function (callback = () => {}) {
const { api } = this.options;
const req = http.request(Object.assign(URI.parse(api), {
method: 'POST'
}), res => {
const buffer = [];
res
.on('error', callback)
.on('data', chunk => buffer.push(chunk))
.on('end', () => {
callback && callback(null, Buffer.concat(buffer));
this.queue = [];
});
})
req.on('error', callback);
req.end(JSON.stringify(this.queue));
return this;
};
module.exports = Falcon;