Last active
November 16, 2017 22:27
-
-
Save kumar303/bb0ef2ab9f3865e2f2f7edd6b7539c59 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require('babel-register'); | |
const fs = require('fs'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
// This was a log file from production for a single NodeJS app instance. | |
// It had 7 OOM crashes. After each crash the app was restarted. | |
input: fs.createReadStream('/Users/kumar/Downloads/docker-addons-frontend.service.log'), | |
}); | |
console.log('Processing log'); | |
const stats = { | |
lastElapsed: 0, | |
highestElapsed: 0, | |
heapOOM: 0, | |
skipped: 0, | |
total: 0, | |
}; | |
// This means the app crashed. | |
const heapOOM = /JavaScript heap out of memory/; | |
const timeStampPrefix = /^[^{]+/; | |
var done = false; | |
var started = false; | |
rl.on('line', (line) => { | |
if (heapOOM.test(line)) { | |
stats.heapOOM += 1; | |
if (stats.heapOOM === 1) { | |
// This is when an OOM crash happens. The app gets restarted so we | |
// start tracking the number of requests. | |
started = true; | |
} | |
if (stats.heapOOM === 2) { | |
// This is when the next OOM crash happens. We can stop | |
// tracking the number of requests. | |
done = true; | |
} | |
return; | |
} | |
if (!started) { | |
return; | |
} | |
if (done) { | |
return; | |
} | |
stats.total += 1; | |
const fixedLine = line.replace(timeStampPrefix, ''); | |
let dataLine; | |
try { | |
dataLine = JSON.parse(fixedLine); | |
} catch(e) { | |
stats.skipped += 1; | |
return; | |
} | |
if (dataLine.start && dataLine.elapsed) { | |
stats.lastElapsed = dataLine.elapsed; | |
if (dataLine.elapsed > stats.highestElapsed) { | |
stats.highestElapsed = dataLine.elapsed; | |
} | |
} | |
}); | |
rl.on('close', () => { | |
console.log(stats); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment