Skip to content

Commit ff25eb5

Browse files
committed
Make getVpaSizeLog2 public and move it to top-level metrics package
1 parent 3c4e689 commit ff25eb5

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

vertical-pod-autoscaler/pkg/utils/metrics/metrics.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package metrics
1919

2020
import (
21+
"math"
2122
"net/http"
2223
"time"
2324

@@ -39,6 +40,10 @@ type ExecutionTimer struct {
3940
const (
4041
// TopMetricsNamespace is a prefix for all VPA-related metrics namespaces
4142
TopMetricsNamespace = "vpa_"
43+
44+
// The metrics will distinguish VPA sizes up to 2^MaxVpaSizeLog (~1M)
45+
// Anything above that size will be reported in the top bucket.
46+
MaxVpaSizeLog = 20
4247
)
4348

4449
// Initialize sets up Prometheus to expose metrics & (optionally) health-check on the given address
@@ -87,3 +92,17 @@ func CreateExecutionTimeMetric(namespace string, help string) *prometheus.Histog
8792
}, []string{"step"},
8893
)
8994
}
95+
96+
// GetVpaSizeLog2 returns a bucket number for a metric labelled with number of Pods under a given VPA.
97+
// It is basically log2(vpaSize), capped to MaxVpaSizeLog
98+
func GetVpaSizeLog2(vpaSize int) int {
99+
if vpaSize == 0 {
100+
return 0
101+
}
102+
103+
ret := int(math.Log2(float64(vpaSize)))
104+
if ret > MaxVpaSizeLog {
105+
return MaxVpaSizeLog
106+
}
107+
return ret
108+
}

vertical-pod-autoscaler/pkg/utils/metrics/updater/updater.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
package updater
1919

2020
import (
21-
"math"
2221
"strconv"
2322

2423
"github.com/prometheus/client_golang/prometheus"
@@ -27,15 +26,11 @@ import (
2726

2827
const (
2928
metricsNamespace = metrics.TopMetricsNamespace + "updater"
30-
31-
// The metrics will distinguish VPA sizes up to 2^maxVpaSizeLog (~1M)
32-
// Anything above that size will be reported in the top bucket.
33-
maxVpaSizeLog = 20
3429
)
3530

3631
// SizeBasedGauge is a wrapper for incrementally recording values indexed by log2(VPA size)
3732
type SizeBasedGauge struct {
38-
values [maxVpaSizeLog]int
33+
values [metrics.MaxVpaSizeLog]int
3934
gauge *prometheus.GaugeVec
4035
}
4136

@@ -97,7 +92,7 @@ func NewExecutionTimer() *metrics.ExecutionTimer {
9792
// newSizeBasedGauge provides a wrapper for counting items in a loop
9893
func newSizeBasedGauge(gauge *prometheus.GaugeVec) *SizeBasedGauge {
9994
return &SizeBasedGauge{
100-
values: [maxVpaSizeLog]int{},
95+
values: [metrics.MaxVpaSizeLog]int{},
10196
gauge: gauge,
10297
}
10398
}
@@ -122,27 +117,15 @@ func NewVpasWithEvictedPodsCounter() *SizeBasedGauge {
122117
return newSizeBasedGauge(vpasWithEvictedPodsCount)
123118
}
124119

125-
func getVpaSizeLog2(vpaSize int) int {
126-
if vpaSize == 0 {
127-
return 0
128-
}
129-
130-
ret := int(math.Log2(float64(vpaSize)))
131-
if ret > maxVpaSizeLog {
132-
return maxVpaSizeLog
133-
}
134-
return ret
135-
}
136-
137120
// AddEvictedPod increases the counter of pods evicted by Updater, by given VPA size
138121
func AddEvictedPod(vpaSize int) {
139-
log2 := getVpaSizeLog2(vpaSize)
122+
log2 := metrics.GetVpaSizeLog2(vpaSize)
140123
evictedCount.WithLabelValues(strconv.Itoa(log2)).Inc()
141124
}
142125

143126
// Add increases the counter for the given VPA size
144127
func (g *SizeBasedGauge) Add(vpaSize int, value int) {
145-
log2 := getVpaSizeLog2(vpaSize)
128+
log2 := metrics.GetVpaSizeLog2(vpaSize)
146129
g.values[log2] += value
147130
}
148131

0 commit comments

Comments
 (0)