Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (128 loc) · 3.91 KB
/
Copy pathindex.js
File metadata and controls
144 lines (128 loc) · 3.91 KB
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
var util = require('util');
var Writable = require('stream').Writable;
var AWS = require('aws-sdk');
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;
AWS.config.update({region: opts.region});
this.cloudwatch = new AWS.CloudWatchLogs();
this.queuedLogs = [];
this.sequenceToken = null;
this.writeQueued = false;
}
CloudWatchStream.prototype._write = function (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 () {
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;
this.cloudwatch.putLogEvents(log, function (err, res) {
if (err) {
log.sequenceToken = err.message.split(": ")[1];
if(err.code == "InvalidSequenceTokenException"){
obj.cloudwatch.putLogEvents(log, function (err, res) {
if (err) {
return obj._error(err);
}
obj.sequenceToken = res.nextSequenceToken;
if (obj.queuedLogs.length) {
return obj._writeLogs();
}
obj.writeQueued = false;
});
}else{
return obj._error(err);
}
}else{
obj.sequenceToken = res.nextSequenceToken;
if (obj.queuedLogs.length) {
return obj._writeLogs();
}
obj.writeQueued = false;
}
});
}
CloudWatchStream.prototype._getSequenceToken = function (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;
}
return obj._error(err);
}
if (data.logStreams.length == 0) {
createLogStream(obj.cloudwatch, obj.logGroupName, obj.logStreamName, createStreamCb);
return;
}
obj.sequenceToken = data.logStreams[0].uploadSequenceToken;
done();
});
function createStreamCb(err, res) {
if (err) return obj._error(err);
// call again to verify stream was created - silently fails sometimes!
obj._getSequenceToken(done);
}
}
CloudWatchStream.prototype._error = function (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: JSON.stringify(message),
timestamp: new Date(bunyanLog.time).getTime()
};
return log;
}
function BunyanCWError(message) {
this.name = 'BunyanCWError';
this.message = message || 'error in bunyan-cloudwatch';
}
BunyanCWError.prototype = Object.create(Error.prototype);
BunyanCWError.prototype.constructor = BunyanCWError;