From 47c6d6bf44e91c57525767a3787d0cdeee946f8b Mon Sep 17 00:00:00 2001 From: hipocoro Date: Sat, 10 Aug 2024 14:07:23 +0200 Subject: [PATCH] fix: avoid duplicate buckets in histogram --- lib/histogram.js | 2 +- test/histogramTest.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/histogram.js b/lib/histogram.js index 539620c2..cefc6c41 100644 --- a/lib/histogram.js +++ b/lib/histogram.js @@ -32,7 +32,7 @@ class Histogram extends Metric { } } - this.upperBounds = this.buckets; + this.upperBounds = [...new Set(this.buckets)]; this.bucketValues = this.upperBounds.reduce((acc, upperBound) => { acc[upperBound] = 0; return acc; diff --git a/test/histogramTest.js b/test/histogramTest.js index 5ec89f97..07434764 100644 --- a/test/histogramTest.js +++ b/test/histogramTest.js @@ -92,6 +92,20 @@ describe.each([ expect(values[1].labels.le).toEqual(5); expect(values[2].labels.le).toEqual('+Inf'); }); + + it('should not have repeated LE buckets', async () => { + const histogram = new Histogram({ + name: 'test_histogram_2', + help: 'test', + buckets: [1, 1, 5], + }); + histogram.observe(6); + const values = (await histogram.get()).values; + expect(values[0].labels.le).toEqual(1); + expect(values[1].labels.le).toEqual(5); + expect(values[2].labels.le).toEqual('+Inf'); + }); + it('should group counts on each label set', async () => { const histogram = new Histogram({ name: 'test_histogram_2',