From e28349ff294280fd03f78ecd24446c104e1d4952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Nov 2018 11:02:28 +0100 Subject: [PATCH 1/3] Send metrics about memory and CPU usage --- app.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app.js b/app.js index 9d5eac2b..07a66f08 100755 --- a/app.js +++ b/app.js @@ -147,6 +147,25 @@ function isGteMinVersion(version, minVersion) { return false; } +setInterval(function memoryUsageMetrics () { + var memoryUsage = process.memoryUsage(); + Object.keys(memoryUsage).forEach(property => { + statsClient.gauge(`sqlapi.memory.${property}`, memoryUsage[property]); + }); +}, 5000); + +let previousCPUUsage = process.cpuUsage(); + +setInterval(function cpuUsageMetrics () { + const CPUUsage = process.cpuUsage(previousCPUUsage); + + Object.keys(CPUUsage).forEach(property => { + statsClient.gauge(`sqlapi.cpu.${property}`, CPUUsage[property]); + }); + + previousCPUUsage = CPUUsage; +}, 5000); + if (global.gc && isGteMinVersion(process.version, 6)) { var gcInterval = Number.isFinite(global.settings.gc_interval) ? global.settings.gc_interval : From 267baf621e1414cf8af0114e948212e89b36c0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Nov 2018 13:49:57 +0100 Subject: [PATCH 2/3] Send userm system, time and percent CPU metrics --- app.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 07a66f08..8e894a26 100755 --- a/app.js +++ b/app.js @@ -154,10 +154,32 @@ setInterval(function memoryUsageMetrics () { }); }, 5000); -let previousCPUUsage = process.cpuUsage(); +function getCPUUsage (oldUsage) { + let usage; + if (oldUsage && oldUsage._start) { + usage = Object.assign({}, process.cpuUsage(oldUsage._start.cpuUsage)); + usage.time = Date.now() - oldUsage._start.time; + } else { + usage = Object.assign({}, process.cpuUsage()); + usage.time = process.uptime() * 1000; // s to ms + } + + usage.percent = (usage.system + usage.user) / (usage.time * 10); + + Object.defineProperty(usage, '_start', { + value: { + cpuUsage: process.cpuUsage(), + time: Date.now() + } + }); + + return usage; +} + +let previousCPUUsage = getCPUUsage(); setInterval(function cpuUsageMetrics () { - const CPUUsage = process.cpuUsage(previousCPUUsage); + const CPUUsage = getCPUUsage(previousCPUUsage); Object.keys(CPUUsage).forEach(property => { statsClient.gauge(`sqlapi.cpu.${property}`, CPUUsage[property]); From fdaae20af78e20798b81e5a75ebdb1e589eb922f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Nov 2018 14:34:36 +0100 Subject: [PATCH 3/3] Use let instead of var --- app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 8e894a26..46c23582 100755 --- a/app.js +++ b/app.js @@ -148,7 +148,8 @@ function isGteMinVersion(version, minVersion) { } setInterval(function memoryUsageMetrics () { - var memoryUsage = process.memoryUsage(); + let memoryUsage = process.memoryUsage(); + Object.keys(memoryUsage).forEach(property => { statsClient.gauge(`sqlapi.memory.${property}`, memoryUsage[property]); });