2021-10-15 11:05:54 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
2024-02-16 11:28:59 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-10-15 11:05:54 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-05-06 00:42:41 +00:00
|
|
|
"github.com/quic-go/quic-go/logging"
|
2021-10-15 11:05:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
namespace = "quic"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
clientConnLabels = []string{"conn_index"}
|
|
|
|
clientMetrics = struct {
|
|
|
|
totalConnections prometheus.Counter
|
2022-12-07 11:04:32 +00:00
|
|
|
closedConnections prometheus.Counter
|
2024-02-16 11:28:59 +00:00
|
|
|
sentFrames *prometheus.CounterVec
|
2021-10-15 11:05:54 +00:00
|
|
|
sentBytes *prometheus.CounterVec
|
2024-02-16 11:28:59 +00:00
|
|
|
receivedFrames *prometheus.CounterVec
|
|
|
|
receivedBytes *prometheus.CounterVec
|
2021-10-15 11:05:54 +00:00
|
|
|
bufferedPackets *prometheus.CounterVec
|
|
|
|
droppedPackets *prometheus.CounterVec
|
|
|
|
lostPackets *prometheus.CounterVec
|
|
|
|
minRTT *prometheus.GaugeVec
|
|
|
|
latestRTT *prometheus.GaugeVec
|
|
|
|
smoothedRTT *prometheus.GaugeVec
|
|
|
|
}{
|
|
|
|
totalConnections: prometheus.NewCounter(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "total_connections",
|
|
|
|
Help: "Number of connections initiated",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
),
|
2022-12-07 11:04:32 +00:00
|
|
|
closedConnections: prometheus.NewCounter(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "closed_connections",
|
|
|
|
Help: "Number of connections that has been closed",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
),
|
2024-02-16 11:28:59 +00:00
|
|
|
sentFrames: prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "sent_frames",
|
|
|
|
Help: "Number of frames that have been sent through a connection",
|
|
|
|
},
|
|
|
|
append(clientConnLabels, "frame_type"),
|
2021-10-15 11:05:54 +00:00
|
|
|
),
|
|
|
|
sentBytes: prometheus.NewCounterVec(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "sent_bytes",
|
|
|
|
Help: "Number of bytes that have been sent through a connection",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
clientConnLabels,
|
|
|
|
),
|
2024-02-16 11:28:59 +00:00
|
|
|
receivedFrames: prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "received_frames",
|
|
|
|
Help: "Number of frames that have been received through a connection",
|
|
|
|
},
|
|
|
|
append(clientConnLabels, "frame_type"),
|
2021-10-15 11:05:54 +00:00
|
|
|
),
|
2024-02-16 11:28:59 +00:00
|
|
|
receivedBytes: prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "receive_bytes",
|
|
|
|
Help: "Number of bytes that have been received through a connection",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
clientConnLabels,
|
|
|
|
),
|
|
|
|
bufferedPackets: prometheus.NewCounterVec(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "buffered_packets",
|
|
|
|
Help: "Number of bytes that have been buffered on a connection",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
append(clientConnLabels, "packet_type"),
|
|
|
|
),
|
|
|
|
droppedPackets: prometheus.NewCounterVec(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "dropped_packets",
|
|
|
|
Help: "Number of bytes that have been dropped on a connection",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
append(clientConnLabels, "packet_type", "reason"),
|
|
|
|
),
|
|
|
|
lostPackets: prometheus.NewCounterVec(
|
2024-02-16 11:28:59 +00:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: "client",
|
|
|
|
Name: "lost_packets",
|
|
|
|
Help: "Number of packets that have been lost from a connection",
|
|
|
|
},
|
2021-10-15 11:05:54 +00:00
|
|
|
append(clientConnLabels, "reason"),
|
|
|
|
),
|
|
|
|
minRTT: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
2024-02-16 11:28:59 +00:00
|
|
|
Subsystem: "client",
|
2021-10-15 11:05:54 +00:00
|
|
|
Name: "min_rtt",
|
|
|
|
Help: "Lowest RTT measured on a connection in millisec",
|
|
|
|
},
|
|
|
|
clientConnLabels,
|
|
|
|
),
|
|
|
|
latestRTT: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
2024-02-16 11:28:59 +00:00
|
|
|
Subsystem: "client",
|
2021-10-15 11:05:54 +00:00
|
|
|
Name: "latest_rtt",
|
|
|
|
Help: "Latest RTT measured on a connection",
|
|
|
|
},
|
|
|
|
clientConnLabels,
|
|
|
|
),
|
|
|
|
smoothedRTT: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
2024-02-16 11:28:59 +00:00
|
|
|
Subsystem: "client",
|
2021-10-15 11:05:54 +00:00
|
|
|
Name: "smoothed_rtt",
|
|
|
|
Help: "Calculated smoothed RTT measured on a connection in millisec",
|
|
|
|
},
|
|
|
|
clientConnLabels,
|
|
|
|
),
|
|
|
|
}
|
2024-02-16 11:28:59 +00:00
|
|
|
|
2021-10-15 11:05:54 +00:00
|
|
|
registerClient = sync.Once{}
|
2022-08-25 10:05:01 +00:00
|
|
|
|
|
|
|
packetTooBigDropped = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
2024-02-16 11:28:59 +00:00
|
|
|
Subsystem: "client",
|
2022-08-25 10:05:01 +00:00
|
|
|
Name: "packet_too_big_dropped",
|
|
|
|
Help: "Count of packets received from origin that are too big to send to the edge and are dropped as a result",
|
|
|
|
})
|
2021-10-15 11:05:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientCollector struct {
|
|
|
|
index string
|
|
|
|
}
|
|
|
|
|
2024-02-16 11:28:59 +00:00
|
|
|
func newClientCollector(index uint8) *clientCollector {
|
2021-10-15 11:05:54 +00:00
|
|
|
registerClient.Do(func() {
|
|
|
|
prometheus.MustRegister(
|
|
|
|
clientMetrics.totalConnections,
|
|
|
|
clientMetrics.closedConnections,
|
2024-02-16 11:28:59 +00:00
|
|
|
clientMetrics.sentFrames,
|
2021-10-15 11:05:54 +00:00
|
|
|
clientMetrics.sentBytes,
|
2024-02-16 11:28:59 +00:00
|
|
|
clientMetrics.receivedFrames,
|
|
|
|
clientMetrics.receivedBytes,
|
2021-10-15 11:05:54 +00:00
|
|
|
clientMetrics.bufferedPackets,
|
|
|
|
clientMetrics.droppedPackets,
|
|
|
|
clientMetrics.lostPackets,
|
|
|
|
clientMetrics.minRTT,
|
|
|
|
clientMetrics.latestRTT,
|
|
|
|
clientMetrics.smoothedRTT,
|
2022-08-25 10:05:01 +00:00
|
|
|
packetTooBigDropped,
|
2021-10-15 11:05:54 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
return &clientCollector{
|
|
|
|
index: uint8ToString(index),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) startedConnection() {
|
|
|
|
clientMetrics.totalConnections.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) closedConnection(err error) {
|
2022-12-07 11:04:32 +00:00
|
|
|
clientMetrics.closedConnections.Inc()
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 11:28:59 +00:00
|
|
|
func (cc *clientCollector) sentPackets(size logging.ByteCount, frames []logging.Frame) {
|
|
|
|
cc.collectPackets(size, frames, clientMetrics.sentFrames, clientMetrics.sentBytes)
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 11:28:59 +00:00
|
|
|
func (cc *clientCollector) receivedPackets(size logging.ByteCount, frames []logging.Frame) {
|
|
|
|
cc.collectPackets(size, frames, clientMetrics.receivedFrames, clientMetrics.receivedBytes)
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) bufferedPackets(packetType logging.PacketType) {
|
|
|
|
clientMetrics.bufferedPackets.WithLabelValues(cc.index, packetTypeString(packetType)).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) droppedPackets(packetType logging.PacketType, size logging.ByteCount, reason logging.PacketDropReason) {
|
|
|
|
clientMetrics.droppedPackets.WithLabelValues(
|
|
|
|
cc.index,
|
|
|
|
packetTypeString(packetType),
|
|
|
|
packetDropReasonString(reason),
|
|
|
|
).Add(byteCountToPromCount(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) lostPackets(reason logging.PacketLossReason) {
|
|
|
|
clientMetrics.lostPackets.WithLabelValues(cc.index, packetLossReasonString(reason)).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *clientCollector) updatedRTT(rtt *logging.RTTStats) {
|
|
|
|
clientMetrics.minRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.MinRTT()))
|
|
|
|
clientMetrics.latestRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.LatestRTT()))
|
|
|
|
clientMetrics.smoothedRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.SmoothedRTT()))
|
|
|
|
}
|
|
|
|
|
2024-02-16 11:28:59 +00:00
|
|
|
func (cc *clientCollector) collectPackets(size logging.ByteCount, frames []logging.Frame, counter, bandwidth *prometheus.CounterVec) {
|
|
|
|
for _, frame := range frames {
|
|
|
|
counter.WithLabelValues(cc.index, frameName(frame)).Inc()
|
|
|
|
}
|
|
|
|
bandwidth.WithLabelValues(cc.index).Add(byteCountToPromCount(size))
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 11:28:59 +00:00
|
|
|
func frameName(frame logging.Frame) string {
|
|
|
|
if frame == nil {
|
|
|
|
return "nil"
|
|
|
|
} else {
|
|
|
|
name := reflect.TypeOf(frame).Elem().Name()
|
|
|
|
return strings.TrimSuffix(name, "Frame")
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
}
|