Debian Codegraph @

pkg/server/metrics/metrics.go

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