@@ -19,106 +19,112 @@ package metrics
19
19
import (
20
20
"github.com/prometheus/client_golang/prometheus"
21
21
"k8s.io/client-go/util/workqueue"
22
- logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
23
22
)
24
23
25
- var log = logf .RuntimeLog .WithName ("metrics" )
26
-
27
24
// This file is copied and adapted from k8s.io/kubernetes/pkg/util/workqueue/prometheus
28
25
// which registers metrics to the default prometheus Registry. We require very
29
26
// similar functionality, but must register metrics to a different Registry.
30
27
28
+ // Metrics subsystem and all keys used by the workqueue.
29
+ const (
30
+ WorkQueueSubsystem = "workqueue"
31
+ DepthKey = "depth"
32
+ AddsKey = "adds_total"
33
+ QueueLatencyKey = "queue_duration_seconds"
34
+ WorkDurationKey = "work_duration_seconds"
35
+ UnfinishedWorkKey = "unfinished_work_seconds"
36
+ LongestRunningProcessorKey = "longest_running_processor_seconds"
37
+ RetriesKey = "retries_total"
38
+ )
39
+
40
+ var (
41
+ depth = prometheus .NewGaugeVec (prometheus.GaugeOpts {
42
+ Subsystem : WorkQueueSubsystem ,
43
+ Name : DepthKey ,
44
+ Help : "Current depth of workqueue" ,
45
+ }, []string {"name" })
46
+
47
+ adds = prometheus .NewCounterVec (prometheus.CounterOpts {
48
+ Subsystem : WorkQueueSubsystem ,
49
+ Name : AddsKey ,
50
+ Help : "Total number of adds handled by workqueue" ,
51
+ }, []string {"name" })
52
+
53
+ latency = prometheus .NewHistogramVec (prometheus.HistogramOpts {
54
+ Subsystem : WorkQueueSubsystem ,
55
+ Name : QueueLatencyKey ,
56
+ Help : "How long in seconds an item stays in workqueue before being requested" ,
57
+ Buckets : prometheus .ExponentialBuckets (10e-9 , 10 , 10 ),
58
+ }, []string {"name" })
59
+
60
+ workDuration = prometheus .NewHistogramVec (prometheus.HistogramOpts {
61
+ Subsystem : WorkQueueSubsystem ,
62
+ Name : WorkDurationKey ,
63
+ Help : "How long in seconds processing an item from workqueue takes." ,
64
+ Buckets : prometheus .ExponentialBuckets (10e-9 , 10 , 10 ),
65
+ }, []string {"name" })
66
+
67
+ unfinished = prometheus .NewGaugeVec (prometheus.GaugeOpts {
68
+ Subsystem : WorkQueueSubsystem ,
69
+ Name : UnfinishedWorkKey ,
70
+ Help : "How many seconds of work has been done that " +
71
+ "is in progress and hasn't been observed by work_duration. Large " +
72
+ "values indicate stuck threads. One can deduce the number of stuck " +
73
+ "threads by observing the rate at which this increases." ,
74
+ }, []string {"name" })
75
+
76
+ longestRunningProcessor = prometheus .NewGaugeVec (prometheus.GaugeOpts {
77
+ Subsystem : WorkQueueSubsystem ,
78
+ Name : LongestRunningProcessorKey ,
79
+ Help : "How many seconds has the longest running " +
80
+ "processor for workqueue been running." ,
81
+ }, []string {"name" })
82
+
83
+ retries = prometheus .NewCounterVec (prometheus.CounterOpts {
84
+ Subsystem : WorkQueueSubsystem ,
85
+ Name : RetriesKey ,
86
+ Help : "Total number of retries handled by workqueue" ,
87
+ }, []string {"name" })
88
+ )
89
+
31
90
func init () {
32
- workqueue .SetProvider (workqueueMetricsProvider {})
33
- }
91
+ Registry .MustRegister (depth )
92
+ Registry .MustRegister (adds )
93
+ Registry .MustRegister (latency )
94
+ Registry .MustRegister (workDuration )
95
+ Registry .MustRegister (unfinished )
96
+ Registry .MustRegister (longestRunningProcessor )
97
+ Registry .MustRegister (retries )
34
98
35
- func registerWorkqueueMetric (c prometheus.Collector , name , queue string ) {
36
- if err := Registry .Register (c ); err != nil {
37
- log .Error (err , "failed to register metric" , "name" , name , "queue" , queue )
38
- }
99
+ workqueue .SetProvider (workqueueMetricsProvider {})
39
100
}
40
101
41
102
type workqueueMetricsProvider struct {}
42
103
43
- func (workqueueMetricsProvider ) NewDepthMetric (queue string ) workqueue.GaugeMetric {
44
- const name = "workqueue_depth"
45
- m := prometheus .NewGauge (prometheus.GaugeOpts {
46
- Name : name ,
47
- Help : "Current depth of workqueue" ,
48
- ConstLabels : prometheus.Labels {"name" : queue },
49
- })
50
- registerWorkqueueMetric (m , name , queue )
51
- return m
104
+ func (workqueueMetricsProvider ) NewDepthMetric (name string ) workqueue.GaugeMetric {
105
+ return depth .WithLabelValues (name )
52
106
}
53
107
54
- func (workqueueMetricsProvider ) NewAddsMetric (queue string ) workqueue.CounterMetric {
55
- const name = "workqueue_adds_total"
56
- m := prometheus .NewCounter (prometheus.CounterOpts {
57
- Name : name ,
58
- Help : "Total number of adds handled by workqueue" ,
59
- ConstLabels : prometheus.Labels {"name" : queue },
60
- })
61
- registerWorkqueueMetric (m , name , queue )
62
- return m
108
+ func (workqueueMetricsProvider ) NewAddsMetric (name string ) workqueue.CounterMetric {
109
+ return adds .WithLabelValues (name )
63
110
}
64
111
65
- func (workqueueMetricsProvider ) NewLatencyMetric (queue string ) workqueue.HistogramMetric {
66
- const name = "workqueue_queue_duration_seconds"
67
- m := prometheus .NewHistogram (prometheus.HistogramOpts {
68
- Name : name ,
69
- Help : "How long in seconds an item stays in workqueue before being requested." ,
70
- ConstLabels : prometheus.Labels {"name" : queue },
71
- Buckets : prometheus .ExponentialBuckets (10e-9 , 10 , 10 ),
72
- })
73
- registerWorkqueueMetric (m , name , queue )
74
- return m
112
+ func (workqueueMetricsProvider ) NewLatencyMetric (name string ) workqueue.HistogramMetric {
113
+ return latency .WithLabelValues (name )
75
114
}
76
115
77
- func (workqueueMetricsProvider ) NewWorkDurationMetric (queue string ) workqueue.HistogramMetric {
78
- const name = "workqueue_work_duration_seconds"
79
- m := prometheus .NewHistogram (prometheus.HistogramOpts {
80
- Name : name ,
81
- Help : "How long in seconds processing an item from workqueue takes." ,
82
- ConstLabels : prometheus.Labels {"name" : queue },
83
- Buckets : prometheus .ExponentialBuckets (10e-9 , 10 , 10 ),
84
- })
85
- registerWorkqueueMetric (m , name , queue )
86
- return m
116
+ func (workqueueMetricsProvider ) NewWorkDurationMetric (name string ) workqueue.HistogramMetric {
117
+ return workDuration .WithLabelValues (name )
87
118
}
88
119
89
- func (workqueueMetricsProvider ) NewUnfinishedWorkSecondsMetric (queue string ) workqueue.SettableGaugeMetric {
90
- const name = "workqueue_unfinished_work_seconds"
91
- m := prometheus .NewGauge (prometheus.GaugeOpts {
92
- Name : name ,
93
- Help : "How many seconds of work has done that " +
94
- "is in progress and hasn't been observed by work_duration. Large " +
95
- "values indicate stuck threads. One can deduce the number of stuck " +
96
- "threads by observing the rate at which this increases." ,
97
- ConstLabels : prometheus.Labels {"name" : queue },
98
- })
99
- registerWorkqueueMetric (m , name , queue )
100
- return m
120
+ func (workqueueMetricsProvider ) NewUnfinishedWorkSecondsMetric (name string ) workqueue.SettableGaugeMetric {
121
+ return unfinished .WithLabelValues (name )
101
122
}
102
123
103
- func (workqueueMetricsProvider ) NewLongestRunningProcessorSecondsMetric (queue string ) workqueue.SettableGaugeMetric {
104
- const name = "workqueue_longest_running_processor_seconds"
105
- m := prometheus .NewGauge (prometheus.GaugeOpts {
106
- Name : name ,
107
- Help : "How many seconds has the longest running " +
108
- "processor for workqueue been running." ,
109
- ConstLabels : prometheus.Labels {"name" : queue },
110
- })
111
- registerWorkqueueMetric (m , name , queue )
112
- return m
124
+ func (workqueueMetricsProvider ) NewLongestRunningProcessorSecondsMetric (name string ) workqueue.SettableGaugeMetric {
125
+ return longestRunningProcessor .WithLabelValues (name )
113
126
}
114
127
115
- func (workqueueMetricsProvider ) NewRetriesMetric (queue string ) workqueue.CounterMetric {
116
- const name = "workqueue_retries_total"
117
- m := prometheus .NewCounter (prometheus.CounterOpts {
118
- Name : name ,
119
- Help : "Total number of retries handled by workqueue" ,
120
- ConstLabels : prometheus.Labels {"name" : queue },
121
- })
122
- registerWorkqueueMetric (m , name , queue )
123
- return m
128
+ func (workqueueMetricsProvider ) NewRetriesMetric (name string ) workqueue.CounterMetric {
129
+ return retries .WithLabelValues (name )
124
130
}
0 commit comments