6
6
7
7
"k8s.io/client-go/util/workqueue"
8
8
"k8s.io/utils/clock"
9
+ "sigs.k8s.io/controller-runtime/pkg/internal/metrics"
9
10
)
10
11
11
12
// This file is mostly a copy of unexported code from
@@ -14,8 +15,9 @@ import (
14
15
// The only two differences are the addition of mapLock in defaultQueueMetrics and converging retryMetrics into queueMetrics.
15
16
16
17
type queueMetrics [T comparable ] interface {
17
- add (item T )
18
- get (item T )
18
+ add (item T , priority int )
19
+ get (item T , priority int )
20
+ updateDepthWithPriorityMetric (oldPriority , newPriority int )
19
21
done (item T )
20
22
updateUnfinishedWork ()
21
23
retry ()
@@ -25,9 +27,9 @@ func newQueueMetrics[T comparable](mp workqueue.MetricsProvider, name string, cl
25
27
if len (name ) == 0 {
26
28
return noMetrics [T ]{}
27
29
}
28
- return & defaultQueueMetrics [T ]{
30
+
31
+ dqm := & defaultQueueMetrics [T ]{
29
32
clock : clock ,
30
- depth : mp .NewDepthMetric (name ),
31
33
adds : mp .NewAddsMetric (name ),
32
34
latency : mp .NewLatencyMetric (name ),
33
35
workDuration : mp .NewWorkDurationMetric (name ),
@@ -37,14 +39,22 @@ func newQueueMetrics[T comparable](mp workqueue.MetricsProvider, name string, cl
37
39
processingStartTimes : map [T ]time.Time {},
38
40
retries : mp .NewRetriesMetric (name ),
39
41
}
42
+
43
+ if mpp , ok := mp .(metrics.MetricsProviderWithPriority ); ok {
44
+ dqm .depthWithPriority = mpp .NewDepthMetricWithPriority (name )
45
+ } else {
46
+ dqm .depth = mp .NewDepthMetric (name )
47
+ }
48
+ return dqm
40
49
}
41
50
42
51
// defaultQueueMetrics expects the caller to lock before setting any metrics.
43
52
type defaultQueueMetrics [T comparable ] struct {
44
53
clock clock.Clock
45
54
46
55
// current depth of a workqueue
47
- depth workqueue.GaugeMetric
56
+ depth workqueue.GaugeMetric
57
+ depthWithPriority metrics.DepthMetricWithPriority
48
58
// total number of adds handled by a workqueue
49
59
adds workqueue.CounterMetric
50
60
// how long an item stays in a workqueue
@@ -64,13 +74,17 @@ type defaultQueueMetrics[T comparable] struct {
64
74
}
65
75
66
76
// add is called for ready items only
67
- func (m * defaultQueueMetrics [T ]) add (item T ) {
77
+ func (m * defaultQueueMetrics [T ]) add (item T , priority int ) {
68
78
if m == nil {
69
79
return
70
80
}
71
81
72
82
m .adds .Inc ()
73
- m .depth .Inc ()
83
+ if m .depthWithPriority != nil {
84
+ m .depthWithPriority .Inc (priority )
85
+ } else {
86
+ m .depth .Inc ()
87
+ }
74
88
75
89
m .mapLock .Lock ()
76
90
defer m .mapLock .Unlock ()
@@ -80,12 +94,16 @@ func (m *defaultQueueMetrics[T]) add(item T) {
80
94
}
81
95
}
82
96
83
- func (m * defaultQueueMetrics [T ]) get (item T ) {
97
+ func (m * defaultQueueMetrics [T ]) get (item T , priority int ) {
84
98
if m == nil {
85
99
return
86
100
}
87
101
88
- m .depth .Dec ()
102
+ if m .depthWithPriority != nil {
103
+ m .depthWithPriority .Dec (priority )
104
+ } else {
105
+ m .depth .Dec ()
106
+ }
89
107
90
108
m .mapLock .Lock ()
91
109
defer m .mapLock .Unlock ()
@@ -97,6 +115,13 @@ func (m *defaultQueueMetrics[T]) get(item T) {
97
115
}
98
116
}
99
117
118
+ func (m * defaultQueueMetrics [T ]) updateDepthWithPriorityMetric (oldPriority , newPriority int ) {
119
+ if m .depthWithPriority != nil {
120
+ m .depthWithPriority .Dec (oldPriority )
121
+ m .depthWithPriority .Inc (newPriority )
122
+ }
123
+ }
124
+
100
125
func (m * defaultQueueMetrics [T ]) done (item T ) {
101
126
if m == nil {
102
127
return
@@ -139,8 +164,9 @@ func (m *defaultQueueMetrics[T]) retry() {
139
164
140
165
type noMetrics [T any ] struct {}
141
166
142
- func (noMetrics [T ]) add (item T ) {}
143
- func (noMetrics [T ]) get (item T ) {}
144
- func (noMetrics [T ]) done (item T ) {}
145
- func (noMetrics [T ]) updateUnfinishedWork () {}
146
- func (noMetrics [T ]) retry () {}
167
+ func (noMetrics [T ]) add (item T , priority int ) {}
168
+ func (noMetrics [T ]) get (item T , priority int ) {}
169
+ func (noMetrics [T ]) updateDepthWithPriorityMetric (oldPriority , newPriority int ) {}
170
+ func (noMetrics [T ]) done (item T ) {}
171
+ func (noMetrics [T ]) updateUnfinishedWork () {}
172
+ func (noMetrics [T ]) retry () {}
0 commit comments