| 1 |
/* |
| 2 |
Copyright 2022 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 |
"sync" |
| 21 |
|
| 22 |
"github.com/prometheus/client_golang/prometheus" |
| 23 |
|
| 24 |
commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics" |
| 25 |
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
Namespace = "konnectivity_network_proxy" |
| 30 |
Subsystem = "client" |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
// Metrics provides access to all client metrics. The client |
| 35 |
// application is responsible for registering (via Metrics.RegisterMetrics). |
| 36 |
Metrics = newMetrics() |
| 37 |
) |
| 38 |
|
| 39 |
// ClientMetrics includes all the metrics of the konnectivity-client. |
| 40 |
type ClientMetrics struct { |
| 41 |
registerOnce sync.Once |
| 42 |
streamPackets *prometheus.CounterVec |
| 43 |
streamErrors *prometheus.CounterVec |
| 44 |
dialFailures *prometheus.CounterVec |
| 45 |
clientConns *prometheus.GaugeVec |
| 46 |
} |
| 47 |
|
| 48 |
type DialFailureReason string |
| 49 |
|
| 50 |
const ( |
| 51 |
DialFailureUnknown DialFailureReason = "unknown" |
| 52 |
// DialFailureTimeout indicates the hard 30 second timeout was hit. |
| 53 |
DialFailureTimeout DialFailureReason = "timeout" |
| 54 |
// DialFailureContext indicates that the context was cancelled or reached it's deadline before |
| 55 |
// the dial response was returned. |
| 56 |
DialFailureContext DialFailureReason = "context" |
| 57 |
// DialFailureEndpoint indicates that the konnectivity-agent was unable to reach the backend endpoint. |
| 58 |
DialFailureEndpoint DialFailureReason = "endpoint" |
| 59 |
// DialFailureDialClosed indicates that the client received a CloseDial response, indicating the |
| 60 |
// connection was closed before the dial could complete. |
| 61 |
DialFailureDialClosed DialFailureReason = "dialclosed" |
| 62 |
// DialFailureTunnelClosed indicates that the client connection was closed before the dial could |
| 63 |
// complete. |
| 64 |
DialFailureTunnelClosed DialFailureReason = "tunnelclosed" |
| 65 |
// DialFailureAlreadyStarted indicates that a single-use tunnel dialer was already used once. |
| 66 |
DialFailureAlreadyStarted DialFailureReason = "tunnelstarted" |
| 67 |
) |
| 68 |
|
| 69 |
type ClientConnectionStatus string |
| 70 |
|
| 71 |
const ( |
| 72 |
// The connection is created but has not yet been dialed. |
| 73 |
ClientConnectionStatusCreated ClientConnectionStatus = "created" |
| 74 |
// The connection is pending dial response. |
| 75 |
ClientConnectionStatusDialing ClientConnectionStatus = "dialing" |
| 76 |
// The connection is established. |
| 77 |
ClientConnectionStatusOk ClientConnectionStatus = "ok" |
| 78 |
// The connection is closing. |
| 79 |
ClientConnectionStatusClosing ClientConnectionStatus = "closing" |
| 80 |
) |
| 81 |
|
| 82 |
func newMetrics() *ClientMetrics { |
| 83 |
// The denominator (total dials started) for both |
| 84 |
// dial_failure_total and dial_duration_seconds is the |
| 85 |
// stream_packets_total (common metric), where segment is |
| 86 |
// "from_client" and packet_type is "DIAL_REQ". |
| 87 |
dialFailures := prometheus.NewCounterVec( |
| 88 |
prometheus.CounterOpts{ |
| 89 |
Namespace: Namespace, |
| 90 |
Subsystem: Subsystem, |
| 91 |
Name: "dial_failure_total", |
| 92 |
Help: "Number of dial failures observed, by reason (example: remote endpoint error)", |
| 93 |
}, |
| 94 |
[]string{ |
| 95 |
"reason", |
| 96 |
}, |
| 97 |
) |
| 98 |
clientConns := prometheus.NewGaugeVec( |
| 99 |
prometheus.GaugeOpts{ |
| 100 |
Namespace: Namespace, |
| 101 |
Subsystem: Subsystem, |
| 102 |
Name: "client_connections", |
| 103 |
Help: "Number of open client connections, by status (Example: dialing)", |
| 104 |
}, |
| 105 |
[]string{ |
| 106 |
"status", |
| 107 |
}, |
| 108 |
) |
| 109 |
return &ClientMetrics{ |
| 110 |
streamPackets: commonmetrics.MakeStreamPacketsTotalMetric(Namespace, Subsystem), |
| 111 |
streamErrors: commonmetrics.MakeStreamErrorsTotalMetric(Namespace, Subsystem), |
| 112 |
dialFailures: dialFailures, |
| 113 |
clientConns: clientConns, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// RegisterMetrics registers all metrics with the client application. |
| 118 |
func (c *ClientMetrics) RegisterMetrics(r prometheus.Registerer) { |
| 119 |
c.registerOnce.Do(func() { |
| 120 |
r.MustRegister(c.streamPackets) |
| 121 |
r.MustRegister(c.streamErrors) |
| 122 |
r.MustRegister(c.dialFailures) |
| 123 |
r.MustRegister(c.clientConns) |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
// LegacyRegisterMetrics registers all metrics via MustRegister func. |
| 128 |
// TODO: remove this once https://github.com/kubernetes/kubernetes/pull/114293 is available. |
| 129 |
func (c *ClientMetrics) LegacyRegisterMetrics(mustRegisterFn func(...prometheus.Collector)) { |
| 130 |
c.registerOnce.Do(func() { |
| 131 |
mustRegisterFn(c.streamPackets) |
| 132 |
mustRegisterFn(c.streamErrors) |
| 133 |
mustRegisterFn(c.dialFailures) |
| 134 |
mustRegisterFn(c.clientConns) |
| 135 |
}) |
| 136 |
} |
| 137 |
|
| 138 |
// Reset resets the metrics. |
| 139 |
func (c *ClientMetrics) Reset() { |
| 140 |
c.streamPackets.Reset() |
| 141 |
c.streamErrors.Reset() |
| 142 |
c.dialFailures.Reset() |
| 143 |
c.clientConns.Reset() |
| 144 |
} |
| 145 |
|
| 146 |
func (c *ClientMetrics) ObserveDialFailure(reason DialFailureReason) { |
| 147 |
c.dialFailures.WithLabelValues(string(reason)).Inc() |
| 148 |
} |
| 149 |
|
| 150 |
func (c *ClientMetrics) GetClientConnectionsMetric() *prometheus.GaugeVec { |
| 151 |
return c.clientConns |
| 152 |
} |
| 153 |
|
| 154 |
func (c *ClientMetrics) ObservePacket(segment commonmetrics.Segment, packetType client.PacketType) { |
| 155 |
commonmetrics.ObservePacket(c.streamPackets, segment, packetType) |
| 156 |
} |
| 157 |
|
| 158 |
func (c *ClientMetrics) ObserveStreamErrorNoPacket(segment commonmetrics.Segment, err error) { |
| 159 |
commonmetrics.ObserveStreamErrorNoPacket(c.streamErrors, segment, err) |
| 160 |
} |
| 161 |
|
| 162 |
func (c *ClientMetrics) ObserveStreamError(segment commonmetrics.Segment, err error, packetType client.PacketType) { |
| 163 |
commonmetrics.ObserveStreamError(c.streamErrors, segment, err, packetType) |
| 164 |
} |