Last active
February 21, 2021 18:36
-
-
Save hackjoy/7b34d8e7df5c4ab5c9d8927dd1a76baa to your computer and use it in GitHub Desktop.
Generate a heapdump file for analysis
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
router.post( | |
'/heapdump-4E5AB3A6ECC24CD5B413A6D4DB5DB02FB4907018', | |
auth(config.server.authToken), | |
asyncHandler(async (req, res, next) => { | |
const { logger } = dependencies; | |
const hostname = os.hostname(); | |
const filepath = '/tmp/'; | |
const filename = `${Date.now()}.heapsnapshot`; | |
try { | |
heapdump.writeSnapshot(filepath + filename, err => { | |
if (err) { | |
throw err; | |
} | |
logger.info('Generated heapdump: ' + filename); | |
const s3 = new S3(); | |
const upload = new S3.ManagedUpload({ | |
service: s3, | |
params: { | |
Bucket: 'engineering', | |
Key: `heapdump/some-service/${hostname}/${filename}`, | |
Body: fs.createReadStream(filepath + filename) | |
} | |
}); | |
upload.send(err => { | |
if (err) { | |
logger.error('Error uploading', { err }); | |
throw err; | |
} | |
logger.info(`Uploaded heapdump: ${filename}`); | |
res.sendStatus(200); | |
}); | |
}); | |
} catch (e) { | |
logger.error(e); | |
res.sendStatus(500); | |
} | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment