| 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 //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 |
) |
| 28 |
|
| 29 |
type Direction string |
| 30 |
|
| 31 |
const ( |
| 32 |
Namespace = "konnectivity_network_proxy" |
| 33 |
Subsystem = "agent" |
| 34 |
|
| 35 |
// DirectionToServer indicates that the agent attempts to send a packet |
| 36 |
// to the proxy server. |
| 37 |
DirectionToServer Direction = "to_server" |
| 38 |
// DirectionFromServer indicates that the agent attempts to receive a |
| 39 |
// packet from the proxy server. |
| 40 |
DirectionFromServer Direction = "from_server" |
| 41 |
) |
| 42 |
|
| 43 |
var ( |
| 44 |
// Use buckets ranging from 5 ms to 30 seconds. |
| 45 |
latencyBuckets = []float64{0.005, 0.025, 0.1, 0.5, 2.5, 10, 30} |
| 46 |
|
| 47 |
// Metrics provides access to all dial metrics. |
| 48 |
Metrics = newAgentMetrics() |
| 49 |
) |
| 50 |
|
| 51 |
// AgentMetrics includes all the metrics of the proxy agent. |
| 52 |
type AgentMetrics struct { |
| 53 |
dialLatencies *prometheus.HistogramVec |
| 54 |
serverFailures *prometheus.CounterVec |
| 55 |
dialFailures *prometheus.CounterVec |
| 56 |
serverConnections *prometheus.GaugeVec |
| 57 |
serverCount prometheus.Gauge |
| 58 |
endpointConnections *prometheus.GaugeVec |
| 59 |
streamPackets *prometheus.CounterVec |
| 60 |
streamErrors *prometheus.CounterVec |
| 61 |
leaseLists *prometheus.CounterVec |
| 62 |
leaseWatches *prometheus.CounterVec |
| 63 |
leaseListLatencies *prometheus.HistogramVec |
| 64 |
} |
| 65 |
|
| 66 |
// newAgentMetrics create a new AgentMetrics, configured with default metric names. |
| 67 |
func newAgentMetrics() *AgentMetrics { |
| 68 |
dialLatencies := prometheus.NewHistogramVec( |
| 69 |
prometheus.HistogramOpts{ |
| 70 |
Namespace: Namespace, |
| 71 |
Subsystem: Subsystem, |
| 72 |
Name: "dial_duration_seconds", |
| 73 |
Help: "Latency of dial to the remote endpoint in seconds", |
| 74 |
Buckets: latencyBuckets, |
| 75 |
}, |
| 76 |
[]string{}, |
| 77 |
) |
| 78 |
serverFailures := prometheus.NewCounterVec( |
| 79 |
prometheus.CounterOpts{ |
| 80 |
Namespace: Namespace, |
| 81 |
Subsystem: Subsystem, |
| 82 |
Name: "server_connection_failure_count", |
| 83 |
Help: "Count of failures to send to or receive from the proxy server, labeled by the direction (from_server or to_server). DEPRECATED, please use stream_events_error_total", |
| 84 |
}, |
| 85 |
[]string{"direction"}, |
| 86 |
) |
| 87 |
dialFailures := prometheus.NewCounterVec( |
| 88 |
prometheus.CounterOpts{ |
| 89 |
Namespace: Namespace, |
| 90 |
Subsystem: Subsystem, |
| 91 |
Name: "endpoint_dial_failure_total", |
| 92 |
Help: "Number of failures dialing the remote endpoint, by reason (example: timeout).", |
| 93 |
}, |
| 94 |
[]string{"reason"}, |
| 95 |
) |
| 96 |
serverConnections := prometheus.NewGaugeVec( |
| 97 |
prometheus.GaugeOpts{ |
| 98 |
Namespace: Namespace, |
| 99 |
Subsystem: Subsystem, |
| 100 |
Name: "open_server_connections", |
| 101 |
Help: "Current number of open server connections.", |
| 102 |
}, |
| 103 |
[]string{}, |
| 104 |
) |
| 105 |
serverCount := prometheus.NewGauge( |
| 106 |
prometheus.GaugeOpts{ |
| 107 |
Namespace: Namespace, |
| 108 |
Subsystem: Subsystem, |
| 109 |
Name: "known_server_count", |
| 110 |
Help: "Current number of servers agent is trying to connect to.", |
| 111 |
}, |
| 112 |
) |
| 113 |
endpointConnections := prometheus.NewGaugeVec( |
| 114 |
prometheus.GaugeOpts{ |
| 115 |
Namespace: Namespace, |
| 116 |
Subsystem: Subsystem, |
| 117 |
Name: "open_endpoint_connections", |
| 118 |
Help: "Current number of open endpoint connections.", |
| 119 |
}, |
| 120 |
[]string{}, |
| 121 |
) |
| 122 |
leaseLists := prometheus.NewCounterVec( |
| 123 |
prometheus.CounterOpts{ |
| 124 |
Namespace: Namespace, |
| 125 |
Subsystem: Subsystem, |
| 126 |
Name: "lease_lists_total", |
| 127 |
Help: "Count of server lease list calls made by the agent to the k8s apiserver, labeled by HTTP response code and reason", |
| 128 |
}, |
| 129 |
[]string{"http_response_code", "reason"}, |
| 130 |
) |
| 131 |
leaseWatches := prometheus.NewCounterVec( |
| 132 |
prometheus.CounterOpts{ |
| 133 |
Namespace: Namespace, |
| 134 |
Subsystem: Subsystem, |
| 135 |
Name: "lease_watches_total", |
| 136 |
Help: "Count of server lease watch calls made by the agent to the k8s apiserver, labeled by HTTP response code and reason", |
| 137 |
}, |
| 138 |
[]string{"http_response_code", "reason"}, |
| 139 |
) |
| 140 |
leaseListLatencies := prometheus.NewHistogramVec( |
| 141 |
prometheus.HistogramOpts{ |
| 142 |
Namespace: Namespace, |
| 143 |
Subsystem: Subsystem, |
| 144 |
Name: "lease_list_latency_seconds", |
| 145 |
Help: "Latency of server lease listing in seconds", |
| 146 |
Buckets: latencyBuckets, |
| 147 |
}, |
| 148 |
[]string{"http_response_code"}, |
| 149 |
) |
| 150 |
streamPackets := commonmetrics.MakeStreamPacketsTotalMetric(Namespace, Subsystem) |
| 151 |
streamErrors := commonmetrics.MakeStreamErrorsTotalMetric(Namespace, Subsystem) |
| 152 |
prometheus.MustRegister(dialLatencies) |
| 153 |
prometheus.MustRegister(serverFailures) |
| 154 |
prometheus.MustRegister(dialFailures) |
| 155 |
prometheus.MustRegister(serverConnections) |
| 156 |
prometheus.MustRegister(endpointConnections) |
| 157 |
prometheus.MustRegister(streamPackets) |
| 158 |
prometheus.MustRegister(streamErrors) |
| 159 |
prometheus.MustRegister(serverCount) |
| 160 |
prometheus.MustRegister(leaseLists) |
| 161 |
prometheus.MustRegister(leaseWatches) |
| 162 |
prometheus.MustRegister(leaseListLatencies) |
| 163 |
return &AgentMetrics{ |
| 164 |
dialLatencies: dialLatencies, |
| 165 |
serverFailures: serverFailures, |
| 166 |
dialFailures: dialFailures, |
| 167 |
serverConnections: serverConnections, |
| 168 |
endpointConnections: endpointConnections, |
| 169 |
streamPackets: streamPackets, |
| 170 |
streamErrors: streamErrors, |
| 171 |
serverCount: serverCount, |
| 172 |
leaseLists: leaseLists, |
| 173 |
leaseWatches: leaseWatches, |
| 174 |
leaseListLatencies: leaseListLatencies, |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
// Reset resets the metrics. |
| 180 |
func (a *AgentMetrics) Reset() { |
| 181 |
a.dialLatencies.Reset() |
| 182 |
a.serverFailures.Reset() |
| 183 |
a.dialFailures.Reset() |
| 184 |
a.serverConnections.Reset() |
| 185 |
a.endpointConnections.Reset() |
| 186 |
a.streamPackets.Reset() |
| 187 |
a.streamErrors.Reset() |
| 188 |
a.leaseLists.Reset() |
| 189 |
} |
| 190 |
|
| 191 |
// ObserveServerFailure records a failure to send to or receive from the proxy |
| 192 |
// server, labeled by the direction. |
| 193 |
func (a *AgentMetrics) ObserveServerFailureDeprecated(direction Direction) { |
| 194 |
a.serverFailures.WithLabelValues(string(direction)).Inc() |
| 195 |
} |
| 196 |
|
| 197 |
type DialFailureReason string |
| 198 |
|
| 199 |
const ( |
| 200 |
DialFailureTimeout DialFailureReason = "timeout" |
| 201 |
DialFailureUnknown DialFailureReason = "unknown" |
| 202 |
) |
| 203 |
|
| 204 |
// ObserveDialLatency records the latency of dial to the remote endpoint. |
| 205 |
func (a *AgentMetrics) ObserveDialLatency(elapsed time.Duration) { |
| 206 |
a.dialLatencies.WithLabelValues().Observe(elapsed.Seconds()) |
| 207 |
} |
| 208 |
|
| 209 |
// ObserveDialFailure records a remote endpoint dial failure. |
| 210 |
func (a *AgentMetrics) ObserveDialFailure(reason DialFailureReason) { |
| 211 |
a.dialFailures.WithLabelValues(string(reason)).Inc() |
| 212 |
} |
| 213 |
|
| 214 |
func (a *AgentMetrics) SetServerConnectionsCount(count int) { |
| 215 |
a.serverConnections.WithLabelValues().Set(float64(count)) |
| 216 |
} |
| 217 |
|
| 218 |
func (a *AgentMetrics) SetServerCount(count int) { |
| 219 |
a.serverCount.Set(float64(count)) |
| 220 |
} |
| 221 |
|
| 222 |
func (a *AgentMetrics) ObserveLeaseList(httpCode int, reason string) { |
| 223 |
a.leaseLists.WithLabelValues(strconv.Itoa(httpCode), reason).Inc() |
| 224 |
} |
| 225 |
|
| 226 |
func (a *AgentMetrics) ObserveLeaseWatch(httpCode int, reason string) { |
| 227 |
a.leaseLists.WithLabelValues(strconv.Itoa(httpCode), reason).Inc() |
| 228 |
} |
| 229 |
|
| 230 |
func (a *AgentMetrics) ObserveLeaseListLatency(latency time.Duration, httpCode int) { |
| 231 |
a.leaseListLatencies.WithLabelValues(strconv.Itoa(httpCode)).Observe(latency.Seconds()) |
| 232 |
} |
| 233 |
|
| 234 |
// EndpointConnectionInc increments a new endpoint connection. |
| 235 |
func (a *AgentMetrics) EndpointConnectionInc() { |
| 236 |
a.endpointConnections.WithLabelValues().Inc() |
| 237 |
} |
| 238 |
|
| 239 |
// EndpointConnectionDec decrements a finished endpoint connection. |
| 240 |
func (a *AgentMetrics) EndpointConnectionDec() { |
| 241 |
a.endpointConnections.WithLabelValues().Dec() |
| 242 |
} |
| 243 |
|
| 244 |
func (a *AgentMetrics) ObservePacket(segment commonmetrics.Segment, packetType client.PacketType) { |
| 245 |
commonmetrics.ObservePacket(a.streamPackets, segment, packetType) |
| 246 |
} |
| 247 |
|
| 248 |
func (a *AgentMetrics) ObserveStreamErrorNoPacket(segment commonmetrics.Segment, err error) { |
| 249 |
commonmetrics.ObserveStreamErrorNoPacket(a.streamErrors, segment, err) |
| 250 |
} |
| 251 |
|
| 252 |
func (a *AgentMetrics) ObserveStreamError(segment commonmetrics.Segment, err error, packetType client.PacketType) { |
| 253 |
commonmetrics.ObserveStreamError(a.streamErrors, segment, err, packetType) |
| 254 |
} |