Skip to content

Commit

Permalink
fix(sdk-metrics): merge uncollected delta accumulations
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Mar 10, 2023
1 parent ecb5ebe commit 20e8415
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/sdk-metrics/src/state/DeltaMetricProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class DeltaMetricProcessor<T extends Maybe<Accumulation>> {
this._aggregator.createAccumulation(collectionTime);
accumulation?.record(value);
let delta = accumulation;
// Diff with recorded cumulative memo.
if (this._cumulativeMemoStorage.has(attributes, hashCode)) {
// has() returned true, previous is present.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -66,6 +67,13 @@ export class DeltaMetricProcessor<T extends Maybe<Accumulation>> {
)!;
delta = this._aggregator.diff(previous, accumulation);
}
// Merge with uncollected active delta.
if (this._activeCollectionStorage.has(attributes, hashCode)) {
// has() returned true, previous is present.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const active = this._activeCollectionStorage.get(attributes, hashCode)!;
delta = this._aggregator.merge(active, delta);
}

// Save the current record and the delta record.
this._cumulativeMemoStorage.set(attributes, accumulation, hashCode);
Expand Down
20 changes: 20 additions & 0 deletions packages/sdk-metrics/test/state/DeltaMetricProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ describe('DeltaMetricProcessor', () => {
assert.strictEqual(accumulation?.toPointValue(), 11);
}
});

it('should merge with active delta of accumulations', () => {
const metricProcessor = new DeltaMetricProcessor(new SumAggregator(true));

{
const measurements = new AttributeHashMap<number>();
measurements.set({}, 10);
metricProcessor.batchCumulate(measurements, [0, 0]);
}

{
const measurements = new AttributeHashMap<number>();
measurements.set({}, 20);
metricProcessor.batchCumulate(measurements, [1, 1]);
}

const accumulations = metricProcessor.collect();
const accumulation = accumulations.get({});
assert.strictEqual(accumulation?.toPointValue(), 20);
});
});

describe('collect', () => {
Expand Down

0 comments on commit 20e8415

Please sign in to comment.