@@ -18,6 +18,8 @@ limitations under the License.
1818package updater
1919
2020import (
21+ "strconv"
22+
2123 "github.com/prometheus/client_golang/prometheus"
2224 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics"
2325)
@@ -26,13 +28,51 @@ const (
2628 metricsNamespace = metrics .TopMetricsNamespace + "updater"
2729)
2830
31+ // SizeBasedGauge is a wrapper for incrementally recording values indexed by log2(VPA size)
32+ type SizeBasedGauge struct {
33+ values [metrics .MaxVpaSizeLog ]int
34+ gauge * prometheus.GaugeVec
35+ }
36+
2937var (
30- evictedCount = prometheus .NewCounter (
38+ controlledCount = prometheus .NewGaugeVec (
39+ prometheus.GaugeOpts {
40+ Namespace : metricsNamespace ,
41+ Name : "controlled_pods_total" ,
42+ Help : "Number of Pods controlled by VPA updater." ,
43+ }, []string {"vpa_size_log2" },
44+ )
45+
46+ evictableCount = prometheus .NewGaugeVec (
47+ prometheus.GaugeOpts {
48+ Namespace : metricsNamespace ,
49+ Name : "evictable_pods_total" ,
50+ Help : "Number of Pods matching evicition criteria." ,
51+ }, []string {"vpa_size_log2" },
52+ )
53+
54+ evictedCount = prometheus .NewCounterVec (
3155 prometheus.CounterOpts {
3256 Namespace : metricsNamespace ,
3357 Name : "evicted_pods_total" ,
3458 Help : "Number of Pods evicted by Updater to apply a new recommendation." ,
35- },
59+ }, []string {"vpa_size_log2" },
60+ )
61+
62+ vpasWithEvictablePodsCount = prometheus .NewGaugeVec (
63+ prometheus.GaugeOpts {
64+ Namespace : metricsNamespace ,
65+ Name : "vpas_with_evictable_pods_total" ,
66+ Help : "Number of VPA objects with at least one Pod matching evicition criteria." ,
67+ }, []string {"vpa_size_log2" },
68+ )
69+
70+ vpasWithEvictedPodsCount = prometheus .NewGaugeVec (
71+ prometheus.GaugeOpts {
72+ Namespace : metricsNamespace ,
73+ Name : "vpas_with_evicted_pods_total" ,
74+ Help : "Number of VPA objects with at least one evicted Pod." ,
75+ }, []string {"vpa_size_log2" },
3676 )
3777
3878 functionLatency = metrics .CreateExecutionTimeMetric (metricsNamespace ,
@@ -41,16 +81,57 @@ var (
4181
4282// Register initializes all metrics for VPA Updater
4383func Register () {
44- prometheus .MustRegister (evictedCount )
45- prometheus .MustRegister (functionLatency )
84+ prometheus .MustRegister (controlledCount , evictableCount , evictedCount , vpasWithEvictablePodsCount , vpasWithEvictedPodsCount , functionLatency )
4685}
4786
4887// NewExecutionTimer provides a timer for Updater's RunOnce execution
4988func NewExecutionTimer () * metrics.ExecutionTimer {
5089 return metrics .NewExecutionTimer (functionLatency )
5190}
5291
53- // AddEvictedPod increases the counter of pods evicted by VPA
54- func AddEvictedPod () {
55- evictedCount .Add (1 )
92+ // newSizeBasedGauge provides a wrapper for counting items in a loop
93+ func newSizeBasedGauge (gauge * prometheus.GaugeVec ) * SizeBasedGauge {
94+ return & SizeBasedGauge {
95+ values : [metrics .MaxVpaSizeLog ]int {},
96+ gauge : gauge ,
97+ }
98+ }
99+
100+ // NewControlledPodsCounter returns a wrapper for counting Pods controlled by Updater
101+ func NewControlledPodsCounter () * SizeBasedGauge {
102+ return newSizeBasedGauge (controlledCount )
103+ }
104+
105+ // NewEvictablePodsCounter returns a wrapper for counting Pods which are matching eviction criteria
106+ func NewEvictablePodsCounter () * SizeBasedGauge {
107+ return newSizeBasedGauge (evictableCount )
108+ }
109+
110+ // NewVpasWithEvictablePodsCounter returns a wrapper for counting VPA objects with Pods matching eviction criteria
111+ func NewVpasWithEvictablePodsCounter () * SizeBasedGauge {
112+ return newSizeBasedGauge (vpasWithEvictablePodsCount )
113+ }
114+
115+ // NewVpasWithEvictedPodsCounter returns a wrapper for counting VPA objects with evicted Pods
116+ func NewVpasWithEvictedPodsCounter () * SizeBasedGauge {
117+ return newSizeBasedGauge (vpasWithEvictedPodsCount )
118+ }
119+
120+ // AddEvictedPod increases the counter of pods evicted by Updater, by given VPA size
121+ func AddEvictedPod (vpaSize int ) {
122+ log2 := metrics .GetVpaSizeLog2 (vpaSize )
123+ evictedCount .WithLabelValues (strconv .Itoa (log2 )).Inc ()
124+ }
125+
126+ // Add increases the counter for the given VPA size
127+ func (g * SizeBasedGauge ) Add (vpaSize int , value int ) {
128+ log2 := metrics .GetVpaSizeLog2 (vpaSize )
129+ g .values [log2 ] += value
130+ }
131+
132+ // Observe stores the recorded values into metrics object associated with the wrapper
133+ func (g * SizeBasedGauge ) Observe () {
134+ for log2 , value := range g .values {
135+ g .gauge .WithLabelValues (strconv .Itoa (log2 )).Set (float64 (value ))
136+ }
56137}
0 commit comments