| 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 provides metric definitions and helpers used |
| 18 |
// across konnectivity client, server, and agent. |
| 19 |
package metrics |
| 20 |
|
| 21 |
import ( |
| 22 |
"github.com/prometheus/client_golang/prometheus" |
| 23 |
"google.golang.org/grpc/status" |
| 24 |
|
| 25 |
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" |
| 26 |
) |
| 27 |
|
| 28 |
// Segment identifies one of four tunnel segments (e.g. from server to agent). |
| 29 |
type Segment string |
| 30 |
|
| 31 |
const ( |
| 32 |
// SegmentFromClient indicates a packet from client to server. |
| 33 |
SegmentFromClient Segment = "from_client" |
| 34 |
// SegmentToClient indicates a packet from server to client. |
| 35 |
SegmentToClient Segment = "to_client" |
| 36 |
// SegmentFromAgent indicates a packet from agent to server. |
| 37 |
SegmentFromAgent Segment = "from_agent" |
| 38 |
// SegmentToAgent indicates a packet from server to agent. |
| 39 |
SegmentToAgent Segment = "to_agent" |
| 40 |
) |
| 41 |
|
| 42 |
func MakeStreamPacketsTotalMetric(namespace, subsystem string) *prometheus.CounterVec { |
| 43 |
return prometheus.NewCounterVec( |
| 44 |
prometheus.CounterOpts{ |
| 45 |
Namespace: namespace, |
| 46 |
Subsystem: subsystem, |
| 47 |
Name: "stream_packets_total", |
| 48 |
Help: "Count of packets processed, by segment and packet type (example: from_client, DIAL_REQ)", |
| 49 |
}, |
| 50 |
[]string{"segment", "packet_type"}, |
| 51 |
) |
| 52 |
} |
| 53 |
|
| 54 |
func MakeStreamErrorsTotalMetric(namespace, subsystem string) *prometheus.CounterVec { |
| 55 |
return prometheus.NewCounterVec( |
| 56 |
prometheus.CounterOpts{ |
| 57 |
Namespace: namespace, |
| 58 |
Subsystem: subsystem, |
| 59 |
Name: "stream_errors_total", |
| 60 |
Help: "Count of gRPC stream errors, by segment, grpc Code, packet type. (example: from_agent, Code.Unavailable, DIAL_RSP)", |
| 61 |
}, |
| 62 |
[]string{"segment", "code", "packet_type"}, |
| 63 |
) |
| 64 |
} |
| 65 |
|
| 66 |
func ObservePacket(m *prometheus.CounterVec, segment Segment, packetType client.PacketType) { |
| 67 |
m.WithLabelValues(string(segment), packetType.String()).Inc() |
| 68 |
} |
| 69 |
|
| 70 |
func ObserveStreamErrorNoPacket(m *prometheus.CounterVec, segment Segment, err error) { |
| 71 |
code := status.Code(err) |
| 72 |
m.WithLabelValues(string(segment), code.String(), "Unknown").Inc() |
| 73 |
} |
| 74 |
|
| 75 |
func ObserveStreamError(m *prometheus.CounterVec, segment Segment, err error, packetType client.PacketType) { |
| 76 |
code := status.Code(err) |
| 77 |
m.WithLabelValues(string(segment), code.String(), packetType.String()).Inc() |
| 78 |
} |