Skip to content

Commit 2541bd1

Browse files
committed
chore(promHistogram): remove Merge implementation and add LinearBuckets
1 parent 9c49c82 commit 2541bd1

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

prometheus_histogram.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88
)
99

10-
var defaultUpperBounds = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
10+
var PrometheusHistogramDefaultBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
1111

1212
// Prometheus Histogram is a histogram for non-negative values with pre-defined buckets
1313
//
@@ -176,7 +176,7 @@ func GetOrCreatePrometheusHistogramExt(name string, upperBounds []float64) *Prom
176176
}
177177

178178
func newPrometheusHistogram(upperBounds []float64) *PrometheusHistogram {
179-
validateBuckets(upperBounds)
179+
mustValidateBuckets(upperBounds)
180180
last := len(upperBounds) - 1
181181
if math.IsInf(upperBounds[last], +1) {
182182
upperBounds = upperBounds[:last] // ignore +Inf bucket as it is covered anyways
@@ -189,15 +189,34 @@ func newPrometheusHistogram(upperBounds []float64) *PrometheusHistogram {
189189
return &h
190190
}
191191

192-
func validateBuckets(upperBounds []float64) {
192+
func mustValidateBuckets(upperBounds []float64) {
193+
if err := ValidateBuckets(upperBounds); err != nil {
194+
panic(err)
195+
}
196+
}
197+
198+
func ValidateBuckets(upperBounds []float64) error {
193199
if len(upperBounds) == 0 {
194-
panic("no upper bounds were given for the buckets")
200+
return fmt.Errorf("no upper bounds were given for the buckets")
195201
}
196202
for i := 0; i < len(upperBounds)-1; i++ {
197203
if upperBounds[i] >= upperBounds[i+1] {
198-
panic("upper bounds for the buckets must be strictly increasing")
204+
return fmt.Errorf("upper bounds for the buckets must be strictly increasing")
199205
}
200206
}
207+
return nil
208+
}
209+
210+
func LinearBuckets(start, width float64, count int) []float64 {
211+
if count < 1 {
212+
panic("LinearBuckets needs a positive count")
213+
}
214+
upperBounds := make([]float64, count)
215+
for i := range upperBounds {
216+
upperBounds[i] = start
217+
start += width
218+
}
219+
return upperBounds
201220
}
202221

203222
func (h *PrometheusHistogram) marshalTo(prefix string, w io.Writer) {

prometheus_histogram_test.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ prefix_count 405
4444
testMarshalTo(t, h, "prefix", expected)
4545

4646
// make sure that if the +Inf bucket is manually specified it gets ignored and we have the same resutls at the end
47-
h2 := NewPrometheusHistogramExt("TestPrometheusHistogram2", append(defaultUpperBounds, math.Inf(+1)))
47+
h2 := NewPrometheusHistogramExt("TestPrometheusHistogram2", append(PrometheusHistogramDefaultBuckets, math.Inf(+1)))
4848

4949
h.Reset()
5050

@@ -63,46 +63,41 @@ prefix_count 405
6363
testMarshalTo(t, h2, "prefix", expected)
6464
}
6565

66-
func TestPrometheusHistogramMerge(t *testing.T) {
67-
name := `TestPrometheusHistogramMerge`
68-
h := NewPrometheusHistogram(name)
69-
// Write data to histogram
70-
for i := 0; i <= 10_100; i += 25 { // from 0 to 10'100 ms in 25ms steps
71-
h.Update(float64(i) * 1e-3)
72-
}
66+
func TestPrometheusHistogramLinearBuckets(t *testing.T) {
67+
const expected string = `prefix_bucket{le="0.5"} 1
68+
prefix_bucket{le="1.5"} 2
69+
prefix_bucket{le="2.5"} 3
70+
prefix_bucket{le="3.5"} 4
71+
prefix_bucket{le="4.5"} 5
72+
prefix_bucket{le="5.5"} 6
73+
prefix_bucket{le="6.5"} 7
74+
prefix_bucket{le="7.5"} 8
75+
prefix_bucket{le="8.5"} 9
76+
prefix_bucket{le="9.5"} 10
77+
prefix_bucket{le="+Inf"} 11
78+
prefix_sum 55
79+
prefix_count 11
80+
`
81+
name := "TestPrometheusHistogramLinearBuckets"
82+
upperBounds := LinearBuckets(0.5, 1.0, 10)
83+
h := NewPrometheusHistogramExt(name, upperBounds)
7384

74-
b := NewPrometheusHistogram("test")
75-
for i := 0; i <= 10_100; i += 25 { // from 0 to 10'100 ms in 25ms steps
76-
h.Update(float64(i) * 1e-3)
85+
// Write data to histogram
86+
for i := 0; i <= 10; i++ { // from 0 to 10
87+
h.Update(float64(i))
7788
}
7889

79-
h.Merge(b)
80-
8190
// Make sure the histogram prints <prefix>_bucket on marshalTo call
82-
testMarshalTo(t, h, "prefix", `prefix_bucket{le="0.005"} 2
83-
prefix_bucket{le="0.01"} 2
84-
prefix_bucket{le="0.025"} 4
85-
prefix_bucket{le="0.05"} 6
86-
prefix_bucket{le="0.1"} 10
87-
prefix_bucket{le="0.25"} 22
88-
prefix_bucket{le="0.5"} 42
89-
prefix_bucket{le="1"} 82
90-
prefix_bucket{le="2.5"} 202
91-
prefix_bucket{le="5"} 402
92-
prefix_bucket{le="10"} 802
93-
prefix_bucket{le="+Inf"} 810
94-
prefix_sum 4090.5
95-
prefix_count 810
96-
`)
91+
testMarshalTo(t, h, "prefix", expected)
9792

93+
// Make sure we panic when the count of linear buckets is < 1
9894
func() {
9995
defer func() {
10096
if r := recover(); r == nil {
101-
t.Error("Merge did not panic with mimatched buckets.")
97+
t.Errorf("LinearBuckets should've panicked with a count of 0.")
10298
}
10399
}()
104-
h2 := NewPrometheusHistogramExt("TestPrometheusHistogramMerge2", []float64{14})
105-
h.Merge(h2)
100+
_ = LinearBuckets(.14, .22, 0)
106101
}()
107102
}
108103

set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (s *Set) GetOrCreateHistogram(name string) *Histogram {
140140
//
141141
// The returned histogram is safe to use from concurrent goroutines.
142142
func (s *Set) NewPrometheusHistogram(name string) *PrometheusHistogram {
143-
return s.NewPrometheusHistogramExt(name, defaultUpperBounds)
143+
return s.NewPrometheusHistogramExt(name, PrometheusHistogramDefaultBuckets)
144144
}
145145

146146

@@ -176,7 +176,7 @@ func (s *Set) NewPrometheusHistogramExt(name string, upperBounds []float64) *Pro
176176
//
177177
// Performance tip: prefer NewPrometheusHistogram instead of GetOrCreatePrometheusHistogram.
178178
func (s *Set) GetOrCreatePrometheusHistogram(name string) *PrometheusHistogram {
179-
return s.GetOrCreatePrometheusHistogramExt(name, defaultUpperBounds)
179+
return s.GetOrCreatePrometheusHistogramExt(name, PrometheusHistogramDefaultBuckets)
180180
}
181181

182182
// GetOrCreatePrometheusHistogramExt returns registered prometheus histogram in

0 commit comments

Comments
 (0)