Skip to content

Commit c63f662

Browse files
committed
📖 document exposed metrics (alternative)
Export the variables defining the metrics in this package, and document them, to follow the pattern established by `pkg/internal/controller/metrics` and `pkg/webhook/internal/metrics`.
1 parent ecb4ee3 commit c63f662

File tree

4 files changed

+151
-164
lines changed

4 files changed

+151
-164
lines changed

pkg/metrics/client_go_adapter.go

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ import (
3232
var (
3333
// client metrics
3434

35-
requestLatency = prometheus.NewHistogramVec(
36-
prometheus.HistogramOpts{
37-
Name: "rest_client_request_latency_seconds",
38-
Help: "Request latency in seconds. Broken down by verb and URL.",
39-
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
40-
},
41-
[]string{"verb", "url"},
35+
// RequestLatency is a prometheus metric which tracks the latency of requests
36+
// broken down by verb and URL.
37+
RequestLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
38+
Name: "rest_client_request_latency_seconds",
39+
Help: "Request latency in seconds. Broken down by verb and URL.",
40+
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
41+
}, []string{"verb", "url"},
4242
)
4343

44-
requestResult = prometheus.NewCounterVec(
45-
prometheus.CounterOpts{
46-
Name: "rest_client_requests_total",
47-
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
48-
},
49-
[]string{"code", "method", "host"},
44+
// RequestResult is a prometheus metric that tracks the number of HTTP requests
45+
// broken down by status code, method, and host.
46+
RequestResult = prometheus.NewCounterVec(prometheus.CounterOpts{
47+
Name: "rest_client_requests_total",
48+
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
49+
}, []string{"code", "method", "host"},
5050
)
5151

5252
// reflector metrics
@@ -56,49 +56,65 @@ var (
5656

5757
reflectorSubsystem = "reflector"
5858

59-
listsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
59+
// ListsTotal is a prometheus metric tracking the total number of API lists done
60+
// by the reflectors.
61+
ListsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
6062
Subsystem: reflectorSubsystem,
6163
Name: "lists_total",
6264
Help: "Total number of API lists done by the reflectors",
6365
}, []string{"name"})
6466

65-
listsDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
67+
// ListsDuration is a prometheus metric tracking how long an API list takes to
68+
// return and decode for the reflectors.
69+
ListsDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
6670
Subsystem: reflectorSubsystem,
6771
Name: "list_duration_seconds",
6872
Help: "How long an API list takes to return and decode for the reflectors",
6973
}, []string{"name"})
7074

71-
itemsPerList = prometheus.NewSummaryVec(prometheus.SummaryOpts{
75+
// ItemsPerList is a prometheus metric tracking how many items an API list returns
76+
// to the reflectors.
77+
ItemsPerList = prometheus.NewSummaryVec(prometheus.SummaryOpts{
7278
Subsystem: reflectorSubsystem,
7379
Name: "items_per_list",
7480
Help: "How many items an API list returns to the reflectors",
7581
}, []string{"name"})
7682

77-
watchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
83+
// WatchesTotal is a prometheus metric tracking the total number of API watches
84+
// done by the reflectors.
85+
WatchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
7886
Subsystem: reflectorSubsystem,
7987
Name: "watches_total",
8088
Help: "Total number of API watches done by the reflectors",
8189
}, []string{"name"})
8290

83-
shortWatchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
91+
// ShortWatchesTotal is a prometheus metric tracking the total number of short
92+
// API watches done by the reflectors.
93+
ShortWatchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
8494
Subsystem: reflectorSubsystem,
8595
Name: "short_watches_total",
8696
Help: "Total number of short API watches done by the reflectors",
8797
}, []string{"name"})
8898

89-
watchDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
99+
// WatchDuration is a prometheus metric tracking how long an API watch takes
100+
// to return and decode for the reflectors.
101+
WatchDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
90102
Subsystem: reflectorSubsystem,
91103
Name: "watch_duration_seconds",
92104
Help: "How long an API watch takes to return and decode for the reflectors",
93105
}, []string{"name"})
94106

95-
itemsPerWatch = prometheus.NewSummaryVec(prometheus.SummaryOpts{
107+
// ItemsPerWatch is a prometheus metric tracking how many items an API watch
108+
// returns to the reflectors.
109+
ItemsPerWatch = prometheus.NewSummaryVec(prometheus.SummaryOpts{
96110
Subsystem: reflectorSubsystem,
97111
Name: "items_per_watch",
98112
Help: "How many items an API watch returns to the reflectors",
99113
}, []string{"name"})
100114

101-
lastResourceVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
115+
// LastResourceVersion is a prometheus metric tracking the last resource version
116+
// seen for the reflectors.
117+
LastResourceVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
102118
Subsystem: reflectorSubsystem,
103119
Name: "last_resource_version",
104120
Help: "Last resource version seen for the reflectors",
@@ -113,23 +129,23 @@ func init() {
113129
// registerClientMetrics sets up the client latency metrics from client-go
114130
func registerClientMetrics() {
115131
// register the metrics with our registry
116-
Registry.MustRegister(requestLatency)
117-
Registry.MustRegister(requestResult)
132+
Registry.MustRegister(RequestLatency)
133+
Registry.MustRegister(RequestResult)
118134

119135
// register the metrics with client-go
120-
clientmetrics.Register(&latencyAdapter{metric: requestLatency}, &resultAdapter{metric: requestResult})
136+
clientmetrics.Register(&latencyAdapter{metric: RequestLatency}, &resultAdapter{metric: RequestResult})
121137
}
122138

123139
// registerReflectorMetrics sets up reflector (reconcile) loop metrics
124140
func registerReflectorMetrics() {
125-
Registry.MustRegister(listsTotal)
126-
Registry.MustRegister(listsDuration)
127-
Registry.MustRegister(itemsPerList)
128-
Registry.MustRegister(watchesTotal)
129-
Registry.MustRegister(shortWatchesTotal)
130-
Registry.MustRegister(watchDuration)
131-
Registry.MustRegister(itemsPerWatch)
132-
Registry.MustRegister(lastResourceVersion)
141+
Registry.MustRegister(ListsTotal)
142+
Registry.MustRegister(ListsDuration)
143+
Registry.MustRegister(ItemsPerList)
144+
Registry.MustRegister(WatchesTotal)
145+
Registry.MustRegister(ShortWatchesTotal)
146+
Registry.MustRegister(WatchDuration)
147+
Registry.MustRegister(ItemsPerWatch)
148+
Registry.MustRegister(LastResourceVersion)
133149

134150
reflectormetrics.SetReflectorMetricsProvider(reflectorMetricsProvider{})
135151
}
@@ -165,33 +181,33 @@ func (r *resultAdapter) Increment(code, method, host string) {
165181
type reflectorMetricsProvider struct{}
166182

167183
func (reflectorMetricsProvider) NewListsMetric(name string) reflectormetrics.CounterMetric {
168-
return listsTotal.WithLabelValues(name)
184+
return ListsTotal.WithLabelValues(name)
169185
}
170186

171187
func (reflectorMetricsProvider) NewListDurationMetric(name string) reflectormetrics.SummaryMetric {
172-
return listsDuration.WithLabelValues(name)
188+
return ListsDuration.WithLabelValues(name)
173189
}
174190

175191
func (reflectorMetricsProvider) NewItemsInListMetric(name string) reflectormetrics.SummaryMetric {
176-
return itemsPerList.WithLabelValues(name)
192+
return ItemsPerList.WithLabelValues(name)
177193
}
178194

179195
func (reflectorMetricsProvider) NewWatchesMetric(name string) reflectormetrics.CounterMetric {
180-
return watchesTotal.WithLabelValues(name)
196+
return WatchesTotal.WithLabelValues(name)
181197
}
182198

183199
func (reflectorMetricsProvider) NewShortWatchesMetric(name string) reflectormetrics.CounterMetric {
184-
return shortWatchesTotal.WithLabelValues(name)
200+
return ShortWatchesTotal.WithLabelValues(name)
185201
}
186202

187203
func (reflectorMetricsProvider) NewWatchDurationMetric(name string) reflectormetrics.SummaryMetric {
188-
return watchDuration.WithLabelValues(name)
204+
return WatchDuration.WithLabelValues(name)
189205
}
190206

191207
func (reflectorMetricsProvider) NewItemsInWatchMetric(name string) reflectormetrics.SummaryMetric {
192-
return itemsPerWatch.WithLabelValues(name)
208+
return ItemsPerWatch.WithLabelValues(name)
193209
}
194210

195211
func (reflectorMetricsProvider) NewLastResourceVersionMetric(name string) reflectormetrics.GaugeMetric {
196-
return lastResourceVersion.WithLabelValues(name)
212+
return LastResourceVersion.WithLabelValues(name)
197213
}

pkg/metrics/doc.go

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,6 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
Package metrics contains controller related metrics utilities.
19-
20-
Client Metrics
21-
22-
"rest_client_request_latency_seconds" - Request latency in seconds. Broken down by verb and URL.
23-
24-
"rest_client_requests_total" - Number of HTTP requests, partitioned by status code, method, and host.
25-
26-
Reflector Metrics
27-
28-
"reflector_lists_total" - Total number of API lists done by the reflectors.
29-
30-
"reflector_list_duration_seconds" - How long an API list takes to return and decode for the reflectors.
31-
32-
"reflector_items_per_list" - How many items an API list returns to the reflectors.
33-
34-
"reflector_watches_total" - Total number of API watches done by the reflectors.
35-
36-
"reflector_short_watches_total" - Total number of short API watches done by the reflectors.
37-
38-
"reflector_watch_duration_seconds" - How long an API watch takes to return and decode for the reflectors.
39-
40-
"reflector_items_per_watch" - How many items an API watch returns to the reflectors.
41-
42-
"reflector_last_resource_version" - Last resource version seen for the reflectors.
43-
44-
Workqueue Metrics
45-
46-
"workqueue_depth" - Current depth of workqueue
47-
48-
"workqueue_adds_total" - Total number of adds handled by workqueue.
49-
50-
"workqueue_queue_duration_seconds" - How long in seconds an item stays in workqueue before being requested.
51-
52-
"workqueue_work_duration_seconds" - How long in seconds processing an item from workqueue takes.
53-
54-
"workqueue_unfinished_work_seconds" - How many seconds of work has done that
55-
is in progress and hasn't been observed by work_duration. Large
56-
values indicate stuck threads. One can deduce the number of stuck
57-
threads by observing the rate at which this increases.
58-
59-
"workqueue_longest_running_processor_seconds" - How many seconds has the longest running
60-
processor for workqueue been running.
61-
62-
"workqueue_retries_total" - Total number of retries handled by workqueue.
63-
18+
Package metrics contains controller related metrics utilities
6419
*/
6520
package metrics

pkg/metrics/listener.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ package metrics
1919
import (
2020
"fmt"
2121
"net"
22+
23+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
2224
)
2325

26+
var log = logf.RuntimeLog.WithName("metrics")
27+
2428
// DefaultBindAddress sets the default bind address for the metrics listener
2529
// The metrics is on by default.
2630
var DefaultBindAddress = ":8080"

0 commit comments

Comments
 (0)