-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (47 loc) · 1.78 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
/**
* CloudWatch logs connector for SumoLogic through AWS Lambda
*
* Created by santthosh on 12/26/15.
*/
var zlib = require('zlib');
var config = require('config.json');
var request = require('request');
exports.handler = function(event, context) {
var payload = new Buffer(event.awslogs.data, 'base64');
zlib.gunzip(payload, function(e, result) {
if (e) {
context.fail(e);
} else {
result = JSON.parse(result.toString('utf8'));
var lines = "";
result.logEvents.forEach(function(log) {
lines += log.timestamp + " name=" + result.logGroup +
" stream="+ result.logStream +
" message=" + JSON.stringify(log.message).replace(/\\\n$/g,'').replace(/\\\\"$/g,"'") + "\n";
});
zlib.gzip(lines, function(err, zipped) {
if (!err) {
request(
{
method: 'POST',
uri: config.sumologic.collector.url,
headers: {
'Content-Encoding': 'gzip',
'X-Sumo-Name': result.logGroup
},
body: zipped
}, function (error, response, body) {
if(response.statusCode == 200){
context.succeed("Successfully processed " + result.logEvents.length + " log events.");
} else {
context.fail(error);
}
}
)
} else {
context.fail(err);
}
});
}
});
};