UNPKG

apminsight

Version:

monitor nodejs applications

128 lines (112 loc) 3.46 kB
var constants = require("../constants"); var utils = require("./../util/utils"); function Component(tracker) { this._componentName = tracker.component !== undefined ? tracker.component : tracker.getComponent(); this._rt = tracker.rt != undefined ? tracker.rt : tracker.getTime(); this._isExt = tracker.isExt !== undefined ? tracker.isExt : isExt(this._componentName); this._count = tracker.count >= 0 ? tracker.count : tracker.isError() ? 0 : 1; this._errorCount = tracker.errorcount >= 0 ? tracker.errorcount : tracker.isError() ? 1 : 0; this._host = tracker.host !== undefined ? tracker.host : tracker.getInfo().host || ""; this._port = tracker.port || tracker.getInfo().port || -1; if (tracker.instance_id) { this._instance_id = tracker.instance_id; } else if (tracker._dtrace) { this._instance_id = apmInsightAgentInstance .getInsInfo() .getInstanceId(); } } Component.prototype.getName = function () { return this._componentName; }; Component.prototype.getRt = function () { return this._rt; }; Component.prototype.isExt = function () { return this._isExt; }; Component.prototype.getCount = function () { return this._count; }; Component.prototype.getErrorCount = function () { return this._errorCount; }; Component.prototype.getHost = function () { return this._host; }; Component.prototype.getPort = function () { return this._port; }; Component.prototype.getInsId = function () { return this._instance_id; }; Component.prototype.isSameMachine = function (comp) { if (this.isExt()) { return ( comp && this.getHost() === comp.getHost() && this.getPort() === comp.getPort() ); } return true; }; Component.prototype.aggregate = function (component) { this._componentName = component.getName(); this._rt += component.getRt(); this._count += component.getCount(); this._errorCount += component.getErrorCount(); }; Component.prototype.aggregateAsNewComponent = function (comp) { var newCompInfo = {}; newCompInfo.component = this.getName(); newCompInfo.rt = this.getRt() + comp.getRt(); newCompInfo.isExt = this.isExt(); newCompInfo.count = this.getCount() + comp.getCount(); newCompInfo.errorcount = this.getErrorCount() + comp.getErrorCount(); newCompInfo.host = this.getHost(); newCompInfo.port = this.getPort(); if (this.getInsId()) { newCompInfo.instance_id = this.getInsId(); } return new Component(newCompInfo); }; Component.prototype.getInfoAsObj = function () { var info = {}; info.name = this.getName(); info.rt = this.getRt(); info.ct = this.getCount() + this.getErrorCount(); info.isExt = this.isExt(); if (this.getErrorCount() > 0) { info.error = this.getErrorCount(); } var port = parseInt(this.getPort()); if (!utils.isEmpty(this.getHost()) && port > 0) { info.host = this.getHost(); info.port = port; } if (this.getInsId()) { info.instance_id = this.getInsId(); } return info; }; function isExt(componentName) { return constants.externalComponents.indexOf(componentName) >= 0 ? 1 : 0; } module.exports = { Component: Component };