我正在从Docker容器中运行node.js应用程序。我正在尝试检索运行node.js应用程序的容器的系统使用率指标。现在我正在使用https://www.npmjs.com/package/dockerstats,但是它始终不显示cpu或内存使用情况,运行docker stats显示每个使用情况。
我的代码类似于以下内容:
let dockerId = setUp.getDockerId();
dockerId.then(dockerId => {
if (dockerId !== null) {
console.log(`dockerId: ${dockerId}`);
dockerstats.dockerContainerStats(dockerId, data => {
console.log(`cpu_percent: ${data.cpu_percent}`);
console.log(`memPercent: ${data.memPercent}`);
console.log(`memUsage: ${data.memUsage}`);
});
}
});
Run Code Online (Sandbox Code Playgroud)
setUp类类似于以下内容,并使用https://www.npmjs.com/package/docker-container-id:
const getId = require('docker-container-id');
module.exports = class setUp {
getDockerId () {
return getId().then(id => {
if (!id) {
return null;
}
return id;
});
}
}
Run Code Online (Sandbox Code Playgroud)