feat: add heap snapshot

This commit is contained in:
Barış Soner Uşaklı
2025-07-11 08:50:53 -04:00
parent e4f56e8392
commit f88329dbbe
6 changed files with 47 additions and 2 deletions

View File

@@ -22,5 +22,6 @@
"connection-count": "Connection Count",
"guests": "Guests",
"info": "Info"
"info": "Info",
"heap-dump": "Heap Dump"
}

View File

@@ -174,6 +174,8 @@ paths:
$ref: 'read/admin/advanced/cache.yaml'
/api/admin/advanced/cache/dump:
$ref: 'read/admin/advanced/cache/dump.yaml'
/api/admin/advanced/heap/dump:
$ref: 'read/admin/advanced/heap/dump.yaml'
/api/admin/development/logger:
$ref: 'read/admin/development/logger.yaml'
/api/admin/development/info:

View File

@@ -0,0 +1,18 @@
get:
tags:
- admin
summary: Get nodejs heap snapshot
description: Downloads a Node.js heap snapshot for memory analysis.
parameters: []
responses:
"200":
description: Heap snapshot file (in .heapsnapshot format)
content:
application/octet-stream:
schema:
type: string
format: binary
examples:
heapSnapshot:
summary: Example Heap Snapshot Download
description: A binary heap snapshot file.

View File

@@ -143,3 +143,21 @@ async function getGitInfo() {
]);
return { hash: hash, hashShort: hash.slice(0, 6), branch: branch };
}
infoController.getHeapdump = async function (req, res) {
const v8 = require('v8');
const path = require('path');
const fs = require('fs');
const filename = path.join(nconf.get('upload_path'), `heapdump-${Date.now()}.heapsnapshot`);
const stored = v8.writeHeapSnapshot(filename, {});
res.download(stored, 'heapdump.heapsnapshot', (err) => {
if (err) {
winston.error(err.stack);
}
fs.unlink(stored, (unlinkErr) => {
if (unlinkErr) {
winston.error(unlinkErr.stack);
}
});
});
};

View File

@@ -81,6 +81,7 @@ function apiRoutes(router, name, middleware, controllers) {
router.get(`/api/${name}/groups/:groupname/csv`, middleware.ensureLoggedIn, helpers.tryRoute(controllers.admin.groups.getCSV));
router.get(`/api/${name}/analytics`, middleware.ensureLoggedIn, helpers.tryRoute(controllers.admin.dashboard.getAnalytics));
router.get(`/api/${name}/advanced/cache/dump`, middleware.ensureLoggedIn, helpers.tryRoute(controllers.admin.cache.dump));
router.get(`/api/${name}/advanced/heap/dump`, middleware.ensureLoggedIn, helpers.tryRoute(controllers.admin.info.getHeapdump));
const multer = require('multer');
const storage = multer.diskStorage({});

View File

@@ -5,7 +5,12 @@
</h5>
<div class="card-body">
<span>[[admin/development/info:nodes-responded, {nodeCount}, {timeout}]]</span>
<div class="d-flex flex-wrap justify-content-between align-items-center mb-2">
<span>[[admin/development/info:nodes-responded, {nodeCount}, {timeout}]]</span>
<a href="{config.relative_path}/api/admin/advanced/heap/dump" class="btn btn-primary btn-sm">
<i class="fa fa-download"></i> [[admin/development/info:heap-dump]]
</a>
</div>
<div class="table-responsive">
<table class="table table-sm text-sm">
<thead>