Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add heap stats + array buffer size #577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/defaultMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const processHandles = require('./metrics/processHandles');
const processRequests = require('./metrics/processRequests');
const processResources = require('./metrics/processResources');
const heapSizeAndUsed = require('./metrics/heapSizeAndUsed');
const heapStat = require('./metrics/heapStats');
const heapSpacesSizeAndUsed = require('./metrics/heapSpacesSizeAndUsed');
const version = require('./metrics/version');
const gc = require('./metrics/gc');
Expand All @@ -30,6 +31,7 @@ const metrics = {
processHandles,
processRequests,
heapSizeAndUsed,
heapStat,
heapSpacesSizeAndUsed,
version,
gc,
Expand Down
11 changes: 11 additions & 0 deletions lib/metrics/heapSizeAndUsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const safeMemoryUsage = require('./helpers/safeMemoryUsage');
const NODEJS_HEAP_SIZE_TOTAL = 'nodejs_heap_size_total_bytes';
const NODEJS_HEAP_SIZE_USED = 'nodejs_heap_size_used_bytes';
const NODEJS_EXTERNAL_MEMORY = 'nodejs_external_memory_bytes';
const NODEJS_ARRAY_BUFFER = 'nodejs_array_buffers_bytes';

module.exports = (registry, config = {}) => {
if (typeof process.memoryUsage !== 'function') {
Expand All @@ -24,6 +25,9 @@ module.exports = (registry, config = {}) => {
if (memUsage.external !== undefined) {
externalMemUsed.set(labels, memUsage.external);
}
if (memUsage.arrayBuffers !== undefined) {
arrayBufferUsed.set(labels, memUsage.arrayBuffers);
}
}
};

Expand All @@ -47,10 +51,17 @@ module.exports = (registry, config = {}) => {
registers,
labelNames,
});
const arrayBufferUsed = new Gauge({
name: namePrefix + NODEJS_ARRAY_BUFFER,
help: 'Node.js arrayBuffers size in bytes.',
registers,
labelNames,
});
};

module.exports.metricNames = [
NODEJS_HEAP_SIZE_TOTAL,
NODEJS_HEAP_SIZE_USED,
NODEJS_EXTERNAL_MEMORY,
NODEJS_ARRAY_BUFFER,
];
29 changes: 29 additions & 0 deletions lib/metrics/heapStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const Gauge = require('../gauge');
const v8 = require('v8');

const NODEJS_HEAP_STATS = 'nodejs_heap_stats';

module.exports = (registry, config = {}) => {
const registers = registry ? [registry] : undefined;
const namePrefix = config.prefix ? config.prefix : '';

const labels = config.labels ? config.labels : {};
const labelNames = ['stat', ...Object.keys(labels)];

const gauge = new Gauge({
name: namePrefix + NODEJS_HEAP_STATS,
help: `v8.getHeapStatistics() stats`,
labelNames,
registers,
});

gauge.collect = () => {
for (const [stat, value] of Object.entries(v8.getHeapStatistics())) {
gauge.set({stat: stat, ...labels}, value)
}
};
};

module.exports.metricNames = Object.values(NODEJS_HEAP_STATS);