Add additional init checks to ExponentialBucketer.

PiperOrigin-RevId: 441529868
This commit is contained in:
Konstantin Bogomolov
2022-04-13 11:15:53 -07:00
committed by gVisor bot
parent edbd203027
commit 717f78b014
2 changed files with 9 additions and 0 deletions
+6
View File
@@ -551,6 +551,9 @@ func NewExponentialBucketer(numFiniteBuckets int, width uint64, scale, growth fl
if numFiniteBuckets < exponentialMinBuckets || numFiniteBuckets > exponentialMaxBuckets {
panic(fmt.Sprintf("number of finite buckets must be in [%d, %d]", exponentialMinBuckets, exponentialMaxBuckets))
}
if scale < 0 || growth < 0 {
panic(fmt.Sprintf("scale and growth for exponential buckets must be >0, got scale=%f and growth=%f", scale, growth))
}
b := &ExponentialBucketer{
numFiniteBuckets: numFiniteBuckets,
width: float64(width),
@@ -562,6 +565,9 @@ func NewExponentialBucketer(numFiniteBuckets int, width uint64, scale, growth fl
b.lowerBounds[0] = 0
for i := 1; i <= numFiniteBuckets; i++ {
b.lowerBounds[i] = int64(b.width*float64(i) + b.scale*math.Pow(b.growth, float64(i-1)))
if b.lowerBounds[i] < 0 {
panic(fmt.Sprintf("encountered bucket width overflow at bucket %d", i))
}
}
b.maxSample = b.lowerBounds[numFiniteBuckets] - 1
return b
+3
View File
@@ -830,6 +830,9 @@ func TestBucketerPanics(t *testing.T) {
"NewDurationBucketer @ 2": func() {
NewDurationBucketer(2, time.Second, time.Minute)
},
"NewDurationBucketer @ 80": func() {
NewDurationBucketer(80, time.Microsecond, 50*time.Microsecond)
},
} {
t.Run(name, func(t *testing.T) {
var recovered interface{}