apminsight
Version:
monitor nodejs applications
133 lines (118 loc) • 4.32 kB
JavaScript
var fs = require("fs");
var path = require("path");
var logger = require("../util/logger");
var utils = require("./../util/utils");
var constants = require("./../constants");
var rescodehandler = require("./responsecode");
function InstanceInfo(info, configInfo) {
this._instanceId = info.instanceId ? info.instanceId : "";
this._status = info.status;
this._modifiedTime = info.time;
this._lastReported = null;
this._retryCounter = rescodehandler.getRetryCounter(info.status, info.time);
this._apmInfoFilePath = "";
}
InstanceInfo.prototype.getInstanceId = function () {
return this._instanceId;
};
InstanceInfo.prototype.updateInstanceInfo = function (instanceId, resCode) {
this.updateLastReported();
if (!utils.isEmpty(instanceId)) {
if (this._instanceId !== instanceId || this._status !== resCode) {
this._instanceId = instanceId;
this._status = resCode ? resCode : constants.manageAgent;
this._modifiedTime = new Date().getTime();
this._retryCounter = 1;
this.writeInfotoFile();
}
}
};
InstanceInfo.prototype.getStatus = function () {
return this._status;
};
InstanceInfo.prototype.getModifiedTime = function () {
return this._modifiedTime;
};
InstanceInfo.prototype.getRetryCounter = function () {
return this._retryCounter;
};
InstanceInfo.prototype.updateLastReported = function () {
this._lastReported = new Date().getTime();
};
InstanceInfo.prototype.getLastReported = function () {
return this._lastReported;
};
InstanceInfo.prototype.setAppInfoFilePath = function(filePath){
this._apmInfoFilePath = filePath;
}
InstanceInfo.prototype.updateStatus = function (resCode) {
this.updateLastReported();
if (!rescodehandler.isValid(resCode)) {
return;
}
this._retryCounter = this._status === resCode ? this._retryCounter + 1 : 1;
if (this._instanceId && this._retryCounter === 1) {
this._status = resCode;
this._modifiedTime = new Date().getTime();
this._retryCounter;
this.writeInfotoFile();
}
if (rescodehandler.isRetryLimitExceeded(resCode, this._retryCounter)) {
logger.critical(
" Retry limit exceeded for response code :" +
resCode +
" so Agent goes to shutdown state"
);
this._status = 0;
var info = { status: 0, time: new Date().getTime() };
this.writeInfotoFile(info);
}
};
InstanceInfo.prototype.getAsJson = function () {
return {
instanceId: this._instanceId,
status: this._status,
time: this._modifiedTime
};
};
InstanceInfo.prototype.writeInfotoFile = function (info) {
try {
utils.createInfoFileFolder();
if (utils.isEmpty(this._apmInfoFilePath)) {
var appFilePath = this.getInfoFilePath();
if (utils.isEmpty(appFilePath)) {
logger.error("Failed to get a valid file path for apminsight.json");
return;
}
this.setAppInfoFilePath(appFilePath);
}
var writeStr = JSON.stringify(info ? info : this.getAsJson());
fs.writeFile(this._apmInfoFilePath, writeStr, "utf8", function (err) {
if (err) {
logger.error("Unable to write state information in file", err);
return;
}
logger.info(
"Instance info written in apminsight.json file successfully"
);
});
} catch (error) {
logger.error("Exception in writeInfotoFile", error);
}
};
InstanceInfo.prototype.getInfoFilePath = function() {
try {
var configDetails = apmInsightAgentInstance.getConfig();
var baseDirectory = configDetails.getBaseDir();
if (utils.isEmpty(baseDirectory) || utils.isEmpty(configDetails)) {
logger.critical("Base directory or config details are empty in getInfoFilePath");
return;
}
var appBaseDir = baseDirectory +"/"+ configDetails.getApplicationName() +"_"+configDetails.getApplicationPort();
return path.join(appBaseDir, "apminsight.json");
} catch (error) {
logger.error("Error in getInfoFilePath", error);
return;
}
}
module.exports = InstanceInfo;