@@ -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+
3846var (
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