-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
439 lines (369 loc) · 13.9 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"use strict";
var AWS = require('aws-sdk'),
EventEmitter = require('events'),
moment = require('moment'),
os = require('os'),
util = require('util'),
uuid = require('uuid');
var DEBUG = false,
TRACE = false;
/* ************************************************** *
* ******************** Constants
* ************************************************** */
const DYNAMO_TYPE_ARRAY_BINARY = "BS",
DYNAMO_TYPE_ARRAY_MAP = "L",
DYNAMO_TYPE_ARRAY_NUMBER = "NS",
DYNAMO_TYPE_ARRAY_STRING = "SS",
DYNAMO_TYPE_BINARY = "B",
DYNAMO_TYPE_BOOLEAN = "BOOL",
DYNAMO_TYPE_MAP = "M",
DYNAMO_TYPE_NULL = "NULL",
DYNAMO_TYPE_NUMBER = "N",
DYNAMO_TYPE_STRING = "S";
const DEFAULT_AWS_API_VERSION = "2012-08-10",
DEFAULT_AWS_MAX_RETRIES = 15,
DEFAULT_AWS_REGION = "us-east-1",
DEFAULT_HOSTNAME_ENABLE = true,
DEFAULT_TABLE_HASH_KEY = "id",
DEFAULT_TABLE_HASH_TYPE = DYNAMO_TYPE_STRING,
DEFAULT_TABLE_RANGE_KEY = "time",
DEFAULT_TABLE_RANGE_TYPE = DYNAMO_TYPE_NUMBER,
DEFAULT_TABLE_READ_CAPACITY = 5,
DEFAULT_TABLE_WRITE_CAPACITY = 5,
DEFAULT_BATCH_SIZE = 25,
DEFAULT_SEND_INTERVAL = (DEBUG) ? 1000 : 5000; // 1 second if debug, otherwise 5 seconds.
/* ************************************************** *
* ******************** Global Private Methods
* ************************************************** */
var createDynamoValueObject = function (key, value) {
var obj = {};
obj[key] = value.toString(); // All types must be converted to a string.
return obj;
};
var buildTableName = function () {
var tableName = "";
if(process.env.APP_NAME) {
tableName += process.env.APP_NAME + "_";
}
if(os.hostname()) {
tableName += os.hostname();
} else {
tableName += uuid.v4();
}
if(process.env.PORT) {
tableName += "_" + process.env.PORT;
}
return tableName;
};
/* ************************************************** *
* ******************** Dynamo Stream Class
* ************************************************** */
class DynamoStream {
constructor(options) {
var self = this;
self.writeBuffer = [];
self.disableBatchSending = false;
self.configLock = false;
self.setConfig(options);
}
setDefaultConfig() {
DEBUG = TRACE = false;
this.config = {
"aws": {},
"enableHostname": DEFAULT_HOSTNAME_ENABLE,
"batchSize": DEFAULT_BATCH_SIZE,
"sendInterval": DEFAULT_SEND_INTERVAL,
"tableName": buildTableName(),
"tableHashKey": DEFAULT_TABLE_HASH_KEY,
"tableHashType": DEFAULT_TABLE_HASH_TYPE,
"tableRangeKey": DEFAULT_TABLE_RANGE_KEY,
"tableRangeType": DEFAULT_TABLE_RANGE_TYPE,
"tableReadCapacity": DEFAULT_TABLE_READ_CAPACITY,
"tableWriteCapacity": DEFAULT_TABLE_WRITE_CAPACITY
};
}
validateLockedOption(options, propertyName) {
if(options[propertyName] !== undefined && options[propertyName] !== this.config[propertyName]) {
console.log("Error: bunyan-dynamo does not support changing the %s property after write() has been called. Instead create a new bunyan-dynamo object.", propertyName);
delete options[propertyName];
}
};
setConfig(options) {
var self = this;
if ( ! options) {
options = {};
}
if(self.config === undefined) {
self.setDefaultConfig();
} else {
self.validateLockedOption(options, 'tableName');
self.validateLockedOption(options, 'tableHashKey');
self.validateLockedOption(options, 'tableHashType');
self.validateLockedOption(options, 'tableRangeKey');
self.validateLockedOption(options, 'tableRangeType');
self.validateLockedOption(options, 'tableReadCapacity');
self.validateLockedOption(options, 'tableWriteCapacity');
if(options["aws"] !== undefined) {
// TODO: Throw error in event instead.
console.log("Error: bunyan-dynamo does not support changing the aws settings after a write() has been called. Instead create a new bunyan-dynamo object.");
delete options.aws;
}
}
self.stopTimer();
self.config = {
"aws": options.aws || self.config.aws,
"enableHostname": (options.enableHostname === true || options.enableHostname === false) ? options.enableHostname : self.config.enableHostname,
"batchSize": options.batchSize || self.config.batchSize,
"sendInterval": options.sendInterval || self.config.sendInterval,
"tableName": options.tableName || self.config.tableName,
"tableHashKey": options.hashKey || self.config.tableHashKey,
"tableHashType": options.hashType || self.config.tableHashType,
"tableRangeKey": options.rangeKey || self.config.tableRangeKey,
"tableRangeType": options.rangeType || self.config.tableRangeType,
"tableReadCapacity": options.tableReadCapacity || self.config.tableReadCapacity,
"tableWriteCapacity": options.tableWriteCapacity || self.config.tableWriteCapacity
};
if(options["debug"] === true) {
DEBUG = true;
}
if(options["trace"] === true) {
DEBUG = TRACE = true;
}
if ((options.aws === undefined || options.aws["apiVersion"] === undefined) && self.config.aws.apiVersion === undefined) {
self.config.aws.apiVersion = DEFAULT_AWS_API_VERSION;
}
if ((options.aws === undefined || options.aws["maxRetries"] === undefined) && self.config.aws.maxRetries === undefined) {
self.config.aws.maxRetries = DEFAULT_AWS_MAX_RETRIES;
}
if ((options.aws === undefined || options.aws["region"] === undefined) && self.config.aws.region === undefined) {
self.config.aws.region = DEFAULT_AWS_REGION;
}
//if (self.config.aws.endpoint) {
// self.config.aws.endpoint = new AWS.Endpoint(self.config.aws.endpoint);
//}
self.db = new AWS.DynamoDB(self.config.aws);
self.startTimer();
}
startTimer() {
var self = this;
self.timer = setInterval(function sendPeriodically() {
if(TRACE) { console.log("sendPeriodically(%s): Send interval expired.", self.config.sendInterval); }
self.processWriteQueue(true, function (err, items) {
// TODO: Send error event instead.
if (err) {
console.log(err);
}
});
}, self.config.sendInterval);
}
stopTimer() {
if(this.timer) {
clearInterval(this.timer);
}
}
getConfig() {
return this.config;
}
write(record, encoding, cb = function (err) {if(err) {console.log(err);} }) {
var item = {},
self = this;
if ( ! record) {
return cb();
}
// If the record is a string, convert it to a JSON object.
if (typeof record === 'string') {
record = JSON.parse(record);
}
// The hash key/value pair must be defined and part of a unique pair.
if ( ! record[self.config.tableHashKey]) {
switch (self.config.tableHashType) {
case DYNAMO_TYPE_STRING:
record[self.config.tableHashKey] = uuid.v4();
break;
case DYNAMO_TYPE_NUMBER:
record[self.config.tableHashKey] = process.hrtime(1);
break;
default:
var error = new Error("DynamoStream: The hash field " + self.config.tableHashKey + " must be defined.");
error.status = 500;
return cb(error);
}
}
item[self.config.tableHashKey] = createDynamoValueObject(self.config.tableHashType, record[self.config.tableHashKey]);
item["time"] = createDynamoValueObject(DYNAMO_TYPE_NUMBER, Date.parse(record.time));
if (record.msg && ! item.msg) {
item.msg = createDynamoValueObject(DYNAMO_TYPE_STRING, record.msg);
}
if (record.level && ! item.level) {
item.level = createDynamoValueObject(DYNAMO_TYPE_NUMBER, record.level);
}
if (self.config.enableHostname && record.hostname && ! item.hostname) {
item.hostname = createDynamoValueObject(DYNAMO_TYPE_STRING, record.hostname);
}
if (record.pid && ! item.pid) {
item.pid = createDynamoValueObject(DYNAMO_TYPE_NUMBER, record.pid);
}
if (record.v && ! item.v) {
item.v = createDynamoValueObject(DYNAMO_TYPE_NUMBER, record.v);
}
this.writeBuffer.push({
PutRequest: {
Item: item
}
});
this.processWriteQueue(false, cb);
}
processWriteQueue(force, cb) {
var self = this;
if( ! self.writeBuffer.length) {
return cb();
}
if( ! force) {
if(self.disableBatchSending) {
if(DEBUG) { console.log("processWriteQueue(%s/%s): Waiting for Dynamo DB to create the table.", self.writeBuffer.length, self.config.batchSize); }
return cb();
} else if(self.writeBuffer.length < self.config.batchSize) {
if(DEBUG) { console.log("processWriteQueue(%s/%s): Waiting for send interval or buffer to fill.", self.writeBuffer.length, self.config.batchSize); }
return cb();
}
}
self.createTable(function (err) {
if (err) {
cb(err);
} else {
var requestItems = self.writeBuffer.slice(0, self.config.batchSize);
// build request
var batchRequest = {
RequestItems: {}
};
batchRequest.RequestItems[self.config.tableName] = requestItems;
// remove batched items from buffer
self.writeBuffer = self.writeBuffer.slice(self.config.batchSize);
self.db.batchWriteItem(batchRequest, function (err, response) {
if (err) {
if(TRACE) { console.log("processWriteQueue(%s/%s): batchWriteItem failed with an error.", self.writeBuffer.length, self.config.batchSize); }
// If an error occurred, the unprocessed items should be returned to the buffer.
self.writeBuffer = requestItems.concat(self.writeBuffer);
cb(err);
} else {
var numItemsSent = requestItems.length;
// If AWS didn't process all of the items, return the unprocessed items to the buffer.
if (response && response.UnprocessedItems && response.UnprocessedItems[self.config.tableName]) {
numItemsSent -= response.UnprocessedItems[self.config.tableName].length;
self.writeBuffer = response.UnprocessedItems[self.config.tableName].concat(self.writeBuffer);
}
if(DEBUG) { console.log("processWriteQueue(%s/%s): Sent %s item%s.", self.writeBuffer.length, self.config.batchSize, numItemsSent, (numItemsSent > 1) ? "s" : ""); }
cb()
}
});
}
});
}
createTable(cb) {
var self = this;
if ( ! self.isTableCreated) {
// Disable changes to the config options once a table needs to be selected or created.
self.configLock = true;
self.db.listTables(function (err, result) {
if (err) {
if(TRACE) { console.log("createTable(%s): listTables failed with an error.", self.config.tableName); }
cb(err);
} else if (result && result.TableNames && result.TableNames.indexOf(self.config.tableName) != -1) {
if(DEBUG) { console.log("createTable(%s): Table is already created.", self.config.tableName); }
self.isTableCreated = true;
cb();
} else {
self.db.createTable({
TableName: self.config.tableName,
AttributeDefinitions: [
{
AttributeName: self.config.tableHashKey,
AttributeType: self.config.tableHashType
},
{
AttributeName: self.config.tableRangeKey,
AttributeType: self.config.tableRangeType
}
],
KeySchema: [
{
AttributeName: self.config.tableHashKey,
KeyType: 'HASH'
},
{
AttributeName: self.config.tableRangeKey,
KeyType: 'RANGE'
}
],
ProvisionedThroughput: {
ReadCapacityUnits: self.config.tableReadCapacity,
WriteCapacityUnits: self.config.tableWriteCapacity
}
}, function (err, result) {
if (err) {
if(TRACE) { console.log("createTable(%s): createTable failed with an error.", self.config.tableName); }
cb(err);
} else {
if(DEBUG) { console.log("createTable(%s): DynamoDB table is being created...", self.config.tableName); }
// Disable sending via the batch trigger.
self.disableBatchSending = true;
// Disable sending via the sendInterval trigger.
self.stopTimer();
// Wait for the database table to be written.
self.db.waitFor('tableExists', { TableName: self.config.tableName }, function (err, data) {
if(err) {
if(TRACE) { console.log("createTable(%s): waitFor table to exist failed with an error.", self.config.tableName); }
cb(err);
} else {
// Enable batch sending.
self.disableBatchSending = false;
// Enable sendInterval sending.
self.startTimer();
// Mark the table as created.
self.isTableCreated = true;
cb();
}
});
}
});
}
});
} else {
cb();
}
}
static get DYNAMO_TYPE_ARRAY_BINARY() {
return DYNAMO_TYPE_ARRAY_BINARY;
}
static get DYNAMO_TYPE_ARRAY_MAP() {
return DYNAMO_TYPE_ARRAY_MAP;
}
static get DYNAMO_TYPE_ARRAY_NUMBER() {
return DYNAMO_TYPE_ARRAY_NUMBER;
}
static get DYNAMO_TYPE_ARRAY_STRING() {
return DYNAMO_TYPE_ARRAY_STRING;
}
static get DYNAMO_TYPE_BINARY() {
return DYNAMO_TYPE_BINARY;
}
static get DYNAMO_TYPE_BOOLEAN() {
return DYNAMO_TYPE_BOOLEAN;
}
static get DYNAMO_TYPE_ARRAY_NUMBER() {
return DYNAMO_TYPE_ARRAY_NUMBER;
}
static get DYNAMO_TYPE_MAP() {
return DYNAMO_TYPE_MAP;
}
static get DYNAMO_TYPE_NULL() {
return DYNAMO_TYPE_NULL;
}
static get DYNAMO_TYPE_NUMBER() {
return DYNAMO_TYPE_NUMBER;
}
static get DYNAMO_TYPE_STRING() {
return DYNAMO_TYPE_STRING;
}
}
module.exports = DynamoStream;