|
| 1 | +var consts = require('../../../consts') |
| 2 | + |
| 3 | +function EdgeMetrics (options) { |
| 4 | + var _this = this |
| 5 | + this.collectorApi = options.collectorApi |
| 6 | + this.config = options.config |
| 7 | + this.collectInterval = this.config.collectInterval |
| 8 | + |
| 9 | + // metrics |
| 10 | + this.metrics = {} |
| 11 | + |
| 12 | + this.interval = setInterval(function () { |
| 13 | + _this.sendMetrics() |
| 14 | + }, this.collectInterval) |
| 15 | +} |
| 16 | + |
| 17 | +EdgeMetrics.prototype.initHost = function (data) { |
| 18 | + if (!this.metrics[data.protocol][data.targetHost]) { |
| 19 | + this.metrics[data.protocol][data.targetHost] = { |
| 20 | + targetServiceKey: data.targetServiceKey, |
| 21 | + responseTime: [], |
| 22 | + networkDelayIncoming: [], |
| 23 | + networkDelayOutgoing: [], |
| 24 | + status: { |
| 25 | + ok: 0, |
| 26 | + notOk: 0 |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + return this.metrics[data.protocol][data.targetHost] |
| 31 | +} |
| 32 | + |
| 33 | +EdgeMetrics.prototype.initProtocol = function (data) { |
| 34 | + if (!this.metrics[data.protocol]) { |
| 35 | + this.metrics[data.protocol] = {} |
| 36 | + } |
| 37 | + return this.metrics[data.protocol] |
| 38 | +} |
| 39 | + |
| 40 | +EdgeMetrics.prototype.report = function (data) { |
| 41 | + this.initProtocol(data) |
| 42 | + var edge = this.initHost(data) |
| 43 | + |
| 44 | + if (data.networkDelay.incoming) { |
| 45 | + edge.networkDelayIncoming.push(data.networkDelay.incoming) |
| 46 | + } |
| 47 | + |
| 48 | + if (data.networkDelay.outgoing) { |
| 49 | + edge.networkDelayOutgoing.push(data.networkDelay.outgoing) |
| 50 | + } |
| 51 | + |
| 52 | + edge.responseTime.push(data.responseTime) |
| 53 | + |
| 54 | + if (data.status === consts.EDGE_STATUS.OK) { |
| 55 | + edge.status.ok += 1 |
| 56 | + } else if (data.status === consts.EDGE_STATUS.NOT_OK) { |
| 57 | + edge.status.notOk += 1 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +EdgeMetrics.prototype.calculateTimes = function (items) { |
| 62 | + var sorted = items.sort(function (a, b) { |
| 63 | + return a - b |
| 64 | + }) |
| 65 | + |
| 66 | + var medianElementIndex = Math.round(sorted.length / 2) - 1 |
| 67 | + var ninetyFiveElementIndex = Math.round(sorted.length * 0.95) - 1 |
| 68 | + |
| 69 | + return { |
| 70 | + median: sorted[medianElementIndex], |
| 71 | + ninetyFive: sorted[ninetyFiveElementIndex] |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +EdgeMetrics.prototype.sendMetrics = function () { |
| 76 | + var _this = this |
| 77 | + |
| 78 | + var metrics = Object.keys(this.metrics).map(function (protocol) { |
| 79 | + var targetHosts = Object.keys(_this.metrics[protocol]).map(function (hostName) { |
| 80 | + var host = _this.metrics[protocol][hostName] |
| 81 | + return { |
| 82 | + name: hostName, |
| 83 | + metrics: { |
| 84 | + targetServiceKey: host.targetServiceKey, |
| 85 | + responseTime: _this.calculateTimes(host.responseTime), |
| 86 | + networkDelayIncoming: _this.calculateTimes(host.networkDelayIncoming), |
| 87 | + networkDelayOutgoing: _this.calculateTimes(host.networkDelayOutgoing), |
| 88 | + status: { |
| 89 | + ok: host.status.ok, |
| 90 | + notOk: host.status.notOk |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return { |
| 97 | + protocol: protocol, |
| 98 | + targetHosts: targetHosts |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + this.metrics = {} |
| 103 | + |
| 104 | + // if no metrics, don't send anything |
| 105 | + if (!metrics || !metrics.length) { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + this.collectorApi.sendEdgeMetrics({ |
| 110 | + timestamp: (new Date()).toISOString(), |
| 111 | + hostMetrics: metrics |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +function create (options) { |
| 116 | + return new EdgeMetrics(options) |
| 117 | +} |
| 118 | + |
| 119 | +module.exports.create = create |
0 commit comments