ca-apm-probe
Version:
CA APM Node.js Agent monitors real-time health and performance of Node.js applications
86 lines (71 loc) • 2.88 kB
JavaScript
/**
* Copyright (c) 2016 CA. All rights reserved.
*
* This software and all information contained therein is confidential and proprietary and
* shall not be duplicated, used, disclosed or disseminated in any way except as authorized
* by the applicable license agreement, without the express written permission of CA. All
* authorized reproductions must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT WARRANTY
* OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL CA BE
* LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR
* INDIRECT, FROM THE USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST
* PROFITS, BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF CA IS
* EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.
*/
;
var path = require('path');
const RESOURCE_METRIC_NODE = 'Node.js Runtime';
const RESOURCE_METRIC_PREFIX = RESOURCE_METRIC_NODE + ':';
function resolveEnv(name) {
return name.replace(/\$\{(\w*)\}/g, function (match, p1, p2, p3, offset, string) {
return process.env[p1];
});
}
function findAppName() {
var appNames = [];
var appScriptFilename = process.mainModule.filename;
var appPackage;
try {
appPackage = require(path.join(path.dirname(appScriptFilename), 'package.json'));
appNames[0] = appPackage.name;
} catch (error) {
}
// app name from application start script
appNames[1] = path.basename(appScriptFilename, '.js');
// app name from application start dir
appNames[2] = path.dirname(appScriptFilename).split(path.sep).slice(-1)[0];
for (var i = 0; i < appNames.length; i++) {
if (appNames[i]) {
return appNames[i];
}
}
return null;
}
function isArray(v) {
return (!!v) && (v.constructor === Array);
};
function isObject(v) {
return (!!v) && (v.constructor === Object);
};
function merge(source,obj){
for (var attrname in obj) {
source[attrname] = obj[attrname];
}
return source;
};
function getResMetricPrefix() {
var resMetricPrefix = RESOURCE_METRIC_PREFIX;
var cluster = require('cluster');
var workerNum = cluster.CAAPMPROBE_WORKERNUM;
if (workerNum) {
resMetricPrefix = RESOURCE_METRIC_NODE + '|worker' + workerNum + ':';
}
return resMetricPrefix;
}
// Reduce the fraction of a floating point number to a fixed quantity
// so that e.g. 0.6317000000000004 becomes 0.6317.
function fix(value) { return +(+value).toFixed(5); }
module.exports = {resolveEnv: resolveEnv, findAppName: findAppName, isArray: isArray, isObject: isObject, merge: merge, getResMetricPrefix: getResMetricPrefix, fix: fix};