2021-10-15 11:05:54 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-05-24 18:41:42 +00:00
|
|
|
"time"
|
2021-10-15 11:05:54 +00:00
|
|
|
|
2023-05-06 00:42:41 +00:00
|
|
|
"github.com/quic-go/quic-go/logging"
|
2021-10-15 11:05:54 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QUICTracer is a wrapper to create new quicConnTracer
|
|
|
|
type tracer struct {
|
|
|
|
logger *zerolog.Logger
|
|
|
|
config *tracerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type tracerConfig struct {
|
|
|
|
isClient bool
|
|
|
|
// Only client has an index
|
|
|
|
index uint8
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func NewClientTracer(logger *zerolog.Logger, index uint8) func(context.Context, logging.Perspective, logging.ConnectionID) *logging.ConnectionTracer {
|
2023-05-06 00:42:41 +00:00
|
|
|
t := &tracer{
|
2021-10-15 11:05:54 +00:00
|
|
|
logger: logger,
|
|
|
|
config: &tracerConfig{
|
|
|
|
isClient: true,
|
|
|
|
index: index,
|
|
|
|
},
|
|
|
|
}
|
2023-05-06 00:42:41 +00:00
|
|
|
return t.TracerForConnection
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func NewServerTracer(logger *zerolog.Logger) *logging.Tracer {
|
|
|
|
return &logging.Tracer{
|
|
|
|
SentPacket: func(net.Addr, *logging.Header, logging.ByteCount, []logging.Frame) {},
|
|
|
|
SentVersionNegotiationPacket: func(_ net.Addr, dest, src logging.ArbitraryLenConnectionID, _ []logging.VersionNumber) {},
|
|
|
|
DroppedPacket: func(net.Addr, logging.PacketType, logging.ByteCount, logging.PacketDropReason) {},
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (t *tracer) TracerForConnection(_ctx context.Context, _p logging.Perspective, _odcid logging.ConnectionID) *logging.ConnectionTracer {
|
2021-10-15 11:05:54 +00:00
|
|
|
if t.config.isClient {
|
2022-05-24 18:41:42 +00:00
|
|
|
return newConnTracer(newClientCollector(t.config.index))
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
2022-05-24 18:41:42 +00:00
|
|
|
return newConnTracer(newServiceCollector())
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:41:42 +00:00
|
|
|
// connTracer collects connection level metrics
|
2021-10-15 11:05:54 +00:00
|
|
|
type connTracer struct {
|
|
|
|
metricsCollector MetricsCollector
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func newConnTracer(metricsCollector MetricsCollector) *logging.ConnectionTracer {
|
|
|
|
tracer := connTracer{
|
2022-05-24 18:41:42 +00:00
|
|
|
metricsCollector: metricsCollector,
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
2023-12-04 09:49:00 +00:00
|
|
|
return &logging.ConnectionTracer{
|
|
|
|
StartedConnection: tracer.StartedConnection,
|
|
|
|
NegotiatedVersion: tracer.NegotiatedVersion,
|
|
|
|
ClosedConnection: tracer.ClosedConnection,
|
|
|
|
SentTransportParameters: tracer.SentTransportParameters,
|
|
|
|
ReceivedTransportParameters: tracer.ReceivedTransportParameters,
|
|
|
|
RestoredTransportParameters: tracer.RestoredTransportParameters,
|
|
|
|
SentLongHeaderPacket: tracer.SentLongHeaderPacket,
|
|
|
|
SentShortHeaderPacket: tracer.SentShortHeaderPacket,
|
|
|
|
ReceivedVersionNegotiationPacket: tracer.ReceivedVersionNegotiationPacket,
|
|
|
|
ReceivedRetry: tracer.ReceivedRetry,
|
|
|
|
ReceivedLongHeaderPacket: tracer.ReceivedLongHeaderPacket,
|
|
|
|
ReceivedShortHeaderPacket: tracer.ReceivedShortHeaderPacket,
|
|
|
|
BufferedPacket: tracer.BufferedPacket,
|
|
|
|
DroppedPacket: tracer.DroppedPacket,
|
|
|
|
UpdatedMetrics: tracer.UpdatedMetrics,
|
|
|
|
AcknowledgedPacket: tracer.AcknowledgedPacket,
|
|
|
|
LostPacket: tracer.LostPacket,
|
|
|
|
UpdatedCongestionState: tracer.UpdatedCongestionState,
|
|
|
|
UpdatedPTOCount: tracer.UpdatedPTOCount,
|
|
|
|
UpdatedKeyFromTLS: tracer.UpdatedKeyFromTLS,
|
|
|
|
UpdatedKey: tracer.UpdatedKey,
|
|
|
|
DroppedEncryptionLevel: tracer.DroppedEncryptionLevel,
|
|
|
|
DroppedKey: tracer.DroppedKey,
|
|
|
|
SetLossTimer: tracer.SetLossTimer,
|
|
|
|
LossTimerExpired: tracer.LossTimerExpired,
|
|
|
|
LossTimerCanceled: tracer.LossTimerCanceled,
|
|
|
|
ECNStateUpdated: tracer.ECNStateUpdated,
|
|
|
|
Close: tracer.Close,
|
|
|
|
Debug: tracer.Debug,
|
|
|
|
}
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) StartedConnection(local, remote net.Addr, srcConnID, destConnID logging.ConnectionID) {
|
|
|
|
ct.metricsCollector.startedConnection()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) ClosedConnection(err error) {
|
|
|
|
ct.metricsCollector.closedConnection(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) SentPacket(hdr *logging.ExtendedHeader, packetSize logging.ByteCount, ack *logging.AckFrame, frames []logging.Frame) {
|
|
|
|
ct.metricsCollector.sentPackets(packetSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) ReceivedPacket(hdr *logging.ExtendedHeader, size logging.ByteCount, frames []logging.Frame) {
|
|
|
|
ct.metricsCollector.receivedPackets(size)
|
|
|
|
}
|
|
|
|
|
2023-05-06 00:42:41 +00:00
|
|
|
func (ct *connTracer) BufferedPacket(pt logging.PacketType, size logging.ByteCount) {
|
2021-10-15 11:05:54 +00:00
|
|
|
ct.metricsCollector.bufferedPackets(pt)
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) DroppedPacket(pt logging.PacketType, number logging.PacketNumber, size logging.ByteCount, reason logging.PacketDropReason) {
|
2021-10-15 11:05:54 +00:00
|
|
|
ct.metricsCollector.droppedPackets(pt, size, reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) LostPacket(level logging.EncryptionLevel, number logging.PacketNumber, reason logging.PacketLossReason) {
|
|
|
|
ct.metricsCollector.lostPackets(reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) UpdatedMetrics(rttStats *logging.RTTStats, cwnd, bytesInFlight logging.ByteCount, packetsInFlight int) {
|
|
|
|
ct.metricsCollector.updatedRTT(rttStats)
|
2022-05-24 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) NegotiatedVersion(chosen logging.VersionNumber, clientVersions, serverVersions []logging.VersionNumber) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) SentTransportParameters(parameters *logging.TransportParameters) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) ReceivedTransportParameters(parameters *logging.TransportParameters) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) RestoredTransportParameters(parameters *logging.TransportParameters) {
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) SentLongHeaderPacket(hdr *logging.ExtendedHeader, size logging.ByteCount, ecn logging.ECN, ack *logging.AckFrame, frames []logging.Frame) {
|
2023-05-06 00:42:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) SentShortHeaderPacket(hdr *logging.ShortHeader, size logging.ByteCount, ecn logging.ECN, ack *logging.AckFrame, frames []logging.Frame) {
|
2023-05-06 00:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) ReceivedVersionNegotiationPacket(dest, src logging.ArbitraryLenConnectionID, _ []logging.VersionNumber) {
|
2022-05-24 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) ReceivedRetry(header *logging.Header) {
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) ReceivedLongHeaderPacket(hdr *logging.ExtendedHeader, size logging.ByteCount, ecn logging.ECN, frames []logging.Frame) {
|
2023-05-06 00:42:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) ReceivedShortHeaderPacket(hdr *logging.ShortHeader, size logging.ByteCount, ecn logging.ECN, frames []logging.Frame) {
|
2023-05-06 00:42:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:41:42 +00:00
|
|
|
func (ct *connTracer) AcknowledgedPacket(level logging.EncryptionLevel, number logging.PacketNumber) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) UpdatedCongestionState(state logging.CongestionState) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) UpdatedPTOCount(value uint32) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) UpdatedKeyFromTLS(level logging.EncryptionLevel, perspective logging.Perspective) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) UpdatedKey(generation logging.KeyPhase, remote bool) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) DroppedEncryptionLevel(level logging.EncryptionLevel) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) DroppedKey(generation logging.KeyPhase) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) SetLossTimer(timerType logging.TimerType, level logging.EncryptionLevel, time time.Time) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) LossTimerExpired(timerType logging.TimerType, level logging.EncryptionLevel) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) LossTimerCanceled() {
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
func (ct *connTracer) ECNStateUpdated(state logging.ECNState, trigger logging.ECNStateTrigger) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:41:42 +00:00
|
|
|
func (ct *connTracer) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *connTracer) Debug(name, msg string) {
|
2021-10-15 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type quicLogger struct {
|
|
|
|
logger *zerolog.Logger
|
|
|
|
connectionID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qt *quicLogger) Write(p []byte) (n int, err error) {
|
|
|
|
qt.logger.Trace().Str("quicConnection", qt.connectionID).RawJSON("event", p).Msg("Quic event")
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*quicLogger) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|