From 56488b2c2d1bf89975886711e9549492314ab020 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 6 Apr 2020 18:58:52 +0200 Subject: [PATCH] Add resident memory and heap size metrics Implement the default resident memory and heap size metrics. --- lib/prometheus.js | 20 ++++++++++++++++++++ test/lib/prometheus.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/prometheus.js b/lib/prometheus.js index 2f1386c8..ce0cf0be 100644 --- a/lib/prometheus.js +++ b/lib/prometheus.js @@ -409,6 +409,26 @@ defaultCollectors.processCpuSecondsTotal = new Gauge( }, ); +defaultCollectors.processResidentMemoryBytes = new Gauge( + 'process_resident_memory_bytes', + "Resident memory size in bytes.", + { + callback: function() { + this.set(process.memoryUsage().rss); + }, + }, +); + +defaultCollectors.processHeapBytes = new Gauge( + 'process_heap_bytes', + "Process heap size in bytes.", + { + callback: function() { + this.set(process.memoryUsage().heapUsed); + }, + }, +); + defaultCollectors.processStartTimeSeconds = new Gauge( 'process_start_time_seconds', "Start time of the process since unix epoch in seconds." diff --git a/test/lib/prometheus.js b/test/lib/prometheus.js index afecec92..9ea6fb3f 100644 --- a/test/lib/prometheus.js +++ b/test/lib/prometheus.js @@ -419,6 +419,46 @@ describe("lib/prometheus", function() { ); }); }); + describe("processResidentMemoryBytes", function() { + it("should give a low value", async function() { + let results = [] + let collector = prometheus.defaultCollectors.processResidentMemoryBytes; + for await (let result of collector.collect()) { + results.push(result); + } + + assert( + results.length == 1, + "collector did not give exactly one result" + ); + + let value = results[0].samples.get('') + assert( + value < 200e6 && value > 0, + `resident memory is not between 0 and 200 MB (${value})` + ); + }); + }); + describe("processHeapBytes", function() { + it("should give a low value", async function() { + let results = [] + let collector = prometheus.defaultCollectors.processHeapBytes; + for await (let result of collector.collect()) { + results.push(result); + } + + assert( + results.length == 1, + "collector did not give exactly one result" + ); + + let value = results[0].samples.get('') + assert( + value < 200e6 && value > 0, + `heap is not between 0 and 200 MB (${value})` + ); + }); + }); }); describe("exposition()", function() {