Skip to content

Commit

Permalink
fix: log-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mvayngrib committed Jul 19, 2018
1 parent 5b77951 commit 5837794
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
31 changes: 22 additions & 9 deletions src/in-house-bot/log-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const createDummySendAlert = (logger: Logger) => async (events, idx) => {
logger.debug('TODO: send alert for sub-event', events[idx])
}

const REQUEST_LIFECYCLE_PROP = '__'
const LIFECYCLE = {
START: 'START',
END: 'END',
REPORT: 'REPORT',
}

export class LogProcessor {
private ignoreGroups: string[]
private sendAlert: SendAlert
Expand Down Expand Up @@ -68,7 +75,7 @@ export class LogProcessor {
})
}
})
.filter(notNull)
.filter(entry => entry && entry[REQUEST_LIFECYCLE_PROP] !== LIFECYCLE.END)
.filter(shouldSave)

const bad = logEvents.filter(shouldRaiseAlert)
Expand Down Expand Up @@ -109,7 +116,8 @@ export const fromLambda = (lambda: IPBLambda) => {
}

export const parseLogEntry = (entry: CloudWatchLogsSubEvent):ParsedEntry => {
const { requestId, body } = parseLogEntryMessage(entry.message)
const parsed = parseLogEntryMessage(entry.message)
const { requestId, body } = parsed
return {
id: entry.id,
timestamp: entry.timestamp,
Expand All @@ -119,26 +127,30 @@ export const parseLogEntry = (entry: CloudWatchLogsSubEvent):ParsedEntry => {
}

const REGEX = {
START: /^START RequestId:\s*([^\s]+)\s*Version:\s*(.*)$/i,
END: /^END RequestId:\s*([^\s]+)$/i,
REPORT: /^REPORT RequestId:\s*([^\s]+)\s*Duration:\s*([^\s]*)\s*ms\s*Billed Duration:\s*([^\s]*)\s*ms\s*Memory Size:\s*(\d+)\s*([a-zA-Z])+\s*Max Memory Used:\s*(\d+)\s*([a-zA-Z])+$/i,
START: /^START RequestId:\s*([^\s]+)\s*Version:\s*(.*)\s*$/i,
END: /^END RequestId:\s*([^\s]+)\s*$/i,
REPORT: /^REPORT RequestId:\s*([^\s]+)\s*Duration:\s*([^\s]*)\s*ms\s*Billed Duration:\s*([^\s]*)\s*ms\s*Memory Size:\s*(\d+)\s*([a-zA-Z])+\s*Max Memory Used:\s*(\d+)\s*([a-zA-Z])+\s*$/i,
}

export const parseLogEntryMessage = (message: string) => {
// "2018-07-18T16:26:47.716Z\t5b382e36-8aa7-11e8-9d6a-9343f875c9b4\t[JSON]
if (message.startsWith('START')) {
if (message.startsWith(LIFECYCLE.START)) {
const [requestId, version] = REGEX.START.exec(message).slice(1)
return {
[REQUEST_LIFECYCLE_PROP]: LIFECYCLE.START,
requestId,
version
}
}

if (message.startsWith('END')) {
return null
if (message.startsWith(LIFECYCLE.END)) {
return {
[REQUEST_LIFECYCLE_PROP]: LIFECYCLE.END,
requestId: REGEX.END.exec(message)[1]
}
}

if (message.startsWith('REPORT')) {
if (message.startsWith(LIFECYCLE.REPORT)) {
const [
requestId,
duration,
Expand All @@ -148,6 +160,7 @@ export const parseLogEntryMessage = (message: string) => {
] = REGEX.REPORT.exec(message).slice(1)

return {
[REQUEST_LIFECYCLE_PROP]: LIFECYCLE.REPORT,
requestId,
duration,
billedDuration,
Expand Down
7 changes: 6 additions & 1 deletion src/test/in-house-bot/log-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ REPORT RequestId: fe9e83ca-8af2-11e8-a5e2-b9d7a9e128fc\tDuration: 107.52 ms Bill

const expectedParsed = [
{
"__": "START",
"requestId": "fe9e83ca-8af2-11e8-a5e2-b9d7a9e128fc",
"version": "$LATEST"
},
Expand Down Expand Up @@ -107,8 +108,12 @@ const expectedParsed = [
"level": "SILLY"
}
},
null,
{
"__": "END",
"requestId": "fe9e83ca-8af2-11e8-a5e2-b9d7a9e128fc"
},
{
"__": "REPORT",
"requestId": "fe9e83ca-8af2-11e8-a5e2-b9d7a9e128fc",
"duration": "107.52",
"billedDuration": "200",
Expand Down

0 comments on commit 5837794

Please sign in to comment.