Skip to content

Commit d708e3d

Browse files
authored
Merge pull request #437 from abursavich/queuemetrics
🐛 remove deprecated workqueue metrics with invalid names
2 parents be010e1 + ce521f8 commit d708e3d

File tree

2 files changed

+173
-189
lines changed

2 files changed

+173
-189
lines changed

pkg/metrics/client_go_adapter.go

Lines changed: 0 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,9 @@ import (
2020
"net/url"
2121
"time"
2222

23-
"k8s.io/apimachinery/pkg/util/runtime"
24-
2523
"github.com/prometheus/client_golang/prometheus"
2624
reflectormetrics "k8s.io/client-go/tools/cache"
2725
clientmetrics "k8s.io/client-go/tools/metrics"
28-
workqueuemetrics "k8s.io/client-go/util/workqueue"
29-
)
30-
31-
const (
32-
workQueueSubsystem = "workqueue"
33-
depthKey = "depth"
34-
addsKey = "adds_total"
35-
queueLatencyKey = "queue_duration_seconds"
36-
workDurationKey = "work_duration_seconds"
37-
unfinishedWorkKey = "unfinished_work_seconds"
38-
longestRunningProcessorKey = "longest_running_processor_seconds"
39-
retriesKey = "retries_total"
4026
)
4127

4228
// this file contains setup logic to initialize the myriad of places
@@ -117,62 +103,11 @@ var (
117103
Name: "last_resource_version",
118104
Help: "Last resource version seen for the reflectors",
119105
}, []string{"name"})
120-
121-
// workqueue metrics
122-
123-
depth = prometheus.NewGaugeVec(prometheus.GaugeOpts{
124-
Subsystem: workQueueSubsystem,
125-
Name: depthKey,
126-
Help: "Current depth of workqueue",
127-
}, []string{"name"})
128-
129-
adds = prometheus.NewCounterVec(prometheus.CounterOpts{
130-
Subsystem: workQueueSubsystem,
131-
Name: addsKey,
132-
Help: "Total number of adds handled by workqueue",
133-
}, []string{"name"})
134-
135-
latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
136-
Subsystem: workQueueSubsystem,
137-
Name: queueLatencyKey,
138-
Help: "How long in seconds an item stays in workqueue before being requested.",
139-
Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10),
140-
}, []string{"name"})
141-
142-
workDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
143-
Subsystem: workQueueSubsystem,
144-
Name: workDurationKey,
145-
Help: "How long in seconds processing an item from workqueue takes.",
146-
Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10),
147-
}, []string{"name"})
148-
149-
unfinishedWork = prometheus.NewGaugeVec(prometheus.GaugeOpts{
150-
Subsystem: workQueueSubsystem,
151-
Name: unfinishedWorkKey,
152-
Help: "How many seconds of work has done that " +
153-
"is in progress and hasn't been observed by work_duration. Large " +
154-
"values indicate stuck threads. One can deduce the number of stuck " +
155-
"threads by observing the rate at which this increases.",
156-
}, []string{"name"})
157-
158-
longestRunning = prometheus.NewGaugeVec(prometheus.GaugeOpts{
159-
Subsystem: workQueueSubsystem,
160-
Name: longestRunningProcessorKey,
161-
Help: "How many seconds has the longest running " +
162-
"processor for workqueue been running.",
163-
}, []string{"name"})
164-
165-
retries = prometheus.NewCounterVec(prometheus.CounterOpts{
166-
Subsystem: workQueueSubsystem,
167-
Name: retriesKey,
168-
Help: "Total number of retries handled by workqueue",
169-
}, []string{"name"})
170106
)
171107

172108
func init() {
173109
registerClientMetrics()
174110
registerReflectorMetrics()
175-
registerWorkqueueMetrics()
176111
}
177112

178113
// registerClientMetrics sets up the client latency metrics from client-go
@@ -199,19 +134,6 @@ func registerReflectorMetrics() {
199134
reflectormetrics.SetReflectorMetricsProvider(reflectorMetricsProvider{})
200135
}
201136

202-
// registerWorkQueueMetrics sets up workqueue (other reconcile) metrics
203-
func registerWorkqueueMetrics() {
204-
Registry.MustRegister(depth)
205-
Registry.MustRegister(adds)
206-
Registry.MustRegister(latency)
207-
Registry.MustRegister(workDuration)
208-
Registry.MustRegister(retries)
209-
Registry.MustRegister(longestRunning)
210-
Registry.MustRegister(unfinishedWork)
211-
212-
workqueuemetrics.SetProvider(workqueueMetricsProvider{})
213-
}
214-
215137
// this section contains adapters, implementations, and other sundry organic, artisinally
216138
// hand-crafted syntax trees required to convince client-go that it actually wants to let
217139
// someone use its metrics.
@@ -273,114 +195,3 @@ func (reflectorMetricsProvider) NewItemsInWatchMetric(name string) reflectormetr
273195
func (reflectorMetricsProvider) NewLastResourceVersionMetric(name string) reflectormetrics.GaugeMetric {
274196
return lastResourceVersion.WithLabelValues(name)
275197
}
276-
277-
// Workqueue metrics (method #3 for client-go metrics),
278-
// copied (more-or-less directly) from k8s.io/kubernetes setup code
279-
// (which isn't anywhere in an easily-importable place).
280-
// TODO(directxman12): stop "cheating" and calling histograms summaries when we pull in the latest deps
281-
282-
type workqueueMetricsProvider struct{}
283-
284-
func (workqueueMetricsProvider) NewDepthMetric(name string) workqueuemetrics.GaugeMetric {
285-
return depth.WithLabelValues(name)
286-
}
287-
288-
func (workqueueMetricsProvider) NewAddsMetric(name string) workqueuemetrics.CounterMetric {
289-
return adds.WithLabelValues(name)
290-
}
291-
292-
func (workqueueMetricsProvider) NewLatencyMetric(name string) workqueuemetrics.HistogramMetric {
293-
return latency.WithLabelValues(name)
294-
}
295-
296-
func (workqueueMetricsProvider) NewWorkDurationMetric(name string) workqueuemetrics.HistogramMetric {
297-
return workDuration.WithLabelValues(name)
298-
}
299-
300-
func (workqueueMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueuemetrics.SettableGaugeMetric {
301-
return unfinishedWork.WithLabelValues(name)
302-
}
303-
304-
func (workqueueMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueuemetrics.SettableGaugeMetric {
305-
return longestRunning.WithLabelValues(name)
306-
}
307-
308-
func (workqueueMetricsProvider) NewRetriesMetric(name string) workqueuemetrics.CounterMetric {
309-
return retries.WithLabelValues(name)
310-
}
311-
312-
func (workqueueMetricsProvider) NewDeprecatedDepthMetric(name string) workqueuemetrics.GaugeMetric {
313-
depth := prometheus.NewGauge(prometheus.GaugeOpts{
314-
Subsystem: name,
315-
Name: "depth",
316-
Help: "Current depth of workqueue: " + name,
317-
})
318-
runtime.HandleError(Registry.Register(depth))
319-
return depth
320-
}
321-
322-
func (workqueueMetricsProvider) NewDeprecatedAddsMetric(name string) workqueuemetrics.CounterMetric {
323-
adds := prometheus.NewCounter(prometheus.CounterOpts{
324-
Subsystem: name,
325-
Name: "adds",
326-
Help: "Total number of adds handled by workqueue: " + name,
327-
})
328-
runtime.HandleError(Registry.Register(adds))
329-
return adds
330-
}
331-
332-
func (workqueueMetricsProvider) NewDeprecatedLatencyMetric(name string) workqueuemetrics.SummaryMetric {
333-
latency := prometheus.NewSummary(prometheus.SummaryOpts{
334-
Subsystem: name,
335-
Name: "queue_latency",
336-
Help: "How long an item stays in workqueue" + name + " before being requested.",
337-
ConstLabels: prometheus.Labels{"name": name},
338-
})
339-
runtime.HandleError(Registry.Register(latency))
340-
return latency
341-
}
342-
343-
func (workqueueMetricsProvider) NewDeprecatedWorkDurationMetric(name string) workqueuemetrics.SummaryMetric {
344-
workDuration := prometheus.NewSummary(prometheus.SummaryOpts{
345-
Subsystem: name,
346-
Name: "work_duration",
347-
Help: "How long processing an item from workqueue" + name + " takes.",
348-
ConstLabels: prometheus.Labels{"name": name},
349-
})
350-
runtime.HandleError(Registry.Register(workDuration))
351-
return workDuration
352-
}
353-
354-
func (workqueueMetricsProvider) NewDeprecatedUnfinishedWorkSecondsMetric(name string) workqueuemetrics.SettableGaugeMetric {
355-
unfinishedWork := prometheus.NewGauge(prometheus.GaugeOpts{
356-
Subsystem: name,
357-
Name: "unfinished_work_seconds",
358-
Help: "How many seconds of work " + name + " has done that " +
359-
"is in progress and hasn't been observed by work_duration. Large " +
360-
"values indicate stuck threads. One can deduce the number of stuck " +
361-
"threads by observing the rate at which this increases.",
362-
})
363-
runtime.HandleError(Registry.Register(unfinishedWork))
364-
return unfinishedWork
365-
}
366-
367-
func (workqueueMetricsProvider) NewDeprecatedLongestRunningProcessorMicrosecondsMetric(name string) workqueuemetrics.SettableGaugeMetric {
368-
longestRunning := prometheus.NewGauge(prometheus.GaugeOpts{
369-
Subsystem: name,
370-
Name: "longest_running_processor_microseconds",
371-
Help: "How many microseconds has the longest running " +
372-
"processor for " + name + " been running.",
373-
})
374-
runtime.HandleError(Registry.Register(longestRunning))
375-
return longestRunning
376-
}
377-
378-
func (workqueueMetricsProvider) NewDeprecatedRetriesMetric(name string) workqueuemetrics.CounterMetric {
379-
retries := prometheus.NewCounter(prometheus.CounterOpts{
380-
Subsystem: name,
381-
Name: "retries",
382-
Help: "Total number of retries handled by workqueue: " + name,
383-
})
384-
runtime.HandleError(Registry.Register(retries))
385-
return retries
386-
}

pkg/metrics/workqueue.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"github.com/prometheus/client_golang/prometheus"
21+
"k8s.io/client-go/util/workqueue"
22+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
23+
)
24+
25+
var log = logf.RuntimeLog.WithName("metrics")
26+
27+
// This file is copied and adapted from k8s.io/kubernetes/pkg/util/workqueue/prometheus
28+
// which registers metrics to the default prometheus Registry. We require very
29+
// similar functionality, but must register metrics to a different Registry.
30+
31+
func init() {
32+
workqueue.SetProvider(workqueueMetricsProvider{})
33+
}
34+
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+
}
39+
}
40+
41+
type workqueueMetricsProvider struct{}
42+
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
52+
}
53+
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
63+
}
64+
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
75+
}
76+
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
87+
}
88+
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
101+
}
102+
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
113+
}
114+
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
124+
}
125+
126+
// TODO(abursavich): Remove the following deprecated metrics when they are
127+
// removed from k8s.io/client-go/util/workqueue.
128+
129+
func (workqueueMetricsProvider) NewDeprecatedLongestRunningProcessorMicrosecondsMetric(queue string) workqueue.SettableGaugeMetric {
130+
const name = "workqueue_longest_running_processor_microseconds"
131+
m := prometheus.NewGauge(prometheus.GaugeOpts{
132+
Name: name,
133+
Help: "(Deprecated) How many microseconds has the longest running " +
134+
"processor for workqueue been running.",
135+
ConstLabels: prometheus.Labels{"name": queue},
136+
})
137+
registerWorkqueueMetric(m, name, queue)
138+
return m
139+
}
140+
141+
// NOTE: The following deprecated metrics are noops because they were never
142+
// included in controller-runtime.
143+
144+
func (workqueueMetricsProvider) NewDeprecatedDepthMetric(queue string) workqueue.GaugeMetric {
145+
return noopMetric{}
146+
}
147+
148+
func (workqueueMetricsProvider) NewDeprecatedAddsMetric(queue string) workqueue.CounterMetric {
149+
return noopMetric{}
150+
}
151+
152+
func (workqueueMetricsProvider) NewDeprecatedLatencyMetric(queue string) workqueue.SummaryMetric {
153+
return noopMetric{}
154+
}
155+
156+
func (workqueueMetricsProvider) NewDeprecatedWorkDurationMetric(queue string) workqueue.SummaryMetric {
157+
return noopMetric{}
158+
}
159+
160+
func (workqueueMetricsProvider) NewDeprecatedUnfinishedWorkSecondsMetric(queue string) workqueue.SettableGaugeMetric {
161+
return noopMetric{}
162+
}
163+
164+
func (workqueueMetricsProvider) NewDeprecatedRetriesMetric(queue string) workqueue.CounterMetric {
165+
return noopMetric{}
166+
}
167+
168+
type noopMetric struct{}
169+
170+
func (noopMetric) Inc() {}
171+
func (noopMetric) Dec() {}
172+
func (noopMetric) Set(float64) {}
173+
func (noopMetric) Observe(float64) {}

0 commit comments

Comments
 (0)