apminsight
Version:
monitor nodejs applications
187 lines (169 loc) • 5.23 kB
JavaScript
var utils = require("./../util/utils");
var logger = require("./../util/logger");
var webTxnMetric = {};
var bgTxnMetric = {};
var traceList = [];
var traceHistory = {};
var dbCallCount = 0;
var appMetricSum = {};
var appMetricAvg = {};
function checkAndIncludeInTrace(txn) {
var threshold = utils.getGenericThreshold(txn.getUrl());
if (traceList.length > threshold.getTraceMetricSize()) {
return;
}
var traceReason = 0x0000;
var errorsCount = Object.keys(txn.getErrorsInfo()).length;
var errorCodeCount = 0;
if (txn.getRt() >= threshold.getTraceThreshold()) {
traceReason |= 0x0008;
}
if(txn.getTxnType() == "webtxn"){
errorCodeCount = Object.keys(txn.getErrorCodeDetails()).length;
}
if (errorsCount > 0 || errorCodeCount > 0) {
traceReason |= 0x0010;
}
if (txn.getExtCallCount > threshold.getTxnTraceExtCallThreshold()) {
traceReason |= 0x0002;
}
if (txn.getTxnType() == "webtxn" && !utils.isEmpty(txn.getSyntheticKey())) {
traceReason |= 0x0020;
}
// TODO :: Support other trace reasons
if (traceReason > 0 && isCritical(txn, errorsCount)) {
traceList.push(txn);
}
}
function isCritical(txn, errorsCount, errorCodeCount) {
var txnInfo = txn.getUrl() + "-" + txn.getMethod();
if (traceHistory[txnInfo]) {
var lastCriticalReported = traceHistory[txnInfo];
if (lastCriticalReported.errcount < errorsCount) {
lastCriticalReported.errcount = errorsCount;
return true;
}
if (lastCriticalReported.errorCodeCount < errorCodeCount) {
lastCriticalReported.errorCodeCount = errorCodeCount;
return true;
}
if (lastCriticalReported.rt < txn.getRt()) {
lastCriticalReported.rt = txn.getRt();
return true;
}
if (lastCriticalReported.extcount < txn.getExtCallCount()) {
lastCriticalReported.extcount = txn.getExtCallCount();
return true;
}
return false;
} else {
var collectedInfo = {
rt: txn.getRt(),
errcount: errorsCount,
extcount: txn.getExtCallCount()
};
traceHistory[txnInfo] = collectedInfo;
}
return true;
}
function incrAppMetric(metricName, incrValue) {
if (!utils.isNonEmptyString(metricName)) {
logger.info("app metric invoked with improper value " + metricName);
return;
}
incrValue = utils.isNumber(incrValue) ? incrValue : 1;
if (apmInsightAgentInstance.getConfig().isDataExporterEnabled()) {
if (appMetricSum[metricName]) {
appMetricSum[metricName].push(incrValue);
} else {
appMetricSum[metricName] = [incrValue];
}
} else {
let metricValue = appMetricSum[metricName];
appMetricSum[metricName] = metricValue
? metricValue + incrValue
: incrValue;
}
}
function avgAppMetric(metricName, metricValue) {
if (!utils.isNonEmptyString(metricName) || !utils.isNumber(metricValue)) {
logger.info(
"app metric invoked with improper value :: metricname:" +
metricName +
", value:" +
metricValue
);
return;
}
let metricInfo = appMetricAvg[metricName];
if (apmInsightAgentInstance.getConfig().isDataExporterEnabled()) {
if (metricInfo) {
metricInfo.push(metricValue);
} else {
appMetricAvg[metricName] = [metricValue];
}
} else {
if (metricInfo) {
metricInfo.total += metricValue;
metricInfo.min =
metricValue < metricInfo.min ? metricValue : metricInfo.min;
metricInfo.max =
metricValue > metricInfo.max ? metricValue : metricInfo.min;
metricInfo.count++;
} else {
appMetricAvg[metricName] = {
total: metricValue,
min: metricValue,
max: metricValue,
count: 1
};
}
}
}
function getWebTxnMetric() {
return webTxnMetric;
}
function getBgTxnmetric() {
return bgTxnMetric;
}
function getTraceMetric() {
return traceList;
}
function getDbCallCount() {
return dbCallCount;
}
function getAppMetricSum() {
return appMetricSum;
}
function getAppMetricAvg() {
return appMetricAvg;
}
function updateAndGetDbCount(incr) {
dbCallCount += incr;
return dbCallCount;
}
function clearMetric() {
webTxnMetric = {};
bgTxnMetric = {};
traceList = [];
dbCallCount = 0;
appMetricAvg = {};
appMetricSum = {};
}
function clearTraceHistory() {
traceHistory = {};
}
module.exports = {
checkAndIncludeInTrace: checkAndIncludeInTrace,
getWebTxnMetric: getWebTxnMetric,
getBgTxnmetric: getBgTxnmetric,
getTraceMetric: getTraceMetric,
getDbCallCount: getDbCallCount,
updateAndGetDbCount: updateAndGetDbCount,
incrAppMetric: incrAppMetric,
avgAppMetric: avgAppMetric,
getAppMetricSum: getAppMetricSum,
getAppMetricAvg: getAppMetricAvg,
clearMetric: clearMetric,
clearTraceHistory: clearTraceHistory
};