metrics: add stream api for system info
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
exports = module.exports = {
|
||||
getSystem,
|
||||
getSystemStream,
|
||||
|
||||
getContainers,
|
||||
|
||||
sendToGraphite
|
||||
@@ -16,6 +18,7 @@ const apps = require('./apps.js'),
|
||||
execSync = require('child_process').execSync,
|
||||
net = require('net'),
|
||||
os = require('os'),
|
||||
{ Readable } = require('stream'),
|
||||
safe = require('safetydance'),
|
||||
services = require('./services.js'),
|
||||
superagent = require('./superagent.js');
|
||||
@@ -226,7 +229,7 @@ async function getContainers(name, options) {
|
||||
};
|
||||
}
|
||||
|
||||
async function getSystemStats(options) {
|
||||
async function readSystemFromGraphite(options) {
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
const { fromSecs, intervalSecs, noNullPoints } = options;
|
||||
@@ -269,7 +272,7 @@ async function getSystemStats(options) {
|
||||
async function getSystem(options) {
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
const systemStats = await getSystemStats(options);
|
||||
const systemStats = await readSystemFromGraphite(options);
|
||||
|
||||
const appStats = {};
|
||||
for (const app of await apps.list()) {
|
||||
@@ -289,3 +292,32 @@ async function getSystem(options) {
|
||||
cpuCount: os.cpus().length
|
||||
};
|
||||
}
|
||||
|
||||
async function getSystemStream() {
|
||||
const INTERVAL_SECS = 2;
|
||||
let intervalId = null, oldCpuMetrics = null;
|
||||
|
||||
const metricsStream = new Readable({
|
||||
read(/*size*/) { /* ignored, we push via interval */ },
|
||||
destroy(error, callback) {
|
||||
clearInterval(intervalId);
|
||||
callback(error);
|
||||
}
|
||||
});
|
||||
|
||||
intervalId = setInterval(async () => {
|
||||
const memoryMetrics = await getMemoryMetrics();
|
||||
const cpuMetrics = await getCpuMetrics();
|
||||
|
||||
const cpuPercent = oldCpuMetrics ? (cpuMetrics.userMsecs + cpuMetrics.sysMsecs - oldCpuMetrics.userMsecs - oldCpuMetrics.sysMsecs) * 0.1 / INTERVAL_SECS : null;
|
||||
oldCpuMetrics = cpuMetrics;
|
||||
|
||||
const now = Date.now();
|
||||
metricsStream.push(JSON.stringify({
|
||||
cpu: [ cpuPercent, now ],
|
||||
memory: [ memoryMetrics.used, now ]
|
||||
}));
|
||||
}, INTERVAL_SECS*1000);
|
||||
|
||||
return metricsStream;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user