@@ -33,44 +33,47 @@ func LinearBucketRange(start, end int64, buckets int) []float64 {
3333 return bs
3434}
3535
36- const precision = float64 (time .Millisecond )
36+ const (
37+ precision = float64 (time .Millisecond )
38+ sigDigits = 6 // Number of significant digits for rounding bucket values
39+ )
3740
3841// ExponentialBucketTimeRange creates a bucket set for a histogram
3942// that has exponentially increasing intervals between the values, e.g. 0, 0.5, 1, 2, 4, 8, ...
4043// Fixed to guarantee exactly 'buckets' number of buckets and produce clean floating-point values.
4144func ExponentialBucketTimeRange (start , end time.Duration , buckets int ) []float64 {
4245 if buckets <= 1 {
43- return []float64 {roundToSignificantDigits (start .Seconds (), 6 )}
46+ return []float64 {roundToSignificantDigits (start .Seconds ())}
4447 }
4548
4649 interval := end - start
4750 if interval <= 0 {
48- return []float64 {roundToSignificantDigits (start .Seconds (), 6 )}
51+ return []float64 {roundToSignificantDigits (start .Seconds ())}
4952 }
5053
5154 // Calculate factor more safely using Pow instead of Exp(Log(...))
5255 // This ensures we generate exactly 'buckets' number of buckets
5356 factor := math .Pow (float64 (interval )/ precision , 1.0 / float64 (buckets - 1 ))
5457
5558 bs := make ([]float64 , 0 , buckets )
56- bs = append (bs , roundToSignificantDigits (start .Seconds (), 6 ))
59+ bs = append (bs , roundToSignificantDigits (start .Seconds ()))
5760
5861 // Generate exactly buckets-1 additional buckets
5962 for i := 1 ; i < buckets ; i ++ {
6063 v := time .Duration (math .Pow (factor , float64 (i )) * precision )
6164 if v > interval {
6265 v = interval
6366 }
64- // Round to 6 significant digits to avoid ugly floating-point representations
65- bs = append (bs , roundToSignificantDigits ((start + v ).Seconds (), 6 ))
67+ // Round to sigDigits significant digits to avoid ugly floating-point representations
68+ bs = append (bs , roundToSignificantDigits ((start + v ).Seconds ()))
6669 }
6770
6871 return bs
6972}
7073
71- // roundToSignificantDigits rounds a float64 to n significant digits
74+ // roundToSignificantDigits rounds a float64 to sigDigits significant digits
7275// This produces cleaner values for Prometheus metrics (e.g., 0.001 instead of 0.0009999999999999999)
73- func roundToSignificantDigits (value float64 , sigDigits int ) float64 {
76+ func roundToSignificantDigits (value float64 ) float64 {
7477 if value == 0 {
7578 return 0
7679 }
0 commit comments