-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
140 lines (122 loc) · 3.81 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
var util = require('util');
var Writable = require('stream').Writable;
var AWS = require('aws-sdk');
var safeJsonStringify = require('safe-json-stringify');
var jsonStringify = safeJsonStringify ? safeJsonStringify : JSON.stringify;
module.exports = createCloudWatchStream;
function createCloudWatchStream(opts) {
return new CloudWatchStream(opts);
}
util.inherits(CloudWatchStream, Writable);
function CloudWatchStream(opts) {
Writable.call(this, {objectMode: true});
this.logGroupName = opts.logGroupName;
this.logStreamName = opts.logStreamName;
this.writeInterval = opts.writeInterval || 0;
if (opts.AWS) {
AWS = opts.AWS;
}
this.cloudwatch = new AWS.CloudWatchLogs(opts.cloudWatchLogsOptions);
this.queuedLogs = [];
this.sequenceToken = null;
this.writeQueued = false;
}
CloudWatchStream.prototype._write = function _write(record, _enc, cb) {
this.queuedLogs.push(record);
if (!this.writeQueued) {
this.writeQueued = true;
setTimeout(this._writeLogs.bind(this), this.writeInterval);
}
cb();
};
CloudWatchStream.prototype._writeLogs = function _writeLogs() {
if (this.sequenceToken === null) {
return this._getSequenceToken(this._writeLogs.bind(this));
}
var log = {
logGroupName: this.logGroupName,
logStreamName: this.logStreamName,
sequenceToken: this.sequenceToken,
logEvents: this.queuedLogs.map(createCWLog)
};
this.queuedLogs = [];
var obj = this;
writeLog();
function writeLog() {
obj.cloudwatch.putLogEvents(log, function (err, res) {
if (err) {
if (err.retryable) return setTimeout(writeLog, obj.writeInterval);
if (err.code === 'InvalidSequenceTokenException') {
return obj._getSequenceToken(function () {
log.sequenceToken = obj.sequenceToken;
setTimeout(writeLog, obj.writeInterval);
});
}
return obj._error(err);
}
obj.sequenceToken = res.nextSequenceToken;
if (obj.queuedLogs.length) {
return setTimeout(obj._writeLogs.bind(obj), obj.writeInterval);
}
obj.writeQueued = false;
});
}
};
CloudWatchStream.prototype._getSequenceToken = function _getSequenceToken(done) {
var params = {
logGroupName: this.logGroupName,
logStreamNamePrefix: this.logStreamName
};
var obj = this;
this.cloudwatch.describeLogStreams(params, function (err, data) {
if (err) {
if (err.name === 'ResourceNotFoundException') {
createLogGroupAndStream(obj.cloudwatch, obj.logGroupName, obj.logStreamName, createStreamCb);
return;
}
obj._error(err);
return;
}
if (data.logStreams.length === 0) {
createLogStream(obj.cloudwatch, obj.logGroupName, obj.logStreamName, createStreamCb);
return;
}
obj.sequenceToken = data.logStreams[0].uploadSequenceToken;
done();
});
function createStreamCb(err) {
if (err) return obj._error(err);
// call again to verify stream was created - silently fails sometimes!
obj._getSequenceToken(done);
}
};
CloudWatchStream.prototype._error = function _error(err) {
if (this.onError) return this.onError(err);
throw err;
};
function createLogGroupAndStream(cloudwatch, logGroupName, logStreamName, cb) {
cloudwatch.createLogGroup({
logGroupName: logGroupName
}, function (err) {
if (err) return err;
createLogStream(cloudwatch, logGroupName, logStreamName, cb);
});
}
function createLogStream(cloudwatch, logGroupName, logStreamName, cb) {
cloudwatch.createLogStream({
logGroupName: logGroupName,
logStreamName: logStreamName
}, cb);
}
function createCWLog(bunyanLog) {
var message = {};
for (var key in bunyanLog) {
if (key === 'time') continue;
message[key] = bunyanLog[key];
}
var log = {
message: jsonStringify(message),
timestamp: new Date(bunyanLog.time).getTime()
};
return log;
}