Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lambda/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,28 @@ 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.
*/
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 {
Expand Down