From 3abd04067d0a7a410222bbc78259b10ac10ae468 Mon Sep 17 00:00:00 2001 From: Joe Turgeon Date: Tue, 10 Sep 2019 15:02:39 -0500 Subject: [PATCH 1/2] Fixing parsing of Cloudwatch Logs streamed to Lambda. --- handlers/convertString.js | 3 ++- handlers/formatCloudwatchLogs.js | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/handlers/convertString.js b/handlers/convertString.js index a2ac128..397d1ea 100644 --- a/handlers/convertString.js +++ b/handlers/convertString.js @@ -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 + '"'; }); diff --git a/handlers/formatCloudwatchLogs.js b/handlers/formatCloudwatchLogs.js index c079de4..eccf5eb 100644 --- a/handlers/formatCloudwatchLogs.js +++ b/handlers/formatCloudwatchLogs.js @@ -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]; } From 6c664a31a1ca4950ee865e75801a022281fc78e9 Mon Sep 17 00:00:00 2001 From: Joe Turgeon Date: Wed, 1 Jul 2020 09:56:07 -0500 Subject: [PATCH 2/2] Updating Cloudwatch Logs test assets to match AWS Lambda log format changes. --- test/assets/cloudwatch.data.json | 2 +- test/assets/cloudwatch.format.json | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/test/assets/cloudwatch.data.json b/test/assets/cloudwatch.data.json index 2277302..9c34f97 100644 --- a/test/assets/cloudwatch.data.json +++ b/test/assets/cloudwatch.data.json @@ -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"} ] } diff --git a/test/assets/cloudwatch.format.json b/test/assets/cloudwatch.format.json index 58cefe3..e892300 100644 --- a/test/assets/cloudwatch.format.json +++ b/test/assets/cloudwatch.format.json @@ -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", @@ -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", @@ -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" } ]