diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts index 1c0660e7c8..1482841106 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts @@ -54,10 +54,12 @@ export class SumAggregator implements Aggregator { * Returns the result of the merge of the given accumulations. */ merge(previous: SumAccumulation, delta: SumAccumulation): SumAccumulation { + const prevPv = previous.toPointValue(); + const deltaPv = delta.toPointValue(); if (delta.reset) { - return new SumAccumulation(delta.startTime, this.monotonic, delta.toPointValue(), delta.reset); + return new SumAccumulation(delta.startTime, this.monotonic, deltaPv, delta.reset); } - return new SumAccumulation(previous.startTime, this.monotonic, previous.toPointValue() + delta.toPointValue()); + return new SumAccumulation(previous.startTime, this.monotonic, prevPv + deltaPv); } /** @@ -74,7 +76,7 @@ export class SumAggregator implements Aggregator { if (this.monotonic && (prevPv > currPv)) { return new SumAccumulation(current.startTime, this.monotonic, currPv, true); } - return new SumAccumulation(current.startTime, this.monotonic, current.toPointValue() - previous.toPointValue()); + return new SumAccumulation(current.startTime, this.monotonic, currPv - prevPv); } toMetricData( diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/state/DeltaMetricProcessor.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/state/DeltaMetricProcessor.ts index 3d269cc14b..22c25edab0 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/state/DeltaMetricProcessor.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/state/DeltaMetricProcessor.ts @@ -49,7 +49,7 @@ export class DeltaMetricProcessor> { accumulation?.record(value); let delta = accumulation; if (this._cumulativeMemoStorage.has(attributes, hashCode)) { - // previous must present. + // has() returned true, previous is present. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const previous = this._cumulativeMemoStorage.get(attributes, hashCode)!; delta = this._aggregator.diff(previous, accumulation); @@ -62,7 +62,7 @@ export class DeltaMetricProcessor> { } /** - * Returns a collection of delta metrics. Their start time is the when first + * Returns a collection of delta metrics. Start time is the when first * time event collected. */ collect() { diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/state/TemporalMetricProcessor.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/state/TemporalMetricProcessor.ts index de152d58fe..6c648ac117 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/state/TemporalMetricProcessor.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/state/TemporalMetricProcessor.ts @@ -153,7 +153,7 @@ export class TemporalMetricProcessor> { const [key, record, hash] = next.value; if (last.has(key, hash)) { const lastAccumulation = last.get(key, hash); - // lastAccumulation must present. + // last.has() returned true, lastAccumulation is present. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const accumulation = aggregator.merge(lastAccumulation!, record); result.set(key, accumulation, hash); @@ -167,20 +167,13 @@ export class TemporalMetricProcessor> { } /** - * Calibrate the reported metric streams's startTime to lastCollectionTime. Leaves + * Calibrate the reported metric streams' startTime to lastCollectionTime. Leaves * the new stream to be the initial observation time unchanged. */ static calibrateStartTime>(last: AttributeHashMap, current: AttributeHashMap, lastCollectionTime: HrTime) { - const iterator = last.keys(); - let next = iterator.next(); - while (next.done !== true) { - const [key, hash] = next.value; - if (current.has(key, hash)) { - const currentAccumulation = current.get(key, hash); - currentAccumulation?.setStartTime(lastCollectionTime); - } - - next = iterator.next(); + for (const [key, hash] of last.keys()) { + const currentAccumulation = current.get(key, hash); + currentAccumulation?.setStartTime(lastCollectionTime); } return current; } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts index bb456b91d1..ac3df809a6 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts @@ -70,7 +70,7 @@ describe('SumAggregator', () => { const prev = aggregator.createAccumulation([0, 0]); prev.record(10); - // A new record with the value been reset. + // Create a new record that indicates a reset. const curr = aggregator.createAccumulation([1, 1]); curr.record(3);