Created
October 31, 2018 16:20
-
-
Save jelder/853c1c1d0143e06c840f68f0e9509bfe to your computer and use it in GitHub Desktop.
Find logs for a single Lambda invocation and report timing data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { truncate, chain } = require("lodash") | |
const AWS = require("aws-sdk") | |
AWS.config.update({ correctClockSkew: true }) | |
const logs = new AWS.CloudWatchLogs() | |
const totalTimes = {} | |
const getAll = async ({ requestId, logGroupName, logStreamNames, nextToken, lastTimestamp }) => { | |
const response = await logs.filterLogEvents({ logGroupName, logStreamNames, nextToken }).promise() | |
for (const { message, timestamp } of response.events) { | |
if (!message.includes(requestId)) { | |
continue | |
} | |
if (message.startsWith("AWS_XRAY_")) { | |
continue | |
} | |
let duration | |
if (lastTimestamp) { | |
duration = timestamp - lastTimestamp | |
process.stdout.write(String(duration)) | |
} | |
process.stdout.write("\t") | |
if (message.startsWith("REPORT")) { | |
process.stdout.write(message) | |
} else if (message.startsWith("END")) { | |
process.stdout.write(message) | |
} else if (message.startsWith("START")) { | |
process.stdout.write(message) | |
} else { | |
const text = cleanMessage(message) | |
process.stdout.write(text) | |
totalTimes[text] = (totalTimes[text] || 0) + (duration || 0) | |
} | |
lastTimestamp = timestamp | |
} | |
if (response.nextToken) { | |
await getAll({ requestId, logGroupName, logStreamNames, nextToken: response.nextToken, lastTimestamp }) | |
} | |
} | |
const cleanMessage = message => { | |
// Skip timestamp and requestId, and flatten multiline messages | |
let [, , ...text] = message.replace(/\n/g, " ").split("\t") | |
text = text.join("") | |
if (text.includes("Lambda: SQS send finished")) { | |
text = "Lambda: SQS send finished" | |
} else if (text.includes("Lambda: Sending prod-google-dmz-ScanMailbox to prod-saas-dash-Lambda")) { | |
text = "Lambda: Sending prod-google-dmz-ScanMailbox to prod-saas-dash-Lambda" | |
} else if (text.includes("UPDATE `googleUsers` SET `mailboxLastScanTime`")) { | |
text = "UPDATE `googleUsers` SET `mailboxLastScanTime`" | |
} | |
return truncate(text, { length: 250 }) + "\n" | |
} | |
void (async function() { | |
await getAll({ | |
requestId: "71c79590-db97-11e8-8fd2-3797d904e344", | |
logGroupName: "/aws/lambda/prod-saas-dash-RefreshOrganization", | |
logStreamNames: ["2018/10/29/[$LATEST]10dba8002f2d4d0c92774e5b83bede3f"], | |
}) | |
console.log("Totals:") | |
const ordered = chain(totalTimes) | |
.toPairs() | |
.sortBy(([key, value]) => value) | |
.reverse() | |
.value() | |
for (const [message, timeSpent] of ordered) { | |
process.stdout.write(`${timeSpent}\t${message}`) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment