Skip to content

Commit

Permalink
Add more stats
Browse files Browse the repository at this point in the history
  • Loading branch information
XciD committed Jul 5, 2023
1 parent 611e228 commit 578cca0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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,
];
30 changes: 30 additions & 0 deletions lib/metrics/heapStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'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,
});

// Use this one metric's `collect` to set all metrics' values.
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);

0 comments on commit 578cca0

Please sign in to comment.