Skip to content

Instantly share code, notes, and snippets.

@hackjoy
Last active February 21, 2021 18:36
Show Gist options
  • Save hackjoy/7b34d8e7df5c4ab5c9d8927dd1a76baa to your computer and use it in GitHub Desktop.
Save hackjoy/7b34d8e7df5c4ab5c9d8927dd1a76baa to your computer and use it in GitHub Desktop.
Generate a heapdump file for analysis
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