Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
refactor: parseInt for collector
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 31, 2018
1 parent 290e988 commit 92c70f9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 109 deletions.
53 changes: 1 addition & 52 deletions packages/metrics/src/collect/CompactMetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -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');
/**
* 根据采集周期获取采集点,支持多个采集点的输出
*/
Expand Down Expand Up @@ -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);
}
}

}
57 changes: 54 additions & 3 deletions packages/metrics/src/collect/MetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -81,7 +81,7 @@ export class MetricsCollector {
if (typeof interval !== 'number' && interval.isZero()) return 0.0;

if (typeof data !== 'number') {
return <Long>data.div(interval).toString();
return parseInt(data.div(interval).toString());
} else {
return data / interval;
}
Expand All @@ -91,11 +91,62 @@ export class MetricsCollector {
if (typeof data !== 'number') {
if (<Long>data.gt(total)) return 1.0;
if (<Long>total.eq(0)) return 0.0;
return <Long>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);
}
}
}
53 changes: 1 addition & 52 deletions packages/metrics/src/collect/NormalMetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 23 additions & 2 deletions packages/metrics/test/unit/collector/MetricsCollector.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BaseCounter,
BaseFastCompass,
BaseHistogram,
BaseMeter,
BaseTimer,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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});
Expand Down

0 comments on commit 92c70f9

Please sign in to comment.