Debian Codegraph @

pkg/server/metrics/metrics.go

raw
1 /*
2 Copyright 2020 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 //nolint:revive
18
19 import (
20 "strconv"
21 "time"
22
23 "github.com/prometheus/client_golang/prometheus"
24
25 commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics"
26 "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
27 "sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
28 )
29
30 const (
31 Namespace = "konnectivity_network_proxy"
32 Subsystem = "server"
33
34 // Proxy is the ProxyService method used to handle incoming streams.
35 Proxy = "Proxy"
36 // Connect is the AgentService method used to establish next hop.
37 Connect = "Connect"
38 )
39
40 var (
41 // Use buckets ranging from 10 ns to 12.5 seconds.
42 latencyBuckets = []float64{0.000001, 0.00001, 0.0001, 0.005, 0.025, 0.1, 0.5, 2.5, 12.5}
43
44 // Metrics provides access to all dial metrics.
45 Metrics = newServerMetrics()
46 )
47
48 // ServerMetrics includes all the metrics of the proxy server.
49 type ServerMetrics struct {
50 endpointLatencies *prometheus.HistogramVec
51 frontendLatencies *prometheus.HistogramVec
52 grpcConnections *prometheus.GaugeVec
53 httpConnections prometheus.Gauge
54 backend *prometheus.GaugeVec
55 totalBackendCount *prometheus.GaugeVec
56 pendingDials *prometheus.GaugeVec
57 establishedConns *prometheus.GaugeVec
58 fullRecvChannels *prometheus.GaugeVec
59 dialFailures *prometheus.CounterVec
60 streamPackets *prometheus.CounterVec
61 streamErrors *prometheus.CounterVec
62 culledLeases prometheus.Counter
63 leaseDeleteLatencies *prometheus.HistogramVec
64 leaseDeletes *prometheus.CounterVec
65 leaseListLatencies *prometheus.HistogramVec
66 leaseLists *prometheus.CounterVec
67 }
68
69 // newServerMetrics create a new ServerMetrics, configured with default metric names.
70 func newServerMetrics() *ServerMetrics {
71 endpointLatencies := prometheus.NewHistogramVec(
72 prometheus.HistogramOpts{
73 Namespace: Namespace,
74 Subsystem: Subsystem,
75 Name: "dial_duration_seconds",
76 Help: "Latency of dial to the remote endpoint in seconds",
77 Buckets: latencyBuckets,
78 },
79 []string{},
80 )
81 frontendLatencies := prometheus.NewHistogramVec(
82 prometheus.HistogramOpts{
83 Namespace: Namespace,
84 Subsystem: Subsystem,
85 Name: "frontend_write_duration_seconds",
86 Help: "Latency of write to the frontend in seconds",
87 Buckets: latencyBuckets,
88 },
89 []string{},
90 )
91 grpcConnections := prometheus.NewGaugeVec(
92 prometheus.GaugeOpts{
93 Namespace: Namespace,
94 Subsystem: Subsystem,
95 Name: "grpc_connections",
96 Help: "Number of current grpc connections, partitioned by service method.",
97 },
98 []string{
99 "service_method",
100 },
101 )
102 httpConnections := prometheus.NewGauge(
103 prometheus.GaugeOpts{
104 Namespace: Namespace,
105 Subsystem: Subsystem,
106 Name: "http_connections",
107 Help: "Number of current HTTP CONNECT connections",
108 },
109 )
110 backend := prometheus.NewGaugeVec(
111 prometheus.GaugeOpts{
112 Namespace: Namespace,
113 Subsystem: Subsystem,
114 Name: "ready_backend_connections",
115 Help: "Number of konnectivity agent connected to the proxy server. DEPRECATED, please use ready_backends",
116 },
117 []string{},
118 )
119 totalBackendCount := prometheus.NewGaugeVec(
120 prometheus.GaugeOpts{
121 Namespace: Namespace,
122 Subsystem: Subsystem,
123 Name: "ready_backends",
124 Help: "Number of konnectivity agent connected to the proxy server",
125 },
126 []string{
127 "proxy_strategy",
128 },
129 )
130 pendingDials := prometheus.NewGaugeVec(
131 prometheus.GaugeOpts{
132 Namespace: Namespace,
133 Subsystem: Subsystem,
134 Name: "pending_backend_dials",
135 Help: "Current number of pending backend dial requests",
136 },
137 []string{},
138 )
139 establishedConns := prometheus.NewGaugeVec(
140 prometheus.GaugeOpts{
141 Namespace: Namespace,
142 Subsystem: Subsystem,
143 Name: "established_connections",
144 Help: "Current number of established end-to-end connections (post-dial).",
145 },
146 []string{},
147 )
148 fullRecvChannels := prometheus.NewGaugeVec(
149 prometheus.GaugeOpts{
150 Namespace: Namespace,
151 Subsystem: Subsystem,
152 Name: "full_receive_channels",
153 Help: "Number of current connections blocked by a full receive channel, partitioned by service method.",
154 },
155 []string{
156 "service_method",
157 },
158 )
159 dialFailures := prometheus.NewCounterVec(
160 prometheus.CounterOpts{
161 Namespace: Namespace,
162 Subsystem: Subsystem,
163 Name: "dial_failure_count",
164 Help: "Number of dial failures observed. Multiple failures can occur for a single dial request.",
165 },
166 []string{
167 "reason",
168 },
169 )
170 culledLeases := prometheus.NewCounter(prometheus.CounterOpts{
171 Namespace: Namespace,
172 Subsystem: Subsystem,
173 Name: "culled_leases_count",
174 Help: "Count of expired leases that the lease garbage collection controller has culled.",
175 })
176 leaseDeleteLatencies := prometheus.NewHistogramVec(
177 prometheus.HistogramOpts{
178 Namespace: Namespace,
179 Subsystem: Subsystem,
180 Name: "lease_delete_latency_seconds",
181 Help: "Latency of lease deletion calls by the garbage collection controller in seconds.",
182 },
183 []string{"http_status_code"},
184 )
185 leaseDeletes := prometheus.NewCounterVec(
186 prometheus.CounterOpts{
187 Namespace: Namespace,
188 Subsystem: Subsystem,
189 Name: "lease_delete_total",
190 Help: "Count of lease delection calls by the garbage collection controller. Labeled by HTTP status code and reason.",
191 },
192 []string{"http_status_code", "reason"},
193 )
194 leaseListLatencies := prometheus.NewHistogramVec(
195 prometheus.HistogramOpts{
196 Namespace: Namespace,
197 Subsystem: Subsystem,
198 Name: "lease_list_latency_seconds",
199 Help: "Latency of lease list calls by the garbage collection controller in seconds.",
200 },
201 []string{"http_status_code"},
202 )
203 leaseLists := prometheus.NewCounterVec(
204 prometheus.CounterOpts{
205 Namespace: Namespace,
206 Subsystem: Subsystem,
207 Name: "lease_list_total",
208 Help: "Count of lease deletion calls by the garbage collection controller. Labeled by HTTP status code and reason.",
209 },
210 []string{"http_status_code", "reason"},
211 )
212 streamPackets := commonmetrics.MakeStreamPacketsTotalMetric(Namespace, Subsystem)
213 streamErrors := commonmetrics.MakeStreamErrorsTotalMetric(Namespace, Subsystem)
214 prometheus.MustRegister(endpointLatencies)
215 prometheus.MustRegister(frontendLatencies)
216 prometheus.MustRegister(grpcConnections)
217 prometheus.MustRegister(httpConnections)
218 prometheus.MustRegister(backend)
219 prometheus.MustRegister(totalBackendCount)
220 prometheus.MustRegister(pendingDials)
221 prometheus.MustRegister(establishedConns)
222 prometheus.MustRegister(fullRecvChannels)
223 prometheus.MustRegister(dialFailures)
224 prometheus.MustRegister(streamPackets)
225 prometheus.MustRegister(streamErrors)
226 prometheus.MustRegister(culledLeases)
227 prometheus.MustRegister(leaseDeleteLatencies)
228 prometheus.MustRegister(leaseDeletes)
229 prometheus.MustRegister(leaseListLatencies)
230 prometheus.MustRegister(leaseLists)
231 return &ServerMetrics{
232 endpointLatencies: endpointLatencies,
233 frontendLatencies: frontendLatencies,
234 grpcConnections: grpcConnections,
235 httpConnections: httpConnections,
236 backend: backend,
237 totalBackendCount: totalBackendCount,
238 pendingDials: pendingDials,
239 establishedConns: establishedConns,
240 fullRecvChannels: fullRecvChannels,
241 dialFailures: dialFailures,
242 streamPackets: streamPackets,
243 streamErrors: streamErrors,
244 culledLeases: culledLeases,
245 leaseDeleteLatencies: leaseDeleteLatencies,
246 leaseDeletes: leaseDeletes,
247 leaseListLatencies: leaseListLatencies,
248 leaseLists: leaseLists,
249 }
250 }
251
252 // Reset resets the metrics.
253 func (s *ServerMetrics) Reset() {
254 s.endpointLatencies.Reset()
255 s.frontendLatencies.Reset()
256 s.grpcConnections.Reset()
257 s.backend.Reset()
258 s.totalBackendCount.Reset()
259 s.pendingDials.Reset()
260 s.establishedConns.Reset()
261 s.fullRecvChannels.Reset()
262 s.dialFailures.Reset()
263 s.streamPackets.Reset()
264 s.streamErrors.Reset()
265 }
266
267 // CulledLeasesInc increments the number of leases that the GC controller has culled.
268 func (s *ServerMetrics) CulledLeasesInc() {
269 s.culledLeases.Inc()
270 }
271
272 // ObserveDialLatency records the latency of dial to the remote endpoint.
273 func (s *ServerMetrics) ObserveDialLatency(elapsed time.Duration) {
274 s.endpointLatencies.WithLabelValues().Observe(elapsed.Seconds())
275 }
276
277 // ObserveFrontendWriteLatency records the latency of blocking on stream send to the client.
278 func (s *ServerMetrics) ObserveFrontendWriteLatency(elapsed time.Duration) {
279 s.frontendLatencies.WithLabelValues().Observe(elapsed.Seconds())
280 }
281
282 // ConnectionInc increments a new grpc client connection.
283 func (s *ServerMetrics) ConnectionInc(serviceMethod string) {
284 s.grpcConnections.With(prometheus.Labels{"service_method": serviceMethod}).Inc()
285 }
286
287 // ConnectionDec decrements a finished grpc client connection.
288 func (s *ServerMetrics) ConnectionDec(serviceMethod string) {
289 s.grpcConnections.With(prometheus.Labels{"service_method": serviceMethod}).Dec()
290 }
291
292 // HTTPConnectionDec increments a new HTTP CONNECTION connection.
293 func (s *ServerMetrics) HTTPConnectionInc() { s.httpConnections.Inc() }
294
295 // HTTPConnectionDec decrements a finished HTTP CONNECTION connection.
296 func (s *ServerMetrics) HTTPConnectionDec() { s.httpConnections.Dec() }
297
298 // SetBackendCountDeprecated sets the number of backend connection.
299 func (s *ServerMetrics) SetBackendCountDeprecated(count int) {
300 s.backend.WithLabelValues().Set(float64(count))
301 }
302
303 // SetTotalBackendCount sets the total number of backend connection.
304 func (s *ServerMetrics) SetTotalBackendCount(proxyStrategy proxystrategies.ProxyStrategy, count int) {
305 s.totalBackendCount.WithLabelValues(proxyStrategy.String()).Set(float64(count))
306 }
307
308 // SetPendingDialCount sets the number of pending dials.
309 func (s *ServerMetrics) SetPendingDialCount(count int) {
310 s.pendingDials.WithLabelValues().Set(float64(count))
311 }
312
313 // SetEstablishedConnCount sets the number of established connections.
314 func (s *ServerMetrics) SetEstablishedConnCount(count int) {
315 s.establishedConns.WithLabelValues().Set(float64(count))
316 }
317
318 // FullRecvChannel retrieves the metric for counting full receive channels.
319 func (s *ServerMetrics) FullRecvChannel(serviceMethod string) prometheus.Gauge {
320 return s.fullRecvChannels.With(prometheus.Labels{"service_method": serviceMethod})
321 }
322
323 type DialFailureReason string
324
325 const (
326 DialFailureNoAgent DialFailureReason = "no_agent" // No available agent is connected.
327 DialFailureErrorResponse DialFailureReason = "error_response" // Dial failure reported by the agent back to the server.
328 DialFailureUnrecognizedResponse DialFailureReason = "unrecognized_response" // Dial repsonse received for unrecognozide dial ID.
329 DialFailureSendResponse DialFailureReason = "send_rsp" // Successful dial response from agent, but failed to send to frontend.
330 DialFailureBackendClose DialFailureReason = "backend_close" // Received a DIAL_CLS from the backend before the dial completed.
331 DialFailureBackendDialTimeout DialFailureReason = "backend_dial_timeout" // Timed out sending DIAL_REQ to or waiting for DIAL_RSP from the backend.
332 DialFailureFrontendClose DialFailureReason = "frontend_close" // Received a DIAL_CLS from the frontend before the dial completed.
333 )
334
335 func (s *ServerMetrics) ObserveDialFailure(reason DialFailureReason) {
336 s.dialFailures.With(prometheus.Labels{"reason": string(reason)}).Inc()
337 }
338
339 func (s *ServerMetrics) ObservePacket(segment commonmetrics.Segment, packetType client.PacketType) {
340 commonmetrics.ObservePacket(s.streamPackets, segment, packetType)
341 }
342
343 func (s *ServerMetrics) ObserveStreamErrorNoPacket(segment commonmetrics.Segment, err error) {
344 commonmetrics.ObserveStreamErrorNoPacket(s.streamErrors, segment, err)
345 }
346
347 func (s *ServerMetrics) ObserveStreamError(segment commonmetrics.Segment, err error, packetType client.PacketType) {
348 commonmetrics.ObserveStreamError(s.streamErrors, segment, err, packetType)
349 }
350
351 func (s *ServerMetrics) ObserveLeaseDeleteLatency(httpCode int, latency time.Duration) {
352 s.leaseDeleteLatencies.WithLabelValues(strconv.Itoa(httpCode)).Observe(latency.Seconds())
353 }
354
355 func (s *ServerMetrics) ObserveLeaseDelete(httpCode int, reason string) {
356 s.leaseDeletes.WithLabelValues(strconv.Itoa(httpCode), reason).Inc()
357 }
358
359 func (s *ServerMetrics) ObserveLeaseListLatency(httpCode int, latency time.Duration) {
360 s.leaseListLatencies.WithLabelValues(strconv.Itoa(httpCode)).Observe(latency.Seconds())
361 }
362
363 func (s *ServerMetrics) ObserveLeaseList(httpCode int, reason string) {
364 s.leaseLists.WithLabelValues(strconv.Itoa(httpCode), reason).Inc()
365 }