@@ -22,6 +22,7 @@ import (
22
22
23
23
"github.com/prometheus/client_golang/prometheus"
24
24
25
+ vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
25
26
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics"
26
27
)
27
28
@@ -35,45 +36,52 @@ type SizeBasedGauge struct {
35
36
gauge * prometheus.GaugeVec
36
37
}
37
38
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
+
38
46
var (
39
47
controlledCount = prometheus .NewGaugeVec (
40
48
prometheus.GaugeOpts {
41
49
Namespace : metricsNamespace ,
42
50
Name : "controlled_pods_total" ,
43
51
Help : "Number of Pods controlled by VPA updater." ,
44
- }, []string {"vpa_size_log2" },
52
+ }, []string {"vpa_size_log2" , "update_mode" },
45
53
)
46
54
47
55
evictableCount = prometheus .NewGaugeVec (
48
56
prometheus.GaugeOpts {
49
57
Namespace : metricsNamespace ,
50
58
Name : "evictable_pods_total" ,
51
59
Help : "Number of Pods matching evicition criteria." ,
52
- }, []string {"vpa_size_log2" },
60
+ }, []string {"vpa_size_log2" , "update_mode" },
53
61
)
54
62
55
63
evictedCount = prometheus .NewCounterVec (
56
64
prometheus.CounterOpts {
57
65
Namespace : metricsNamespace ,
58
66
Name : "evicted_pods_total" ,
59
67
Help : "Number of Pods evicted by Updater to apply a new recommendation." ,
60
- }, []string {"vpa_size_log2" },
68
+ }, []string {"vpa_size_log2" , "update_mode" },
61
69
)
62
70
63
71
vpasWithEvictablePodsCount = prometheus .NewGaugeVec (
64
72
prometheus.GaugeOpts {
65
73
Namespace : metricsNamespace ,
66
74
Name : "vpas_with_evictable_pods_total" ,
67
75
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" },
69
77
)
70
78
71
79
vpasWithEvictedPodsCount = prometheus .NewGaugeVec (
72
80
prometheus.GaugeOpts {
73
81
Namespace : metricsNamespace ,
74
82
Name : "vpas_with_evicted_pods_total" ,
75
83
Help : "Number of VPA objects with at least one evicted Pod." ,
76
- }, []string {"vpa_size_log2" },
84
+ }, []string {"vpa_size_log2" , "update_mode" },
77
85
)
78
86
79
87
inPlaceUpdatableCount = prometheus .NewGaugeVec (
@@ -138,30 +146,41 @@ func newSizeBasedGauge(gauge *prometheus.GaugeVec) *SizeBasedGauge {
138
146
}
139
147
}
140
148
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
+
141
160
// 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 )
144
163
}
145
164
146
165
// 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 )
149
168
}
150
169
151
170
// 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 )
154
173
}
155
174
156
175
// 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 )
159
178
}
160
179
161
180
// 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 ) {
163
182
log2 := metrics .GetVpaSizeLog2 (vpaSize )
164
- evictedCount .WithLabelValues (strconv .Itoa (log2 )).Inc ()
183
+ evictedCount .WithLabelValues (strconv .Itoa (log2 ), string ( mode ) ).Inc ()
165
184
}
166
185
167
186
// NewInPlaceUpdatablePodsCounter returns a wrapper for counting Pods which are matching in-place update criteria
@@ -203,3 +222,19 @@ func (g *SizeBasedGauge) Observe() {
203
222
g .gauge .WithLabelValues (strconv .Itoa (log2 )).Set (float64 (value ))
204
223
}
205
224
}
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