From 60d4dabde94268e52ad105847927329ab4668230 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 6 Oct 2020 01:59:25 +0300 Subject: [PATCH] fix: histogram aggregator lastUpdateTime (#1567) * chore: fix histogram aggregator update time * chore: add tests and fix logic * chore: fix lint * fix: use sinon faketimers instead of sleep --- .../src/export/aggregators/Histogram.ts | 2 +- .../test/export/aggregators/Histogram.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-metrics/src/export/aggregators/Histogram.ts b/packages/opentelemetry-metrics/src/export/aggregators/Histogram.ts index a44ea9836b..b877db33bd 100644 --- a/packages/opentelemetry-metrics/src/export/aggregators/Histogram.ts +++ b/packages/opentelemetry-metrics/src/export/aggregators/Histogram.ts @@ -45,6 +45,7 @@ export class HistogramAggregator implements HistogramAggregatorType { } update(value: number): void { + this._lastUpdateTime = hrTime(); this._current.count += 1; this._current.sum += value; @@ -54,7 +55,6 @@ export class HistogramAggregator implements HistogramAggregatorType { return; } } - // value is above all observed boundaries this._current.buckets.counts[this._boundaries.length] += 1; } diff --git a/packages/opentelemetry-metrics/test/export/aggregators/Histogram.test.ts b/packages/opentelemetry-metrics/test/export/aggregators/Histogram.test.ts index f2882858c2..a489ebf520 100644 --- a/packages/opentelemetry-metrics/test/export/aggregators/Histogram.test.ts +++ b/packages/opentelemetry-metrics/test/export/aggregators/Histogram.test.ts @@ -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()', () => { @@ -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]);