diff --git a/packages/metrics/src/collect/CompactMetricsCollector.ts b/packages/metrics/src/collect/CompactMetricsCollector.ts index 45d4fcbf..8651d0ca 100644 --- a/packages/metrics/src/collect/CompactMetricsCollector.ts +++ b/packages/metrics/src/collect/CompactMetricsCollector.ts @@ -1,8 +1,7 @@ import { MetricsCollector } from './MetricsCollector'; -import { BucketCounter, ICounter, IFastCompass, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common'; +import { BucketCounter, ICounter, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common'; import { MetricObject } from './MetricObject'; -const BigNumber = require('long'); /** * 根据采集周期获取采集点,支持多个采集点的输出 */ @@ -65,54 +64,4 @@ export class CompactMetricsCollector extends MetricsCollector { .addMetricWithSuffix(name, 'm1', this.convertRate(meter.getOneMinuteRate()), timestamp); } - collectFastCompass(name: MetricName, fastCompass: IFastCompass, timestamp: number) { - let bucketInterval = fastCompass.getBucketInterval(); - - let start = this.getNormalizedStartTime(timestamp, bucketInterval); - let totalCount = new BigNumber(); - let totalRt = new BigNumber(); - let successCount = new BigNumber(); - let hitCount = new BigNumber(-1, -1); - - let countPerCategory = fastCompass.getMethodCountPerCategory(start); - for (let [ key, value ] of countPerCategory.entries()) { - if (value.has(start)) { - this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - - totalCount = totalCount.add(value.get(start)); - if ('success' === key) { - successCount = successCount.add(value.get(start)); - } - if ('hit' === key) { - hitCount = value.get(start); - successCount = successCount.add(value.get(start)); - } - } else { - this.addMetricWithSuffix(name, key + '_bucket_count', 0, start, - MetricObject.MetricType.DELTA, bucketInterval); - } - } - - for (let value of fastCompass.getMethodRtPerCategory(start).values()) { - if (value.has(start)) { - totalRt = totalRt.add(value.get(start)); - } - } - this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - this.addMetricWithSuffix(name, 'bucket_sum', totalRt.toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - this.addMetricWithSuffix(name, 'qps', this.rate(totalCount, bucketInterval), start, - MetricObject.MetricType.GAUGE, bucketInterval); - this.addMetricWithSuffix(name, 'rt', this.rate(totalRt, totalCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - this.addMetricWithSuffix(name, 'success_rate', this.ratio(successCount, totalCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - if (hitCount.gte(0)) { - this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - } - } - } diff --git a/packages/metrics/src/collect/MetricsCollector.ts b/packages/metrics/src/collect/MetricsCollector.ts index 9c3b8f5f..26f0b330 100644 --- a/packages/metrics/src/collect/MetricsCollector.ts +++ b/packages/metrics/src/collect/MetricsCollector.ts @@ -1,4 +1,4 @@ -import { MetricFilter, MetricName, MetricsCollectPeriodConfig } from '../common/index'; +import { IFastCompass, MetricFilter, MetricName, MetricsCollectPeriodConfig } from '../common'; import { CollectMetricType, MetricObject } from './MetricObject'; import { Long } from '../domain'; @@ -81,7 +81,7 @@ export class MetricsCollector { if (typeof interval !== 'number' && interval.isZero()) return 0.0; if (typeof data !== 'number') { - return data.div(interval).toString(); + return parseInt(data.div(interval).toString()); } else { return data / interval; } @@ -91,11 +91,62 @@ export class MetricsCollector { if (typeof data !== 'number') { if (data.gt(total)) return 1.0; if (total.eq(0)) return 0.0; - return data.div(total).toString(); + return parseInt(data.div(total).toString()); } else { if (data > total) return 1.0; if (total === 0) return 0.0; return 1.0 * data / total; } } + + collectFastCompass(name: MetricName, fastCompass: IFastCompass, timestamp: number) { + let bucketInterval = fastCompass.getBucketInterval(); + + let start = this.getNormalizedStartTime(timestamp, bucketInterval); + let totalCount = 0; + let totalRt = 0; + let successCount = 0; + let hitCount = -1; + + let countPerCategory = fastCompass.getMethodCountPerCategory(start); + for (let [ key, value ] of countPerCategory.entries()) { + if (value.has(start)) { + const data = parseInt(value.get(start).toString()); + this.addMetricWithSuffix(name, key + '_bucket_count', data, start, + MetricObject.MetricType.DELTA, bucketInterval); + + totalCount += data; + if ('success' === key) { + successCount += data; + } + if ('hit' === key) { + hitCount = data; + successCount += data; + } + } else { + this.addMetricWithSuffix(name, key + '_bucket_count', 0, start, + MetricObject.MetricType.DELTA, bucketInterval); + } + } + + for (let value of fastCompass.getMethodRtPerCategory(start).values()) { + if (value.has(start)) { + totalRt += parseInt(value.get(start).toString()); + } + } + this.addMetricWithSuffix(name, 'bucket_count', totalCount, start, + MetricObject.MetricType.DELTA, bucketInterval); + this.addMetricWithSuffix(name, 'bucket_sum', totalRt, start, + MetricObject.MetricType.DELTA, bucketInterval); + this.addMetricWithSuffix(name, 'qps', this.rate(totalCount, bucketInterval), start, + MetricObject.MetricType.GAUGE, bucketInterval); + this.addMetricWithSuffix(name, 'rt', this.rate(totalRt, totalCount), start, + MetricObject.MetricType.GAUGE, bucketInterval); + this.addMetricWithSuffix(name, 'success_rate', this.ratio(successCount, totalCount), start, + MetricObject.MetricType.GAUGE, bucketInterval); + if (hitCount === 0) { + this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start, + MetricObject.MetricType.GAUGE, bucketInterval); + } + } } diff --git a/packages/metrics/src/collect/NormalMetricsCollector.ts b/packages/metrics/src/collect/NormalMetricsCollector.ts index 873b7cfe..fdb70cc8 100644 --- a/packages/metrics/src/collect/NormalMetricsCollector.ts +++ b/packages/metrics/src/collect/NormalMetricsCollector.ts @@ -1,9 +1,7 @@ import { MetricsCollector } from './MetricsCollector'; -import { BucketCounter, ICounter, IFastCompass, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common'; +import { BucketCounter, ICounter, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common'; import { MetricObject } from './MetricObject'; -const BigNumber = require('long'); - export class NormalMetricsCollector extends MetricsCollector { collectTimer(name: MetricName, timer: ITimer, timestamp: number) { @@ -66,53 +64,4 @@ export class NormalMetricsCollector extends MetricsCollector { this.addInstantCountMetric(meter.getInstantCount(), name, meter.getInstantCountInterval(), timestamp); } - collectFastCompass(name: MetricName, fastCompass: IFastCompass, timestamp: number) { - let bucketInterval = fastCompass.getBucketInterval(); - - let start = this.getNormalizedStartTime(timestamp, bucketInterval); - let totalCount = new BigNumber(); - let totalRt = new BigNumber(); - let successCount = new BigNumber(); - let hitCount = new BigNumber(-1, -1); - - let countPerCategory = fastCompass.getMethodCountPerCategory(start); - for (let [ key, value ] of countPerCategory.entries()) { - if (value.has(start)) { - this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - - totalCount = totalCount.add(value.get(start)); - if ('success' === key) { - successCount = successCount.add(value.get(start)); - } - if ('hit' === key) { - hitCount = value.get(start); - successCount = successCount.add(value.get(start)); - } - } else { - this.addMetricWithSuffix(name, key + '_bucket_count', 0, start, - MetricObject.MetricType.DELTA, bucketInterval); - } - } - - for (let value of fastCompass.getMethodRtPerCategory(start).values()) { - if (value.has(start)) { - totalRt = totalRt.add(value.get(start)); - } - } - this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - this.addMetricWithSuffix(name, 'bucket_sum', totalRt.toString(), start, - MetricObject.MetricType.DELTA, bucketInterval); - this.addMetricWithSuffix(name, 'qps', this.rate(totalCount, bucketInterval), start, - MetricObject.MetricType.GAUGE, bucketInterval); - this.addMetricWithSuffix(name, 'rt', this.rate(totalRt, totalCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - this.addMetricWithSuffix(name, 'success_rate', this.ratio(successCount, totalCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - if (hitCount.gte(0)) { - this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start, - MetricObject.MetricType.GAUGE, bucketInterval); - } - } } diff --git a/packages/metrics/test/unit/collector/MetricsCollector.test.ts b/packages/metrics/test/unit/collector/MetricsCollector.test.ts index 9b69addd..1607ea48 100644 --- a/packages/metrics/test/unit/collector/MetricsCollector.test.ts +++ b/packages/metrics/test/unit/collector/MetricsCollector.test.ts @@ -1,5 +1,6 @@ import { BaseCounter, + BaseFastCompass, BaseHistogram, BaseMeter, BaseTimer, @@ -29,8 +30,6 @@ function delay(interval) { describe('/test/unit/collector/MetricsCollector.test.ts', () => { - let timestamp = Date.now(); - it('should create new CompactMetricsCollector', async () => { const globalReportInterval = 2; MetricsCollectPeriodConfig.getInstance().configGlobalPeriod(globalReportInterval); @@ -41,10 +40,16 @@ describe('/test/unit/collector/MetricsCollector.test.ts', () => { let h1 = new BaseHistogram(); let m1 = new BaseMeter(); let t1 = new BaseTimer(); + let f1 = new BaseFastCompass(2); c1.inc(); c2.inc(); + f1.record(2, 'success'); + f1.record(4, 'error'); + f1.record(3, 'success'); + + let timestamp = Date.now(); await delay(2100); collector.collectGauge(MetricName.build('collector.gauge'), 10, timestamp); @@ -53,6 +58,8 @@ describe('/test/unit/collector/MetricsCollector.test.ts', () => { collector.collectHistogram(MetricName.build('collector.histogram'), h1, timestamp); collector.collectMeter(MetricName.build('collector.meter'), m1, timestamp); collector.collectTimer(MetricName.build('collector.timer'), t1, timestamp); + collector.collectTimer(MetricName.build('collector.timer'), t1, timestamp); + collector.collectFastCompass(MetricName.build('collector.fastcompass'), f1, timestamp + 2000); let results = collector.build(); expect(findObject(results, 'collector.gauge').interval).to.equal(-1); @@ -99,9 +106,20 @@ describe('/test/unit/collector/MetricsCollector.test.ts', () => { expect(findObject(results, 'collector.timer.p95')).to.not.exist; expect(findObject(results, 'collector.timer.p75')).to.not.exist; expect(findObject(results, 'collector.timer.bucket_count').metricType).to.equal('DELTA'); + + expect(findObject(results, 'collector.fastcompass.success_bucket_count').timestamp.toString().length).to.equal(13); + expect(findObject(results, 'collector.fastcompass.success_bucket_count').value).to.equal(2); + expect(findObject(results, 'collector.fastcompass.error_bucket_count').value).to.equal(1); + expect(findObject(results, 'collector.fastcompass.bucket_count')).to.exist; + expect(findObject(results, 'collector.fastcompass.bucket_sum')).to.exist; + expect(findObject(results, 'collector.fastcompass.qps')).to.exist; + expect(findObject(results, 'collector.fastcompass.rt')).to.exist; + expect(findObject(results, 'collector.fastcompass.success_rate')).to.exist; + }); it('should create new NormalMetricsCollector', async () => { + let timestamp = Date.now(); let collector = new NormalMetricsCollector({globalTags: {}, rateFactor: 10, durationFactor: 10}); collector.collectGauge(MetricName.build('collector.gauge'), 10, timestamp); collector.collectCounter(MetricName.build('collector.counter'), new BucketCounter(), timestamp); @@ -171,10 +189,13 @@ describe('/test/unit/collector/MetricsCollector.test.ts', () => { expect(collector.getNormalizedStartTime(1514887712411, 10)).to.equal(1514887700000); expect(collector.getNormalizedStartTime(1514887712411, 15)).to.equal(1514887695000); expect(collector.getNormalizedStartTime(1514887712411, 20)).to.equal(1514887680000); + expect(collector.getNormalizedStartTime(1540958764377, 2)).to.equal(1540958762000); + expect(collector.getNormalizedStartTime(1540960034686, 2)).to.equal(1540960032000); }); it('should test output interval value', async () => { const globalReportInterval = 2; + const timestamp = Date.now(); MetricsCollectPeriodConfig.getInstance().configGlobalPeriod(globalReportInterval); let collector = new CompactMetricsCollector({globalTags: {}, rateFactor: 10, durationFactor: 10, reportInterval: globalReportInterval});