-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (133 loc) · 5.02 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
'use strict';
const
util = require('util'),
winston = require('winston'),
dynamodbIntegration = require('./lib/dynamodb-integration'),
isEmpty = require('lodash.isempty'),
isError = require('lodash.iserror'),
stringify = require('./lib/utils').stringify,
debug = require('./lib/utils').debug,
defaultFlushTimeoutMs = 10_000,
// we chose that as we wish to keep the message size under 400KB, to avoid truncation, and it should be enough as a safety net
maxMessageLength = 300_000;
const WinstonDynamoDB = function (options) {
winston.Transport.call(this, options);
this.level = options.level || 'info';
this.name = options.name || 'DynamoDB';
this.tableName = options.tableName;
this.logStreamName = options.logStreamName;
this.options = options;
const messageFormatter = options.messageFormatter ? options.messageFormatter : function (log) {
return [log.level, log.message].join(' - ')
};
this.formatMessage = options.jsonMessage ? stringify : messageFormatter;
this.proxyServer = options.proxyServer;
this.uploadRate = options.uploadRate || 2000;
this.logEvents = [];
this.errorHandler = options.errorHandler;
if (options.dynamoDbClient) {
this.dynamoDB = options.dynamoDbClient;
} else {
throw new Error("Pass configured DynamoDB client as 'dynamoDbClient' option");
}
debug('constructor finished');
};
util.inherits(WinstonDynamoDB, winston.Transport);
WinstonDynamoDB.prototype.log = function (info, callback) {
debug('log (called by winston)', info);
if (!isEmpty(info.message) || isError(info.message)) {
this.add(info);
}
if (!/^uncaughtException: /.test(info.message)) {
// do not wait, just return right away
return callback(null, true);
}
debug('message not empty, proceeding')
// clear interval and send logs immediately
// as Winston is about to end the process
clearInterval(this.intervalId);
this.intervalId = null;
this.submit(callback);
};
WinstonDynamoDB.prototype.createUploadInterval = function () {
this.intervalId = setInterval(() => {
this.submit();
}, this.uploadRate);
}
WinstonDynamoDB.prototype.add = function (log) {
debug('add log to queue', log);
const { message: originalMessage } = log;
if (isEmpty(originalMessage) || isError(originalMessage)) {
this.logEvents.push({
message: this.formatMessage(log),
timestamp: process.hrtime.bigint(),
rawMessage: log
});
}
else if (originalMessage.length <= maxMessageLength) {
this.logEvents.push({
message: this.formatMessage(log),
timestamp: process.hrtime.bigint(),
rawMessage: log
});
if (this.logEvents.length >= dynamodbIntegration.MAX_BATCH_ITEM_NUM) {
debug('Max items for batch reached - submitting and rescheduling interval');
clearInterval(this.intervalId);
this.createUploadInterval();
this.submit();
}
}
else {
for (let i = 0; i < originalMessage.length; i += maxMessageLength) {
let currentMessageSlice = originalMessage.slice(i, i + maxMessageLength);
this.logEvents.push({
message: this.formatMessage({...log, message: currentMessageSlice}),
timestamp: process.hrtime.bigint(),
rawMessage: log
});
debug(`Send each slice individually right away. current slice number: ${(i / maxMessageLength) + 1}`);
clearInterval(this.intervalId);
this.createUploadInterval();
this.submit();
}
}
if (!this.intervalId) {
debug('creating interval');
this.createUploadInterval()
}
};
WinstonDynamoDB.prototype.submit = function (callback) {
const defaultCallback = (err) => {
if (err) {
debug('error during submit', err, true);
this.errorHandler && this.errorHandler(err);
}
}
callback = callback || defaultCallback;
const streamName = typeof this.logStreamName === 'function' ?
this.logStreamName() : this.logStreamName;
if (isEmpty(this.logEvents)) {
return callback();
}
dynamodbIntegration.upload(
this.dynamoDB,
this.tableName,
streamName,
this.logEvents,
this.options,
callback
);
};
WinstonDynamoDB.prototype.kthxbye = function (callback) {
clearInterval(this.intervalId);
this.intervalId = null;
this.flushTimeout = this.flushTimeout || (Date.now() + defaultFlushTimeoutMs);
this.submit((function (error) {
if (error) return callback(error);
if (isEmpty(this.logEvents)) return callback();
if (Date.now() > this.flushTimeout) return callback(new Error('Timeout reached while waiting for logs to submit'));
else setTimeout(this.kthxbye.bind(this, callback), 0);
}).bind(this));
};
winston.transports.DynamoDB = WinstonDynamoDB;
module.exports = WinstonDynamoDB;