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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tally
import (
"errors"
"fmt"
"math"
"sort"
"time"
)
var (
// DefaultBuckets can be passed to specify to default buckets.
DefaultBuckets Buckets
errBucketsCountNeedsGreaterThanZero = errors.New("n needs to be > 0")
errBucketsStartNeedsGreaterThanZero = errors.New("start needs to be > 0")
errBucketsFactorNeedsGreaterThanOne = errors.New("factor needs to be > 1")
_singleBucket = bucketPair{
lowerBoundDuration: time.Duration(math.MinInt64),
upperBoundDuration: time.Duration(math.MaxInt64),
lowerBoundValue: -math.MaxFloat64,
upperBoundValue: math.MaxFloat64,
}
)
// ValueBuckets is a set of float64 values that implements Buckets.
type ValueBuckets []float64
// Implements sort.Interface
func (v ValueBuckets) Len() int {
return len(v)
}
// Implements sort.Interface
func (v ValueBuckets) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
// Implements sort.Interface
func (v ValueBuckets) Less(i, j int) bool {
return v[i] < v[j]
}
func (v ValueBuckets) String() string {
values := make([]string, len(v))
for i := range values {
values[i] = fmt.Sprintf("%f", v[i])
}
return fmt.Sprint(values)
}
// AsValues implements Buckets.
func (v ValueBuckets) AsValues() []float64 {
return v
}
// AsDurations implements Buckets and returns time.Duration
// representations of the float64 values divided by time.Second.
func (v ValueBuckets) AsDurations() []time.Duration {
values := make([]time.Duration, len(v))
for i := range values {
values[i] = time.Duration(v[i] * float64(time.Second))
}
return values
}
// DurationBuckets is a set of time.Duration values that implements Buckets.
type DurationBuckets []time.Duration
// Implements sort.Interface
func (v DurationBuckets) Len() int {
return len(v)
}
// Implements sort.Interface
func (v DurationBuckets) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
// Implements sort.Interface
func (v DurationBuckets) Less(i, j int) bool {
return v[i] < v[j]
}
func (v DurationBuckets) String() string {
values := make([]string, len(v))
for i := range values {
values[i] = v[i].String()
}
return fmt.Sprintf("%v", values)
}
// AsValues implements Buckets and returns float64
// representations of the time.Duration values divided by time.Second.
func (v DurationBuckets) AsValues() []float64 {
values := make([]float64, len(v))
for i := range values {
values[i] = float64(v[i]) / float64(time.Second)
}
return values
}
// AsDurations implements Buckets.
func (v DurationBuckets) AsDurations() []time.Duration {
return v
}
func bucketsEqual(x Buckets, y Buckets) bool {
switch b1 := x.(type) {
case DurationBuckets:
b2, ok := y.(DurationBuckets)
if !ok {
return false
}
if len(b1) != len(b2) {
return false
}
for i := 0; i < len(b1); i++ {
if b1[i] != b2[i] {
return false
}
}
case ValueBuckets:
b2, ok := y.(ValueBuckets)
if !ok {
return false
}
if len(b1) != len(b2) {
return false
}
for i := 0; i < len(b1); i++ {
if b1[i] != b2[i] {
return false
}
}
}
return true
}
func newBucketPair(
htype histogramType,
durations []time.Duration,
values []float64,
upperBoundIndex int,
prev BucketPair,
) bucketPair {
var pair bucketPair
switch htype {
case durationHistogramType:
pair = bucketPair{
lowerBoundDuration: prev.UpperBoundDuration(),
upperBoundDuration: durations[upperBoundIndex],
}
case valueHistogramType:
pair = bucketPair{
lowerBoundValue: prev.UpperBoundValue(),
upperBoundValue: values[upperBoundIndex],
}
default:
// nop
}
return pair
}
// BucketPairs creates a set of bucket pairs from a set
// of buckets describing the lower and upper bounds for
// each derived bucket.
func BucketPairs(buckets Buckets) []BucketPair {
htype := valueHistogramType
if _, ok := buckets.(DurationBuckets); ok {
htype = durationHistogramType
}
if buckets == nil || buckets.Len() < 1 {
return []BucketPair{_singleBucket}
}
var (
values []float64
durations []time.Duration
pairs = make([]BucketPair, 0, buckets.Len()+2)
pair bucketPair
)
switch htype {
case durationHistogramType:
durations = copyAndSortDurations(buckets.AsDurations())
pair.lowerBoundDuration = _singleBucket.lowerBoundDuration
pair.upperBoundDuration = durations[0]
case valueHistogramType:
values = copyAndSortValues(buckets.AsValues())
pair.lowerBoundValue = _singleBucket.lowerBoundValue
pair.upperBoundValue = values[0]
default:
// n.b. This branch will never be executed because htype is only ever
// one of two values.
panic("unsupported histogram type")
}
pairs = append(pairs, pair)
for i := 1; i < buckets.Len(); i++ {
pairs = append(
pairs,
newBucketPair(htype, durations, values, i, pairs[i-1]),
)
}
switch htype {
case durationHistogramType:
pair.lowerBoundDuration = pairs[len(pairs)-1].UpperBoundDuration()
pair.upperBoundDuration = _singleBucket.upperBoundDuration
case valueHistogramType:
pair.lowerBoundValue = pairs[len(pairs)-1].UpperBoundValue()
pair.upperBoundValue = _singleBucket.upperBoundValue
}
pairs = append(pairs, pair)
return pairs
}
func copyAndSortValues(values []float64) []float64 {
valuesCopy := make([]float64, len(values))
copy(valuesCopy, values)
sort.Sort(ValueBuckets(valuesCopy))
return valuesCopy
}
func copyAndSortDurations(durations []time.Duration) []time.Duration {
durationsCopy := make([]time.Duration, len(durations))
copy(durationsCopy, durations)
sort.Sort(DurationBuckets(durationsCopy))
return durationsCopy
}
type bucketPair struct {
lowerBoundValue float64
upperBoundValue float64
lowerBoundDuration time.Duration
upperBoundDuration time.Duration
}
func (p bucketPair) LowerBoundValue() float64 {
return p.lowerBoundValue
}
func (p bucketPair) UpperBoundValue() float64 {
return p.upperBoundValue
}
func (p bucketPair) LowerBoundDuration() time.Duration {
return p.lowerBoundDuration
}
func (p bucketPair) UpperBoundDuration() time.Duration {
return p.upperBoundDuration
}
// LinearValueBuckets creates a set of linear value buckets.
func LinearValueBuckets(start, width float64, n int) (ValueBuckets, error) {
if n <= 0 {
return nil, errBucketsCountNeedsGreaterThanZero
}
buckets := make([]float64, n)
for i := range buckets {
buckets[i] = start + (float64(i) * width)
}
return buckets, nil
}
// MustMakeLinearValueBuckets creates a set of linear value buckets
// or panics.
func MustMakeLinearValueBuckets(start, width float64, n int) ValueBuckets {
buckets, err := LinearValueBuckets(start, width, n)
if err != nil {
panic(err)
}
return buckets
}
// LinearDurationBuckets creates a set of linear duration buckets.
func LinearDurationBuckets(start, width time.Duration, n int) (DurationBuckets, error) {
if n <= 0 {
return nil, errBucketsCountNeedsGreaterThanZero
}
buckets := make([]time.Duration, n)
for i := range buckets {
buckets[i] = start + (time.Duration(i) * width)
}
return buckets, nil
}
// MustMakeLinearDurationBuckets creates a set of linear duration buckets.
// or panics.
func MustMakeLinearDurationBuckets(start, width time.Duration, n int) DurationBuckets {
buckets, err := LinearDurationBuckets(start, width, n)
if err != nil {
panic(err)
}
return buckets
}
// ExponentialValueBuckets creates a set of exponential value buckets.
func ExponentialValueBuckets(start, factor float64, n int) (ValueBuckets, error) {
if n <= 0 {
return nil, errBucketsCountNeedsGreaterThanZero
}
if start <= 0 {
return nil, errBucketsStartNeedsGreaterThanZero
}
if factor <= 1 {
return nil, errBucketsFactorNeedsGreaterThanOne
}
buckets := make([]float64, n)
curr := start
for i := range buckets {
buckets[i] = curr
curr *= factor
}
return buckets, nil
}
// MustMakeExponentialValueBuckets creates a set of exponential value buckets
// or panics.
func MustMakeExponentialValueBuckets(start, factor float64, n int) ValueBuckets {
buckets, err := ExponentialValueBuckets(start, factor, n)
if err != nil {
panic(err)
}
return buckets
}
// ExponentialDurationBuckets creates a set of exponential duration buckets.
func ExponentialDurationBuckets(start time.Duration, factor float64, n int) (DurationBuckets, error) {
if n <= 0 {
return nil, errBucketsCountNeedsGreaterThanZero
}
if start <= 0 {
return nil, errBucketsStartNeedsGreaterThanZero
}
if factor <= 1 {
return nil, errBucketsFactorNeedsGreaterThanOne
}
buckets := make([]time.Duration, n)
curr := start
for i := range buckets {
buckets[i] = curr
curr = time.Duration(float64(curr) * factor)
}
return buckets, nil
}
// MustMakeExponentialDurationBuckets creates a set of exponential value buckets
// or panics.
func MustMakeExponentialDurationBuckets(start time.Duration, factor float64, n int) DurationBuckets {
buckets, err := ExponentialDurationBuckets(start, factor, n)
if err != nil {
panic(err)
}
return buckets
}
|