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

Commit

Permalink
fix: use long replace number
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 25, 2018
1 parent a28f503 commit b7ee0ed
Show file tree
Hide file tree
Showing 19 changed files with 684 additions and 130 deletions.
1 change: 1 addition & 0 deletions packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.2.1",
"long": "^4.0.0",
"mixin": "^0.2.0",
"module-hook": "^2.1.0",
"node-df": "^0.1.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/metrics/src/MetricsServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export class MetricsServerManager extends AbstractIndicator implements MetricsMa
result.set(MetricType.HISTOGRAM, metricRegistry.getHistograms(filter));
result.set(MetricType.METER, metricRegistry.getMeters(filter));
result.set(MetricType.TIMER, metricRegistry.getTimers(filter));
result.set(MetricType.FASTCOMPASS, metricRegistry.getFastCompasses(filter));

return result;
}
Expand All @@ -350,6 +351,7 @@ export class MetricsServerManager extends AbstractIndicator implements MetricsMa
result.set(MetricType.HISTOGRAM, allMetricsRegistry.getHistograms(filter));
result.set(MetricType.METER, allMetricsRegistry.getMeters(filter));
result.set(MetricType.TIMER, allMetricsRegistry.getTimers(filter));
result.set(MetricType.FASTCOMPASS, allMetricsRegistry.getFastCompasses(filter));

return result;
}
Expand Down
45 changes: 21 additions & 24 deletions packages/metrics/src/collect/CompactMetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {MetricsCollector} from './MetricsCollector';
import { BucketCounter, IFastCompass, MetricName } from '../common/index';
import {MetricObject} from './MetricObject';
import {ITimer} from '../common/metrics/Timer';
import {IHistogram} from '../common/metrics/Histogram';
import {ICounter} from '../common/metrics/Counter';
import {IMeter} from '../common/metrics/Meter';
import {Snapshot} from '../common/domain';
import { MetricsCollector } from './MetricsCollector';
import { BucketCounter, ICounter, IFastCompass, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common';
import { MetricObject } from './MetricObject';

const BigNumber = require('long');
/**
* 根据采集周期获取采集点,支持多个采集点的输出
*/
Expand All @@ -17,7 +13,7 @@ export class CompactMetricsCollector extends MetricsCollector {
let startTime = timestamp - this.reportInterval * 1000 + 1;

let totalCounts = timer.getInstantCount(startTime);
for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}

Expand Down Expand Up @@ -46,7 +42,7 @@ export class CompactMetricsCollector extends MetricsCollector {

if (counter instanceof BucketCounter) {
let totalCounts = (<BucketCounter> counter).getBucketCounts(startTime);
for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}
}
Expand All @@ -60,7 +56,7 @@ export class CompactMetricsCollector extends MetricsCollector {
let startTime = timestamp - this.reportInterval * 1000 + 1;
let totalCounts = meter.getInstantCount(startTime);

for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}

Expand All @@ -73,23 +69,24 @@ export class CompactMetricsCollector extends MetricsCollector {
let bucketInterval = fastCompass.getBucketInterval();

let start = this.getNormalizedStartTime(timestamp, bucketInterval);
let totalCount = 0;
let totalRt = 0;
let successCount = 0;
let hitCount = -1;
let totalCount = new BigNumber();
let totalRt = new BigNumber();
let successCount = new BigNumber();
let hitCount = new BigNumber(-1);

let countPerCategory = fastCompass.getMethodCountPerCategory(start);
for (let [key, value] of countPerCategory.entries()) {
for (let [ key, value ] of countPerCategory.entries()) {
if (value.has(start)) {
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start), start,
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
totalCount += value.get(start);

totalCount.add(value.get(start));
if ('success' === key) {
successCount += value.get(start);
successCount.add(value.get(start));
}
if ('hit' === key) {
hitCount = value.get(start);
successCount += value.get(start);
successCount.add(value.get(start));
}
} else {
this.addMetricWithSuffix(name, key + '_bucket_count', 0, start,
Expand All @@ -99,20 +96,20 @@ export class CompactMetricsCollector extends MetricsCollector {

for (let value of fastCompass.getMethodRtPerCategory(start).values()) {
if (value.has(start)) {
totalRt += value.get(start);
totalRt.add(value.get(start));
}
}
this.addMetricWithSuffix(name, 'bucket_count', totalCount, start,
this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'bucket_sum', totalRt, start,
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 >= 0) {
if (hitCount.gte(0)) {
this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
}
Expand Down
27 changes: 20 additions & 7 deletions packages/metrics/src/collect/MetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {MetricFilter, MetricName, MetricsCollectPeriodConfig} from '../common/index';
import {CollectMetricType, MetricObject} from './MetricObject';
import { MetricFilter, MetricName, MetricsCollectPeriodConfig } from '../common/index';
import { CollectMetricType, MetricObject } from './MetricObject';
import { Long } from '../domain';

export class MetricsCollector {
protected metrics: Array<MetricObject> = [];
Expand All @@ -13,7 +14,7 @@ export class MetricsCollector {
*/
protected filter: MetricFilter;

constructor(options: { globalTags, rateFactor, durationFactor, filter?, reportInterval?}) {
constructor(options: { globalTags, rateFactor, durationFactor, filter?, reportInterval? }) {
this.globalTags = options.globalTags;
this.rateFactor = options.rateFactor;
this.durationFactor = options.durationFactor;
Expand Down Expand Up @@ -77,12 +78,24 @@ export class MetricsCollector {

public rate(data, interval) {
if (interval === 0) return 0.0;
return data / interval;
if (typeof interval !== 'number' && interval.isZero()) return 0.0;

if (typeof data !== 'number') {
return <Long>data.div(interval).toString();
} else {
return data / interval;
}
}

public ratio(data, total) {
if (data > total) return 1.0;
if (total === 0) return 0.0;
return 1.0 * data / total;
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();
} else {
if (data > total) return 1.0;
if (total === 0) return 0.0;
return 1.0 * data / total;
}
}
}
27 changes: 15 additions & 12 deletions packages/metrics/src/collect/NormalMetricsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { MetricsCollector } from './MetricsCollector';
import { BucketCounter, ICounter, IFastCompass, 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 @@ -68,23 +70,24 @@ export class NormalMetricsCollector extends MetricsCollector {
let bucketInterval = fastCompass.getBucketInterval();

let start = this.getNormalizedStartTime(timestamp, bucketInterval);
let totalCount = 0;
let totalRt = 0;
let successCount = 0;
let hitCount = -1;
let totalCount = new BigNumber();
let totalRt = new BigNumber();
let successCount = new BigNumber();
let hitCount = new BigNumber(-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), start,
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
totalCount += value.get(start);

totalCount.add(value.get(start));
if ('success' === key) {
successCount += value.get(start);
successCount.add(value.get(start));
}
if ('hit' === key) {
hitCount = value.get(start);
successCount += value.get(start);
successCount.add(value.get(start));
}
} else {
this.addMetricWithSuffix(name, key + '_bucket_count', 0, start,
Expand All @@ -94,20 +97,20 @@ export class NormalMetricsCollector extends MetricsCollector {

for (let value of fastCompass.getMethodRtPerCategory(start).values()) {
if (value.has(start)) {
totalRt += value.get(start);
totalRt.add(value.get(start));
}
}
this.addMetricWithSuffix(name, 'bucket_count', totalCount, start,
this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'bucket_sum', totalRt, start,
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 >= 0) {
if (hitCount.gte(0)) {
this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/src/common/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Counting {
*
* @return the current count
*/
getCount(): number;
getCount(): number | string;
}


Expand Down
37 changes: 19 additions & 18 deletions packages/metrics/src/common/metrics/BucketCounter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {BaseCounter, ICounter} from './Counter';
import {MetricType} from '../MetricType';
import { BaseCounter, ICounter } from './Counter';

/**
* 提供分桶计数功能,每个桶统计一定时间间隔内的计数。
Expand Down Expand Up @@ -34,12 +33,12 @@ export interface IBucketCounter extends ICounter {
getBucketInterval();
}

interface Bucket {
export interface Bucket {
timestamp;
count;
}

class BucketDeque {
export class BucketDeque {

private queue: Array<Bucket> = [];

Expand All @@ -51,20 +50,24 @@ class BucketDeque {
this.size = length;
// init buckets
for (let i = 0; i < length; i++) {
this.queue[i] = {
timestamp: -1,
count: 0,
};
this.queue[ i ] = this.createQueueItem();
}
}

protected createQueueItem(): Bucket {
return {
timestamp: -1,
count: 0,
};
}

addLast(e: Bucket) {
this.current = (this.current + 1) % this.size;
this.queue[this.current] = e;
this.queue[ this.current ] = e;
}

peek(): Bucket {
return this.queue[this.current];
return this.queue[ this.current ];
}

/**
Expand All @@ -83,19 +86,19 @@ class BucketDeque {
let length = this.queue.length - 1;
let bucketList: Array<Bucket> = [];
let startPos = this.current;
let startTs = this.queue[this.current].timestamp;
let startTs = this.queue[ this.current ].timestamp;
if (startPos < 0) {
startPos = 0;
}
for (let i = startPos; i >= 0 && startPos - i < length; i--) {
bucketList.push(this.queue[i]);
bucketList.push(this.queue[ i ]);
}
for (let i = length; i > startPos + 1; i--) {
if (this.queue[i].timestamp > startTs) {
if (this.queue[ i ].timestamp > startTs) {
// the current index has been update during this iteration
// therefore the data shall not be collected
} else {
bucketList.push(this.queue[i]);
bucketList.push(this.queue[ i ]);
}
}
return bucketList;
Expand All @@ -105,12 +108,10 @@ class BucketDeque {

export class BucketCounter extends BaseCounter implements IBucketCounter {

type = MetricType.COUNTER;

/**
* 保存从创建开始累积的计数
*/
private totalCount: BaseCounter;
private totalCount: ICounter;

/**
* 保存最近N次的精确计数, 采用环形队列避免数据的挪动
Expand Down Expand Up @@ -178,7 +179,7 @@ export class BucketCounter extends BaseCounter implements IBucketCounter {
}

getCount() {
return this.totalCount.getCount();
return <number>this.totalCount.getCount();
}

inc(n?) {
Expand Down
Loading

0 comments on commit b7ee0ed

Please sign in to comment.