Skip to content

Commit

Permalink
fix: histogram aggregator lastUpdateTime (#1567)
Browse files Browse the repository at this point in the history
* chore: fix histogram aggregator update time

* chore: add tests and fix logic

* chore: fix lint

* fix: use sinon faketimers instead of sleep
  • Loading branch information
AndrewGrachov authored Oct 5, 2020
1 parent fa9af4a commit 60d4dab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class HistogramAggregator implements HistogramAggregatorType {
}

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

Expand All @@ -54,7 +55,6 @@ export class HistogramAggregator implements HistogramAggregatorType {
return;
}
}

// value is above all observed boundaries
this._current.buckets.counts[this._boundaries.length] += 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import * as assert from 'assert';
import { HistogramAggregator } from '../../../src/export/aggregators';
import { Histogram } from '../../../src';
import { hrTime, hrTimeToMilliseconds } from '@opentelemetry/core';
import sinon = require('sinon');

describe('HistogramAggregator', () => {
describe('constructor()', () => {
Expand Down Expand Up @@ -99,6 +101,30 @@ describe('HistogramAggregator', () => {
});
});

describe('.timestamp', () => {
let clock: sinon.SinonFakeTimers;
before(() => {
clock = sinon.useFakeTimers({ toFake: ['hrtime'] });
});

it('should update point timestamp', () => {
const aggregator = new HistogramAggregator([100, 200]);
const timestamp = hrTimeToMilliseconds(hrTime());
const timeDiff = 10;
clock.tick(timeDiff);
aggregator.update(150);
assert.equal(
hrTimeToMilliseconds(aggregator.toPoint().timestamp) >=
timestamp + timeDiff,
true
);
});

after(() => {
clock.restore();
});
});

describe('.count', () => {
it('should return last checkpoint count', () => {
const aggregator = new HistogramAggregator([100]);
Expand Down

0 comments on commit 60d4dab

Please sign in to comment.