Skip to content

Commit c7a5142

Browse files
authored
Merge pull request #8395 from laoj2/add-label-to-vpa-metric
Add update_mode label to VPA updater metrics
2 parents c1352da + 36804f1 commit c7a5142

File tree

4 files changed

+354
-21
lines changed

4 files changed

+354
-21
lines changed

vertical-pod-autoscaler/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4242
github.com/josharian/intern v1.0.0 // indirect
4343
github.com/json-iterator/go v1.1.12 // indirect
44+
github.com/kylelemons/godebug v1.1.0 // indirect
4445
github.com/mailru/easyjson v0.9.0 // indirect
4546
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4647
github.com/modern-go/reflect2 v1.0.2 // indirect

vertical-pod-autoscaler/pkg/updater/logic/updater.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ func (u *updater) RunOnce(ctx context.Context) {
230230
// to contain only Pods controlled by a VPA in auto, recreate, or inPlaceOrRecreate mode
231231
for vpa, livePods := range controlledPods {
232232
vpaSize := len(livePods)
233-
controlledPodsCounter.Add(vpaSize, vpaSize)
233+
updateMode := vpa_api_util.GetUpdateMode(vpa)
234+
controlledPodsCounter.Add(vpaSize, updateMode, vpaSize)
234235
creatorToSingleGroupStatsMap, podToReplicaCreatorMap, err := u.restrictionFactory.GetCreatorMaps(livePods, vpa)
235236
if err != nil {
236237
klog.ErrorS(err, "Failed to get creator maps")
@@ -242,7 +243,6 @@ func (u *updater) RunOnce(ctx context.Context) {
242243

243244
podsForInPlace := make([]*apiv1.Pod, 0)
244245
podsForEviction := make([]*apiv1.Pod, 0)
245-
updateMode := vpa_api_util.GetUpdateMode(vpa)
246246

247247
if updateMode == vpa_types.UpdateModeInPlaceOrRecreate && features.Enabled(features.InPlaceOrRecreate) {
248248
podsForInPlace = u.getPodsUpdateOrder(filterNonInPlaceUpdatablePods(livePods, inPlaceLimiter), vpa)
@@ -253,7 +253,7 @@ func (u *updater) RunOnce(ctx context.Context) {
253253
klog.InfoS("Warning: feature gate is not enabled for this updateMode", "featuregate", features.InPlaceOrRecreate, "updateMode", vpa_types.UpdateModeInPlaceOrRecreate)
254254
}
255255
podsForEviction = u.getPodsUpdateOrder(filterNonEvictablePods(livePods, evictionLimiter), vpa)
256-
evictablePodsCounter.Add(vpaSize, len(podsForEviction))
256+
evictablePodsCounter.Add(vpaSize, updateMode, len(podsForEviction))
257257
}
258258

259259
withInPlaceUpdatable := false
@@ -304,7 +304,7 @@ func (u *updater) RunOnce(ctx context.Context) {
304304
klog.V(0).InfoS("Eviction failed", "error", evictErr, "pod", klog.KObj(pod))
305305
} else {
306306
withEvicted = true
307-
metrics_updater.AddEvictedPod(vpaSize)
307+
metrics_updater.AddEvictedPod(vpaSize, updateMode)
308308
}
309309
}
310310

@@ -315,10 +315,10 @@ func (u *updater) RunOnce(ctx context.Context) {
315315
vpasWithInPlaceUpdatedPodsCounter.Add(vpaSize, 1)
316316
}
317317
if withEvictable {
318-
vpasWithEvictablePodsCounter.Add(vpaSize, 1)
318+
vpasWithEvictablePodsCounter.Add(vpaSize, updateMode, 1)
319319
}
320320
if withEvicted {
321-
vpasWithEvictedPodsCounter.Add(vpaSize, 1)
321+
vpasWithEvictedPodsCounter.Add(vpaSize, updateMode, 1)
322322
}
323323
}
324324
timer.ObserveStep("EvictPods")

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/prometheus/client_golang/prometheus"
2424

25+
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
2526
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics"
2627
)
2728

@@ -35,45 +36,52 @@ type SizeBasedGauge struct {
3536
gauge *prometheus.GaugeVec
3637
}
3738

39+
// UpdateModeAndSizeBasedGauge is a wrapper for incrementally recording values
40+
// indexed by log2(VPA size) and update mode
41+
type UpdateModeAndSizeBasedGauge struct {
42+
values [metrics.MaxVpaSizeLog]map[vpa_types.UpdateMode]int
43+
gauge *prometheus.GaugeVec
44+
}
45+
3846
var (
3947
controlledCount = prometheus.NewGaugeVec(
4048
prometheus.GaugeOpts{
4149
Namespace: metricsNamespace,
4250
Name: "controlled_pods_total",
4351
Help: "Number of Pods controlled by VPA updater.",
44-
}, []string{"vpa_size_log2"},
52+
}, []string{"vpa_size_log2", "update_mode"},
4553
)
4654

4755
evictableCount = prometheus.NewGaugeVec(
4856
prometheus.GaugeOpts{
4957
Namespace: metricsNamespace,
5058
Name: "evictable_pods_total",
5159
Help: "Number of Pods matching evicition criteria.",
52-
}, []string{"vpa_size_log2"},
60+
}, []string{"vpa_size_log2", "update_mode"},
5361
)
5462

5563
evictedCount = prometheus.NewCounterVec(
5664
prometheus.CounterOpts{
5765
Namespace: metricsNamespace,
5866
Name: "evicted_pods_total",
5967
Help: "Number of Pods evicted by Updater to apply a new recommendation.",
60-
}, []string{"vpa_size_log2"},
68+
}, []string{"vpa_size_log2", "update_mode"},
6169
)
6270

6371
vpasWithEvictablePodsCount = prometheus.NewGaugeVec(
6472
prometheus.GaugeOpts{
6573
Namespace: metricsNamespace,
6674
Name: "vpas_with_evictable_pods_total",
6775
Help: "Number of VPA objects with at least one Pod matching evicition criteria.",
68-
}, []string{"vpa_size_log2"},
76+
}, []string{"vpa_size_log2", "update_mode"},
6977
)
7078

7179
vpasWithEvictedPodsCount = prometheus.NewGaugeVec(
7280
prometheus.GaugeOpts{
7381
Namespace: metricsNamespace,
7482
Name: "vpas_with_evicted_pods_total",
7583
Help: "Number of VPA objects with at least one evicted Pod.",
76-
}, []string{"vpa_size_log2"},
84+
}, []string{"vpa_size_log2", "update_mode"},
7785
)
7886

7987
inPlaceUpdatableCount = prometheus.NewGaugeVec(
@@ -138,30 +146,41 @@ func newSizeBasedGauge(gauge *prometheus.GaugeVec) *SizeBasedGauge {
138146
}
139147
}
140148

149+
// newModeAndSizeBasedGauge provides a wrapper for counting items in a loop
150+
func newModeAndSizeBasedGauge(gauge *prometheus.GaugeVec) *UpdateModeAndSizeBasedGauge {
151+
g := &UpdateModeAndSizeBasedGauge{
152+
gauge: gauge,
153+
}
154+
for i := range g.values {
155+
g.values[i] = make(map[vpa_types.UpdateMode]int)
156+
}
157+
return g
158+
}
159+
141160
// NewControlledPodsCounter returns a wrapper for counting Pods controlled by Updater
142-
func NewControlledPodsCounter() *SizeBasedGauge {
143-
return newSizeBasedGauge(controlledCount)
161+
func NewControlledPodsCounter() *UpdateModeAndSizeBasedGauge {
162+
return newModeAndSizeBasedGauge(controlledCount)
144163
}
145164

146165
// NewEvictablePodsCounter returns a wrapper for counting Pods which are matching eviction criteria
147-
func NewEvictablePodsCounter() *SizeBasedGauge {
148-
return newSizeBasedGauge(evictableCount)
166+
func NewEvictablePodsCounter() *UpdateModeAndSizeBasedGauge {
167+
return newModeAndSizeBasedGauge(evictableCount)
149168
}
150169

151170
// NewVpasWithEvictablePodsCounter returns a wrapper for counting VPA objects with Pods matching eviction criteria
152-
func NewVpasWithEvictablePodsCounter() *SizeBasedGauge {
153-
return newSizeBasedGauge(vpasWithEvictablePodsCount)
171+
func NewVpasWithEvictablePodsCounter() *UpdateModeAndSizeBasedGauge {
172+
return newModeAndSizeBasedGauge(vpasWithEvictablePodsCount)
154173
}
155174

156175
// NewVpasWithEvictedPodsCounter returns a wrapper for counting VPA objects with evicted Pods
157-
func NewVpasWithEvictedPodsCounter() *SizeBasedGauge {
158-
return newSizeBasedGauge(vpasWithEvictedPodsCount)
176+
func NewVpasWithEvictedPodsCounter() *UpdateModeAndSizeBasedGauge {
177+
return newModeAndSizeBasedGauge(vpasWithEvictedPodsCount)
159178
}
160179

161180
// AddEvictedPod increases the counter of pods evicted by Updater, by given VPA size
162-
func AddEvictedPod(vpaSize int) {
181+
func AddEvictedPod(vpaSize int, mode vpa_types.UpdateMode) {
163182
log2 := metrics.GetVpaSizeLog2(vpaSize)
164-
evictedCount.WithLabelValues(strconv.Itoa(log2)).Inc()
183+
evictedCount.WithLabelValues(strconv.Itoa(log2), string(mode)).Inc()
165184
}
166185

167186
// NewInPlaceUpdatablePodsCounter returns a wrapper for counting Pods which are matching in-place update criteria
@@ -203,3 +222,19 @@ func (g *SizeBasedGauge) Observe() {
203222
g.gauge.WithLabelValues(strconv.Itoa(log2)).Set(float64(value))
204223
}
205224
}
225+
226+
// Add increases the counter for the given VPA size and VPA update mode.
227+
func (g *UpdateModeAndSizeBasedGauge) Add(vpaSize int, vpaUpdateMode vpa_types.UpdateMode, value int) {
228+
log2 := metrics.GetVpaSizeLog2(vpaSize)
229+
g.values[log2][vpaUpdateMode] += value
230+
}
231+
232+
// Observe stores the recorded values into metrics object associated with the
233+
// wrapper
234+
func (g *UpdateModeAndSizeBasedGauge) Observe() {
235+
for log2, valueMap := range g.values {
236+
for vpaMode, value := range valueMap {
237+
g.gauge.WithLabelValues(strconv.Itoa(log2), string(vpaMode)).Set(float64(value))
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)