-
Notifications
You must be signed in to change notification settings - Fork 429
/
metrics.go
350 lines (296 loc) · 10.8 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package daemon
import (
"context"
"net/url"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
reflectormetrics "k8s.io/client-go/tools/cache"
clientmetrics "k8s.io/client-go/tools/metrics"
)
var (
nodeName = ""
cniOperationHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "cni_op_latency_seconds",
Help: "The latency seconds for cni operations",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
}, []string{
"node_name",
"method",
"status_code",
})
cniWaitAddressResult = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cni_wait_address_seconds_total",
Help: "Latency that cni wait controller to assign an address",
},
[]string{"node_name"},
)
cniWaitRouteResult = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cni_wait_route_seconds_total",
Help: "Latency that cni wait controller to add routed annotation to pod",
},
[]string{"node_name"},
)
cniConnectivityResult = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cni_wait_connectivity_seconds_total",
Help: "Latency that cni wait address ready in overlay network",
},
[]string{"node_name"},
)
// client metrics
requestLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "rest_client_request_latency_seconds",
Help: "Request latency in seconds. Broken down by verb and URL.",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
},
[]string{"verb", "url"},
)
requestResult = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "rest_client_requests_total",
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
},
[]string{"code", "method", "host"},
)
metricOvnSubnetGatewayPacketBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ovn_subnet_gateway_packet_bytes",
Help: "the ovn subnet gateway packet bytes.",
}, []string{
"hostname",
"subnet_name",
"cidr",
"direction",
"protocol",
},
)
metricOvnSubnetGatewayPackets = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ovn_subnet_gateway_packets",
Help: "the ovn subnet gateway packet num.",
}, []string{
"hostname",
"subnet_name",
"cidr",
"direction",
"protocol",
},
)
metricIPLocalPortRange = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "ip_local_port_range",
Help: "value of system parameter /proc/sys/net/ipv4/ip_local_port_range, which should not conflict with the nodeport range",
}, []string{
"hostname",
"start",
"end",
})
metricCheckSumErr = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "checksum_err_count",
Help: "Value of InCsumErrors for cmd `netstat -us`, checksum is error when value is greater than 0",
},
[]string{"hostname"})
metricCniConfig = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cni_config_file",
Help: "cni config file in /etc/cni/net.d/",
}, []string{
"hostname",
"ovn",
"other",
})
metricDNSSearch = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "dns_search_domain",
Help: "search domain in /etc/resolv.conf",
}, []string{
"hostname",
"additional",
})
metricTCPTwRecycle = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "tcp_tw_recycle",
Help: "value of system parameter /proc/sys/net/ipv4/tcp_tw_recycle, the recommended value is 0",
}, []string{
"hostname",
})
metricTCPMtuProbing = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "tcp_mtu_probing",
Help: "value of system parameter /proc/sys/net/ipv4/tcp_mtu_probing, the recommended value is 1",
}, []string{
"hostname",
})
metricConntrackTCPLiberal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "nf_conntrack_tcp_be_liberal",
Help: "value of system parameter /proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal, the recommended value is 1",
}, []string{
"hostname",
})
metricBridgeNfCallIptables = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "bridge_nf_call_iptables",
Help: "value of system parameter /proc/sys/net/bridge/bridge-nf-call-iptables, the recommended value is 1 for overlay, and 0 for underlay network",
}, []string{
"hostname",
})
metricTCPMem = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "tcp_mem",
Help: "value of system parameter /proc/sys/net/ipv4/tcp_mem, recommend a large number value",
}, []string{
"hostname",
"minimum",
"pressure",
"maximum",
})
metricIPv6RouteMaxsize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "max_size",
Help: "value of system parameter /proc/sys/net/ipv6/route/max_size, recommend a large number value, at least 16384",
}, []string{
"hostname",
})
// reflector metrics
// TODO(directxman12): update these to be histograms once the metrics overhaul KEP
// PRs start landing.
reflectorSubsystem = "reflector"
listsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "lists_total",
Help: "Total number of API lists done by the reflectors",
}, []string{"name"})
listsDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "list_duration_seconds",
Help: "How long an API list takes to return and decode for the reflectors",
}, []string{"name"})
itemsPerList = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "items_per_list",
Help: "How many items an API list returns to the reflectors",
}, []string{"name"})
watchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "watches_total",
Help: "Total number of API watches done by the reflectors",
}, []string{"name"})
shortWatchesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "short_watches_total",
Help: "Total number of short API watches done by the reflectors",
}, []string{"name"})
watchDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "watch_duration_seconds",
Help: "How long an API watch takes to return and decode for the reflectors",
}, []string{"name"})
itemsPerWatch = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "items_per_watch",
Help: "How many items an API watch returns to the reflectors",
}, []string{"name"})
lastResourceVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: reflectorSubsystem,
Name: "last_resource_version",
Help: "Last resource version seen for the reflectors",
}, []string{"name"})
)
func InitMetrics() {
registerReflectorMetrics()
registerClientMetrics()
registerOvnSubnetGatewayMetrics()
registerSystemParameterMetrics()
prometheus.MustRegister(cniOperationHistogram)
prometheus.MustRegister(cniWaitAddressResult)
prometheus.MustRegister(cniConnectivityResult)
}
func registerOvnSubnetGatewayMetrics() {
prometheus.MustRegister(metricOvnSubnetGatewayPacketBytes)
prometheus.MustRegister(metricOvnSubnetGatewayPackets)
}
func registerSystemParameterMetrics() {
prometheus.MustRegister(metricIPLocalPortRange)
prometheus.MustRegister(metricCheckSumErr)
prometheus.MustRegister(metricCniConfig)
prometheus.MustRegister(metricDNSSearch)
prometheus.MustRegister(metricTCPTwRecycle)
prometheus.MustRegister(metricTCPMtuProbing)
prometheus.MustRegister(metricConntrackTCPLiberal)
prometheus.MustRegister(metricBridgeNfCallIptables)
prometheus.MustRegister(metricTCPMem)
prometheus.MustRegister(metricIPv6RouteMaxsize)
}
// registerClientMetrics sets up the client latency metrics from client-go
func registerClientMetrics() {
// register the metrics with our registry
prometheus.MustRegister(requestLatency)
prometheus.MustRegister(requestResult)
// register the metrics with client-go
opts := clientmetrics.RegisterOpts{
RequestLatency: clientmetrics.LatencyMetric(&latencyAdapter{metric: requestLatency}),
RequestResult: clientmetrics.ResultMetric(&resultAdapter{metric: requestResult}),
}
clientmetrics.Register(opts)
}
// registerReflectorMetrics sets up reflector (reconcile) loop metrics
func registerReflectorMetrics() {
prometheus.MustRegister(listsTotal)
prometheus.MustRegister(listsDuration)
prometheus.MustRegister(itemsPerList)
prometheus.MustRegister(watchesTotal)
prometheus.MustRegister(shortWatchesTotal)
prometheus.MustRegister(watchDuration)
prometheus.MustRegister(itemsPerWatch)
prometheus.MustRegister(lastResourceVersion)
reflectormetrics.SetReflectorMetricsProvider(reflectorMetricsProvider{})
}
// this section contains adapters, implementations, and other sundry organic, artisanally
// hand-crafted syntax trees required to convince client-go that it actually wants to let
// someone use its metrics.
// Client metrics adapters (method #1 for client-go metrics),
// copied (more-or-less directly) from k8s.io/kubernetes setup code
// (which isn't anywhere in an easily-importable place).
type latencyAdapter struct {
metric *prometheus.HistogramVec
}
func (l *latencyAdapter) Observe(_ context.Context, verb string, u url.URL, latency time.Duration) {
url := u.String()
last := strings.LastIndex(url, "/")
if last != -1 {
url = url[:last]
}
l.metric.WithLabelValues(verb, url).Observe(latency.Seconds())
}
type resultAdapter struct {
metric *prometheus.CounterVec
}
func (r *resultAdapter) Increment(_ context.Context, code, method, host string) {
r.metric.WithLabelValues(code, method, host).Inc()
}
// Reflector metrics provider (method #2 for client-go metrics),
// copied (more-or-less directly) from k8s.io/kubernetes setup code
// (which isn't anywhere in an easily-importable place).
type reflectorMetricsProvider struct{}
func (reflectorMetricsProvider) NewListsMetric(name string) reflectormetrics.CounterMetric {
return listsTotal.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewListDurationMetric(name string) reflectormetrics.SummaryMetric {
return listsDuration.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewItemsInListMetric(name string) reflectormetrics.SummaryMetric {
return itemsPerList.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewWatchesMetric(name string) reflectormetrics.CounterMetric {
return watchesTotal.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewShortWatchesMetric(name string) reflectormetrics.CounterMetric {
return shortWatchesTotal.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewWatchDurationMetric(name string) reflectormetrics.SummaryMetric {
return watchDuration.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewItemsInWatchMetric(name string) reflectormetrics.SummaryMetric {
return itemsPerWatch.WithLabelValues(name)
}
func (reflectorMetricsProvider) NewLastResourceVersionMetric(name string) reflectormetrics.GaugeMetric {
return lastResourceVersion.WithLabelValues(name)
}