Skip to content

Commit

Permalink
chore(metrics): move each aggregator in its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Apr 6, 2020
1 parent de0e26e commit 55a011d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/export/Batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
CounterSumAggregator,
MeasureExactAggregator,
ObserverAggregator,
} from './Aggregator';
} from './aggregators';
import { MetricRecord, MetricKind, Aggregator } from './types';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Aggregator, Point } from '../types';
import { HrTime } from '@opentelemetry/api';
import { hrTime } from '@opentelemetry/core';

/** Basic aggregator which calculates a Sum from individual measurements. */
export class CounterSumAggregator implements Aggregator {
private _current: number = 0;
private _lastUpdateTime: HrTime = [0, 0];

update(value: number): void {
this._current += value;
this._lastUpdateTime = hrTime();
}

toPoint(): Point {
return {
value: this._current,
timestamp: this._lastUpdateTime,
};
}
}
19 changes: 19 additions & 0 deletions packages/opentelemetry-metrics/src/export/aggregators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './countersum';
export * from './observer';
export * from './measureexact';
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,10 @@
* limitations under the License.
*/

import { Aggregator, Distribution, Point } from './types';
import { Aggregator, Point, Distribution } from '../types';
import { HrTime } from '@opentelemetry/api';
import { hrTime } from '@opentelemetry/core';

/** Basic aggregator which calculates a Sum from individual measurements. */
export class CounterSumAggregator implements Aggregator {
private _current: number = 0;
private _lastUpdateTime: HrTime = [0, 0];

update(value: number): void {
this._current += value;
this._lastUpdateTime = hrTime();
}

toPoint(): Point {
return {
value: this._current,
timestamp: this._lastUpdateTime,
};
}
}

/** Basic aggregator for Observer which keeps the last recorded value. */
export class ObserverAggregator implements Aggregator {
private _current: number = 0;
private _lastUpdateTime: HrTime = [0, 0];

update(value: number): void {
this._current = value;
this._lastUpdateTime = hrTime();
}

toPoint(): Point {
return {
value: this._current,
timestamp: this._lastUpdateTime,
};
}
}

/** Basic aggregator keeping all raw values (events, sum, max and min). */
export class MeasureExactAggregator implements Aggregator {
private _distribution: Distribution;
Expand Down Expand Up @@ -82,4 +46,4 @@ export class MeasureExactAggregator implements Aggregator {
timestamp: this._lastUpdateTime,
};
}
}
}
37 changes: 37 additions & 0 deletions packages/opentelemetry-metrics/src/export/aggregators/observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Aggregator, Point } from '../types';
import { HrTime } from '@opentelemetry/api';
import { hrTime } from '@opentelemetry/core';

/** Basic aggregator for Observer which keeps the last recorded value. */
export class ObserverAggregator implements Aggregator {
private _current: number = 0;
private _lastUpdateTime: HrTime = [0, 0];

update(value: number): void {
this._current = value;
this._lastUpdateTime = hrTime();
}

toPoint(): Point {
return {
value: this._current,
timestamp: this._lastUpdateTime,
};
}
}
3 changes: 1 addition & 2 deletions packages/opentelemetry-metrics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export * from './BoundInstrument';
export * from './Meter';
export * from './Metric';
export * from './MeterProvider';
export * from './export/Aggregator';
export * from './export/aggregators';
export * from './export/ConsoleMetricExporter';
export * from './export/types';
export * from './export/Aggregator';
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { NoopLogger, hrTime, hrTimeToNanoseconds } from '@opentelemetry/core';
import {
CounterSumAggregator,
ObserverAggregator,
} from '../src/export/Aggregator';
} from '../src/export/aggregators';
import { ValueType } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { hashLabels } from '../src/Utils';
Expand Down

0 comments on commit 55a011d

Please sign in to comment.