2020-06-02 18:40:54 +00:00
|
|
|
package metricsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var bucketBoundaries = []struct {
|
|
|
|
Value time.Duration
|
|
|
|
Label string
|
|
|
|
}{
|
|
|
|
{1 * time.Minute, "1m"},
|
|
|
|
{10 * time.Minute, "10m"},
|
|
|
|
{20 * time.Minute, "20m"},
|
|
|
|
{1 * time.Hour, "1h"},
|
|
|
|
{2 * time.Hour, "2h"},
|
|
|
|
{24 * time.Hour, "1d"},
|
|
|
|
{2 * 24 * time.Hour, "2d"},
|
|
|
|
{7 * 24 * time.Hour, "7d"},
|
|
|
|
{30 * 24 * time.Hour, "30d"},
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:36:24 +00:00
|
|
|
const OverflowBucket = "+Inf"
|
2020-06-02 18:40:54 +00:00
|
|
|
|
|
|
|
// TTLBucket computes the label to apply for a token TTL.
|
|
|
|
func TTLBucket(ttl time.Duration) string {
|
|
|
|
upperBound := sort.Search(
|
|
|
|
len(bucketBoundaries),
|
|
|
|
func(i int) bool {
|
|
|
|
return ttl <= bucketBoundaries[i].Value
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if upperBound >= len(bucketBoundaries) {
|
2020-06-23 23:36:24 +00:00
|
|
|
return OverflowBucket
|
2020-06-02 18:40:54 +00:00
|
|
|
} else {
|
|
|
|
return bucketBoundaries[upperBound].Label
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 18:26:58 +00:00
|
|
|
|
|
|
|
func ExpiryBucket(expiryTime time.Time, leaseEpsilon time.Duration, rollingWindow time.Time, labelNS string, useNS bool) *LeaseExpiryLabel {
|
|
|
|
if !useNS {
|
|
|
|
labelNS = ""
|
|
|
|
}
|
|
|
|
leaseExpiryLabel := LeaseExpiryLabel{LabelNS: labelNS}
|
|
|
|
|
|
|
|
// calculate rolling window
|
|
|
|
if expiryTime.Before(rollingWindow) {
|
|
|
|
leaseExpiryLabel.LabelName = expiryTime.Round(leaseEpsilon).String()
|
|
|
|
return &leaseExpiryLabel
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type LeaseExpiryLabel = struct {
|
|
|
|
LabelName string
|
|
|
|
LabelNS string
|
|
|
|
}
|