-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
214 lines (200 loc) · 5.99 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
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
const sample = require('./lib/sample');
const betterror = require('./lib/betterror');
const flush = require('./lib/flush');
const formatter = require('./lib/formatter');
const push = require('./lib/push');
const sender = require('./lib/sender');
const spread = require('./lib/spread');
const types = require('./lib/types');
const TYPES_LIST = Object.keys(types);
const TYPES = Object.freeze(
TYPES_LIST.reduce(
(accumulator, type) => Object.assign(
accumulator,
{ [type]: type }
),
{}
)
);
/**
* SDC
* @property {Number} MTU
* @property {Number} timeout
* @property {Object} tags
* @property {Array} bulk
* @property {Number} timer
* @property {Function} send
* @property {Function} format
* @property {Function(bound)} flush
*
* @property {Number} size
* @property {Function} generic
* @property {Function} count
* @property {Function} time
* @property {Function} gauge
* @property {Function} set
* @property {Function} histogram
*/
class SDC {
/**
* @static
* @getter
* @type {Object}
* @property {String} count 'count'
* @property {String} time 'time'
* @property {String} gauge 'gauge'
* @property {String} set 'set'
* @property {String} histogram 'histogram'
*/
static get TYPES() {
return TYPES;
}
/**
* SDC constructor
* @param {String} [options.host='127.0.0.1']
* @param {String} [options.port=8125]
* @param {String} [options.protocol='UDP']
* @param {String} [options.protocol_version='ipv4']
* @param {Number} [options.MTU=576]
* @param {Number} [options.timeout=1000]
* @param {Object} [options.tags]
* @param {String} [options.scheme='datadog']
* @param {String} [options.prefix]
* @param {Function} [options.sanitise=(default sanitiser)]
* @param {Function} [options.errorHandler]
* @param {Boolean} [options.enforceRate=true]
*/
constructor(
{
host = '127.0.0.1',
port = 8125,
protocol = 'UDP',
protocol_version = 'ipv4',
MTU = 576,
timeout = 1000,
tags,
scheme,
prefix,
sanitise,
errorHandler,
enforceRate = true
} = {}
) {
Object.assign(
this,
{
MTU, // Maximum Transmission Unit
timeout,
tags,
errorHandler,
enforceRate,
bulk: [],
timer: null,
send: sender({
host,
port,
protocol,
protocol_version,
errorHandler
}),
format: formatter({
sanitise,
prefix,
scheme
}),
flush: flush.bind(this)
}
);
[...TYPES_LIST, 'generic'].forEach((fn) => {
this[fn] = this[fn].bind(this);
});
process.on('exit', this.flush);
}
/**
* The size of current bulk
* @return {Number}
*/
get size() {
return this.bulk.join('\n').length;
}
/**
* Generic metric send method
* @param {String} type Metric type
* @param {String} key The metric name (key)
* @param {Number} [value=1] The value to report
* @param {Number} [options.rate] Sample rate - a fraction of 1 (.01 is one percent)
* @param {Object} [options.tags] Key-value pairs of tags set as object literal
* @return {Number} current size of the bulk
*/
generic(...args) {
let [
type, // eslint-disable-line prefer-const
key, // eslint-disable-line prefer-const
value = 1, // eslint-disable-line prefer-const
rate, // eslint-disable-line prefer-const
tags,
enforceRate = true // eslint-disable-line prefer-const
] = spread(args);
if (rate) {
if (typeof rate !== 'number') {
throw betterror(
new TypeError(`Expected 'rate' to be a number, instead got ${rate} (${typeof rate})`),
{ type, key, value, rate, tags }
);
}
if (rate > 1) {
throw betterror(
new TypeError(`Expected 'rate' to be a number between 0 and 1, instead got ${rate}`),
{ type, key, value, rate, tags }
);
}
if (enforceRate && this.enforceRate && !sample(rate)) {
return this.size;
}
}
if (this.tags) {
tags = Object.assign({}, this.tags, tags || {});
}
return push.call(
this,
this.format(
type,
key,
value,
{ rate, tags }
)
);
}
destroy() {
process.off('exit', this.flush);
this.send.destroy();
}
}
Object.defineProperties(
SDC.prototype,
TYPES_LIST.reduce(
(accumulator, type) => Object.assign(
accumulator,
{
[type]: {
/**
* Specific metric type send method
* @param {String} key The metric name (key)
* @param {Number} [value] The value to report
* @param {Number} [options.rate] Sample rate - a fraction of 1 (.01 is one percent)
* @param {Object} [options.tags] Key-value pairs of tags set as object literal
* @return {Number} current size of the bulk
*/
value: function(...args) {
return this.generic(type, ...args);
},
configurable: true,
enumerable: true,
writable: true
}
}
),
{}
)
);
module.exports = SDC;