ca-apm-probe
Version:
CA APM Node.js Agent monitors real-time health and performance of Node.js applications
38 lines (32 loc) • 1.27 kB
JavaScript
var logger = require("./logger.js");
const fs = require("fs");
var findContainerId = function () {
try {
// if this machine is docker
if (require('is-docker')() || require('is-kubernetes')()) {
var output = fs.readFileSync('/proc/self/cgroup', 'utf8');
var lines = output.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line) {
if(output.includes('containerd')){
line = line.substring(line.lastIndexOf(':') + 1, line.length);
} else {
line = line.substring(line.lastIndexOf('/') + 1, line.length);
// specific to older docker versions 1.12
line = line.replace(new RegExp('docker-', 'g'), '').replace(new RegExp('.scope', 'g'), '');
}
// is it full container id
if (line.length == 64) {
return line;
}
}
}
}
}
catch (err) {
logger.debug('error while finding container id: %s', err.message);
}
return null;
};
module.exports = findContainerId;