From 3abd04067d0a7a410222bbc78259b10ac10ae468 Mon Sep 17 00:00:00 2001 From: Joe Turgeon Date: Tue, 10 Sep 2019 15:02:39 -0500 Subject: [PATCH] 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]; }