k8s.io/apiserver@v0.31.1/pkg/server/egressselector/metrics/metrics.go (about) 1 /* 2 Copyright 2017 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 "time" 21 22 "k8s.io/component-base/metrics" 23 "k8s.io/component-base/metrics/legacyregistry" 24 "k8s.io/utils/clock" 25 ) 26 27 const ( 28 namespace = "apiserver" 29 subsystem = "egress_dialer" 30 31 // ProtocolHTTPConnect means that the proxy protocol is http-connect. 32 ProtocolHTTPConnect = "http_connect" 33 // ProtocolGRPC means that the proxy protocol is the GRPC protocol. 34 ProtocolGRPC = "grpc" 35 // TransportTCP means that the transport is TCP. 36 TransportTCP = "tcp" 37 // TransportUDS means that the transport is UDS. 38 TransportUDS = "uds" 39 // StageConnect indicates that the dial failed at establishing connection to the proxy server. 40 StageConnect = "connect" 41 // StageProxy indicates that the dial failed at requesting the proxy server to proxy. 42 StageProxy = "proxy" 43 ) 44 45 var ( 46 // Use buckets ranging from 5 ms to 12.5 seconds. 47 latencyBuckets = []float64{0.005, 0.025, 0.1, 0.5, 2.5, 12.5} 48 49 // Metrics provides access to all dial metrics. 50 Metrics = newDialMetrics() 51 ) 52 53 // DialMetrics instruments dials to proxy server with prometheus metrics. 54 type DialMetrics struct { 55 clock clock.Clock 56 starts *metrics.CounterVec 57 latencies *metrics.HistogramVec 58 failures *metrics.CounterVec 59 } 60 61 // newDialMetrics create a new DialMetrics, configured with default metric names. 62 func newDialMetrics() *DialMetrics { 63 starts := metrics.NewCounterVec( 64 &metrics.CounterOpts{ 65 Namespace: namespace, 66 Subsystem: subsystem, 67 Name: "dial_start_total", 68 Help: "Dial starts, labeled by the protocol (http-connect or grpc) and transport (tcp or uds).", 69 StabilityLevel: metrics.ALPHA, 70 }, 71 []string{"protocol", "transport"}, 72 ) 73 74 latencies := metrics.NewHistogramVec( 75 &metrics.HistogramOpts{ 76 Namespace: namespace, 77 Subsystem: subsystem, 78 Name: "dial_duration_seconds", 79 Help: "Dial latency histogram in seconds, labeled by the protocol (http-connect or grpc), transport (tcp or uds)", 80 Buckets: latencyBuckets, 81 StabilityLevel: metrics.ALPHA, 82 }, 83 []string{"protocol", "transport"}, 84 ) 85 86 failures := metrics.NewCounterVec( 87 &metrics.CounterOpts{ 88 Namespace: namespace, 89 Subsystem: subsystem, 90 Name: "dial_failure_count", 91 Help: "Dial failure count, labeled by the protocol (http-connect or grpc), transport (tcp or uds), and stage (connect or proxy). The stage indicates at which stage the dial failed", 92 StabilityLevel: metrics.ALPHA, 93 }, 94 []string{"protocol", "transport", "stage"}, 95 ) 96 97 legacyregistry.MustRegister(starts) 98 legacyregistry.MustRegister(latencies) 99 legacyregistry.MustRegister(failures) 100 return &DialMetrics{starts: starts, latencies: latencies, failures: failures, clock: clock.RealClock{}} 101 } 102 103 // Clock returns the clock. 104 func (m *DialMetrics) Clock() clock.Clock { 105 return m.clock 106 } 107 108 // SetClock sets the clock. 109 func (m *DialMetrics) SetClock(c clock.Clock) { 110 m.clock = c 111 } 112 113 // Reset resets the metrics. 114 func (m *DialMetrics) Reset() { 115 m.starts.Reset() 116 m.latencies.Reset() 117 m.failures.Reset() 118 } 119 120 // ObserveDialStart records the start of a dial attempt, labeled by protocol, transport. 121 func (m *DialMetrics) ObserveDialStart(protocol, transport string) { 122 m.starts.WithLabelValues(protocol, transport).Inc() 123 } 124 125 // ObserveDialLatency records the latency of a dial, labeled by protocol, transport. 126 func (m *DialMetrics) ObserveDialLatency(elapsed time.Duration, protocol, transport string) { 127 m.latencies.WithLabelValues(protocol, transport).Observe(elapsed.Seconds()) 128 } 129 130 // ObserveDialFailure records a failed dial, labeled by protocol, transport, and the stage the dial failed at. 131 func (m *DialMetrics) ObserveDialFailure(protocol, transport, stage string) { 132 m.failures.WithLabelValues(protocol, transport, stage).Inc() 133 }