Skip to content

Commit 3e50edd

Browse files
author
beorn7
committed
Do not count the +Inf bucket for bucket creation.
Add explanation for DefBuckets.
1 parent 4c4f51d commit 3e50edd

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

prometheus/examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func ExampleHistogram() {
457457
temps := prometheus.NewHistogram(prometheus.HistogramOpts{
458458
Name: "pond_temperature_celsius",
459459
Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
460-
Buckets: prometheus.LinearBuckets(20, 5, 6), // 6 buckets, each 5 centigrade wide.
460+
Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
461461
})
462462

463463
// Simulate some observations.

prometheus/histogram.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,25 @@ type Histogram interface {
4747
Observe(float64)
4848
}
4949

50-
// DefBuckets are the default Histogram buckets. Most likely, you want to define
51-
// buckets customized to your use case.
50+
// DefBuckets are the default Histogram buckets. The default buckets are
51+
// tailored to broadly measure response time in seconds for a typical online
52+
// serving system. Most likely, however, you will be required to define buckets
53+
// customized to your use case.
5254
var (
5355
DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
5456
)
5557

5658
// LinearBuckets creates 'count' buckets, each 'width' wide, where the lowest
57-
// bucket has an upper bound of 'start'. The final +Inf bucket is counted
58-
// towards the total 'count', but not included in the returned slice. The
59-
// returned slice is meant to be used for the Buckets field of HistogramOpts.
59+
// bucket has an upper bound of 'start'. The final +Inf bucket is not counted
60+
// and not included in the returned slice. The returned slice is meant to be
61+
// used for the Buckets field of HistogramOpts.
6062
//
61-
// The function panics if 'count' is less than 2.
63+
// The function panics if 'count' is zero or negative.
6264
func LinearBuckets(start, width float64, count int) []float64 {
63-
if count < 2 {
64-
panic("LinearBuckets needs a count > 1")
65+
if count < 1 {
66+
panic("LinearBuckets needs a positive count")
6567
}
66-
buckets := make([]float64, count-1)
68+
buckets := make([]float64, count)
6769
for i := range buckets {
6870
buckets[i] = start
6971
start += width
@@ -73,23 +75,23 @@ func LinearBuckets(start, width float64, count int) []float64 {
7375

7476
// ExponentialBuckets creates 'count' buckets, where the lowest bucket has an
7577
// upper bound of 'start' and each following bucket's upper bound is 'factor'
76-
// times the previous bucket's upper bound. The final +Inf bucket is counted
77-
// towards the total 'count', but not included in the returned slice. The
78-
// returned slice is meant to be used for the Buckets field of HistogramOpts.
78+
// times the previous bucket's upper bound. The final +Inf bucket is not counted
79+
// and not included in the returned slice. The returned slice is meant to be
80+
// used for the Buckets field of HistogramOpts.
7981
//
80-
// The function panics if 'count' is less than 2, if 'start' is 0 or negative,
82+
// The function panics if 'count' is 0 or negative, if 'start' is 0 or negative,
8183
// or if 'factor' is less than or equal 1.
8284
func ExponentialBuckets(start, factor float64, count int) []float64 {
83-
if count < 2 {
84-
panic("ExponentialBuckets needs a count > 1")
85+
if count < 1 {
86+
panic("ExponentialBuckets needs a positive count")
8587
}
8688
if start <= 0 {
8789
panic("ExponentialBuckets needs a positive start value")
8890
}
8991
if factor <= 1 {
9092
panic("ExponentialBuckets needs a factor greater than 1")
9193
}
92-
buckets := make([]float64, count-1)
94+
buckets := make([]float64, count)
9395
for i := range buckets {
9496
buckets[i] = start
9597
start *= factor

prometheus/histogram_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@ func getCumulativeCounts(vars []float64) []uint64 {
304304
}
305305

306306
func TestBuckets(t *testing.T) {
307-
got := LinearBuckets(-15, 5, 7)
307+
got := LinearBuckets(-15, 5, 6)
308308
want := []float64{-15, -10, -5, 0, 5, 10}
309309
if !reflect.DeepEqual(got, want) {
310310
t.Errorf("linear buckets: got %v, want %v", got, want)
311311
}
312312

313-
got = ExponentialBuckets(100, 1.2, 4)
313+
got = ExponentialBuckets(100, 1.2, 3)
314314
want = []float64{100, 120, 144}
315315
if !reflect.DeepEqual(got, want) {
316316
t.Errorf("linear buckets: got %v, want %v", got, want)

0 commit comments

Comments
 (0)