diff --git a/CHANGELOG.md b/CHANGELOG.md index de63ceba..53d5e94d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ project adheres to [Semantic Versioning](http://semver.org/). - perf: Remove truthy conditionals from default metric collectors - fix: Preserve zero-valued Counter exemplars - perf: Remove object and array fallback truthiness from core metric paths +- fix: use independent TDigest stores in TimeWindowQuantiles ### Added diff --git a/lib/timeWindowQuantiles.js b/lib/timeWindowQuantiles.js index 357e7dfd..34a76151 100644 --- a/lib/timeWindowQuantiles.js +++ b/lib/timeWindowQuantiles.js @@ -23,7 +23,10 @@ class TimeWindowQuantiles { this.shouldRotate = maxAgeSeconds && ageBuckets; - this.ringBuffer = Array(ageBuckets).fill(new TDigest()); + this.ringBuffer = Array.from( + { length: ageBuckets || 1 }, + () => new TDigest(), + ); this.currentBuffer = 0; this.lastRotateTimestampMillis = Date.now(); diff --git a/test/timeWindowQuantilesTest.js b/test/timeWindowQuantilesTest.js index 8adc9bd1..52226273 100644 --- a/test/timeWindowQuantilesTest.js +++ b/test/timeWindowQuantilesTest.js @@ -88,5 +88,13 @@ describe('timeWindowQuantiles', () => { expect(td.centroids.size).toEqual(0); }); }); + + it('should keep buckets independent during rotation', () => { + instance.push(0); + jest.advanceTimersByTime(1001); + instance.push(100); + + expect(instance.percentile(0.5)).toEqual(50); + }); }); });