diff --git a/lambda/utils.js b/lambda/utils.js index 745c58d..6dcb820 100644 --- a/lambda/utils.js +++ b/lambda/utils.js @@ -604,6 +604,20 @@ module.exports.computeAverageDuration = (durations, discardTopBottom) => { return averageDuration; }; +/** + * Returns true if provided string is valid json, false otherwise + * @param str string to check + * @returns {boolean} + */ +module.exports.isValidJSON = (str) => { + try { + JSON.parse(str); + return true; + } catch (e) { + return false; + } +}; + /** * Extract duration (in ms) from a given Lambda's CloudWatch log. */ @@ -611,7 +625,7 @@ module.exports.extractDuration = (log, durationType) => { if (!durationType){ durationType = DURATIONS.durationMs; // default to `durationMs` } - if (log.charAt(0) === '{') { + if (utils.isValidJSON(log)) { // extract from JSON (multi-line) return utils.extractDurationFromJSON(log, durationType); } else {