Skip to content

Commit

Permalink
Merge pull request #29 from arithmetric/fix_cloudwatch_lambda_stream
Browse files Browse the repository at this point in the history
Fixing parsing of Cloudwatch Logs streamed to Lambda.
  • Loading branch information
arithmetric authored Jul 1, 2020
2 parents e8a9353 + 6c664a3 commit 288a69c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion handlers/convertString.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ exports.process = function(config) {

config.data = _.map(config.data, function(datum) {
var parts = _.map(datum, function(value, key) {
key = key.replace('-', '_').replace(/\W/g, '');
key = String(key).replace('-', '_').replace(/\W/g, '');
value = String(value).replace(/\n/g, ' ');
return key + '="' + value + '"';
});

Expand Down
11 changes: 7 additions & 4 deletions handlers/formatCloudwatchLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,33 @@ exports.process = function(config) {
if (parts && parts[1] === 'START') {
item.logType = 'start';
parts = item.message.match(/^START RequestId: ([a-z0-9-]+) Version: (\S+)/); // eslint-disable-line max-len
if (parts && parts.length === 3) {
if (parts && parts.length >= 3) {
item.requestId = parts[1];
item.lambdaVersion = parts[2];
delete item.message;
}
} else if (parts && parts[1] === 'REPORT') {
item.logType = 'report';
parts = item.message.match(/^REPORT RequestId: ([a-z0-9-]+)\tDuration: ([0-9.]+) ms\tBilled Duration: ([0-9.]+) ms \tMemory Size: ([0-9.]+) MB\tMax Memory Used: ([0-9.]+)/); // eslint-disable-line max-len
if (parts && parts.length === 6) {
parts = item.message.match(/^REPORT RequestId: ([a-z0-9-]+)\tDuration: ([0-9.]+) ms\tBilled Duration: ([0-9.]+) ms\tMemory Size: ([0-9.]+) MB\tMax Memory Used: ([0-9.]+)/); // eslint-disable-line max-len
if (parts && parts.length >= 6) {
item.requestId = parts[1];
item.duration = parts[2];
item.durationBilled = parts[3];
item.memConfigured = parts[4];
item.memUsed = parts[5];
delete item.message;
}
} else if (parts && parts[1] === 'END') {
item.logType = 'end';
parts = item.message.match(/^END RequestId: ([a-z0-9-]+)/);
if (parts && parts[1]) {
item.requestId = parts[1];
delete item.message;
}
} else {
item.logType = 'message';
parts = item.message.match(/^(.*)\t(.*)\t((.|\n)*)/m);
if (parts && parts.length === 5) {
if (parts && parts.length >= 5) {
item.requestId = parts[2];
item.message = parts[3];
}
Expand Down
2 changes: 1 addition & 1 deletion test/assets/cloudwatch.data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{"id":"1234567891","timestamp":1466263061419,"message":"2016-06-18T15:17:41.419Z\tabcdef-1234-5678-9012-abcdef\tExample log message 1\n"},
{"id":"1234567892","timestamp":1466263061429,"message":"2016-06-18T15:17:41.429Z\tabcdef-1234-5678-9012-abcdef\tExample log message 2\nLine 2\n"},
{"id":"1234567893","timestamp":1466263061439,"message":"END RequestId: abcdef-1234-5678-9012-abcdef\n"},
{"id":"1234567894","timestamp":1466263061449,"message":"REPORT RequestId: abcdef-1234-5678-9012-abcdef\tDuration: 432.10 ms\tBilled Duration: 500 ms \tMemory Size: 256 MB\tMax Memory Used: 123 MB\n"}
{"id":"1234567894","timestamp":1466263061449,"message":"REPORT RequestId: abcdef-1234-5678-9012-abcdef\tDuration: 432.10 ms\tBilled Duration: 500 ms\tMemory Size: 256 MB\tMax Memory Used: 123 MB\n"}
]
}
7 changes: 2 additions & 5 deletions test/assets/cloudwatch.format.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
"id":"1234567890",
"timestamp":1466263061409,
"message":"START RequestId: abcdef-1234-5678-9012-abcdef Version: $LATEST\n",
"logType": "start",
"requestId": "abcdef-1234-5678-9012-abcdef",
"lambdaVersion": "$LATEST",
Expand All @@ -29,8 +28,7 @@
"timestamp":1466263061439,
"date": "2016-06-18T15:17:41.439Z",
"requestId": "abcdef-1234-5678-9012-abcdef",
"logType": "end",
"message":"END RequestId: abcdef-1234-5678-9012-abcdef\n"
"logType": "end"
},
{
"id":"1234567894",
Expand All @@ -41,7 +39,6 @@
"duration": "432.10",
"durationBilled": "500",
"memConfigured": "256",
"memUsed": "123",
"message":"REPORT RequestId: abcdef-1234-5678-9012-abcdef\tDuration: 432.10 ms\tBilled Duration: 500 ms \tMemory Size: 256 MB\tMax Memory Used: 123 MB\n"
"memUsed": "123"
}
]

0 comments on commit 288a69c

Please sign in to comment.