From c9431b4f11640a37b47d280c4aea02078f5b0731 Mon Sep 17 00:00:00 2001 From: Adam Bezecny Date: Mon, 31 Mar 2025 14:32:08 +0200 Subject: [PATCH] extractDuration: improved json detection --- lambda/utils.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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 {