aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/buckets.go
blob: 063c0c4418aba4aee14ef86ac121180f9369a268 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package metrics

import (
	"sort"
	"time"
)

var (
	_ DurationBuckets = (*durationBuckets)(nil)
	_ Buckets         = (*buckets)(nil)
)

const (
	errBucketsCountNeedsGreaterThanZero = "n needs to be > 0"
	errBucketsStartNeedsGreaterThanZero = "start needs to be > 0"
	errBucketsFactorNeedsGreaterThanOne = "factor needs to be > 1"
)

type durationBuckets struct {
	buckets []time.Duration
}

// NewDurationBuckets returns new DurationBuckets implementation.
func NewDurationBuckets(bk ...time.Duration) DurationBuckets {
	sort.Slice(bk, func(i, j int) bool {
		return bk[i] < bk[j]
	})
	return durationBuckets{buckets: bk}
}

func (d durationBuckets) Size() int {
	return len(d.buckets)
}

func (d durationBuckets) MapDuration(dv time.Duration) (idx int) {
	for _, bound := range d.buckets {
		if dv < bound {
			break
		}
		idx++
	}
	return
}

func (d durationBuckets) UpperBound(idx int) time.Duration {
	if idx > d.Size()-1 {
		panic("idx is out of bounds")
	}
	return d.buckets[idx]
}

type buckets struct {
	buckets []float64
}

// NewBuckets returns new Buckets implementation.
func NewBuckets(bk ...float64) Buckets {
	sort.Slice(bk, func(i, j int) bool {
		return bk[i] < bk[j]
	})
	return buckets{buckets: bk}
}

func (d buckets) Size() int {
	return len(d.buckets)
}

func (d buckets) MapValue(v float64) (idx int) {
	for _, bound := range d.buckets {
		if v < bound {
			break
		}
		idx++
	}
	return
}

func (d buckets) UpperBound(idx int) float64 {
	if idx > d.Size()-1 {
		panic("idx is out of bounds")
	}
	return d.buckets[idx]
}

// MakeLinearBuckets creates a set of linear value buckets.
func MakeLinearBuckets(start, width float64, n int) Buckets {
	if n <= 0 {
		panic(errBucketsCountNeedsGreaterThanZero)
	}
	bounds := make([]float64, n)
	for i := range bounds {
		bounds[i] = start + (float64(i) * width)
	}
	return NewBuckets(bounds...)
}

// MakeLinearDurationBuckets creates a set of linear duration buckets.
func MakeLinearDurationBuckets(start, width time.Duration, n int) DurationBuckets {
	if n <= 0 {
		panic(errBucketsCountNeedsGreaterThanZero)
	}
	buckets := make([]time.Duration, n)
	for i := range buckets {
		buckets[i] = start + (time.Duration(i) * width)
	}
	return NewDurationBuckets(buckets...)
}

// MakeExponentialBuckets creates a set of exponential value buckets.
func MakeExponentialBuckets(start, factor float64, n int) Buckets {
	if n <= 0 {
		panic(errBucketsCountNeedsGreaterThanZero)
	}
	if start <= 0 {
		panic(errBucketsStartNeedsGreaterThanZero)
	}
	if factor <= 1 {
		panic(errBucketsFactorNeedsGreaterThanOne)
	}
	buckets := make([]float64, n)
	curr := start
	for i := range buckets {
		buckets[i] = curr
		curr *= factor
	}
	return NewBuckets(buckets...)
}

// MakeExponentialDurationBuckets creates a set of exponential duration buckets.
func MakeExponentialDurationBuckets(start time.Duration, factor float64, n int) DurationBuckets {
	if n <= 0 {
		panic(errBucketsCountNeedsGreaterThanZero)
	}
	if start <= 0 {
		panic(errBucketsStartNeedsGreaterThanZero)
	}
	if factor <= 1 {
		panic(errBucketsFactorNeedsGreaterThanOne)
	}
	buckets := make([]time.Duration, n)
	curr := start
	for i := range buckets {
		buckets[i] = curr
		curr = time.Duration(float64(curr) * factor)
	}
	return NewDurationBuckets(buckets...)
}