Merge pull request #2676 from lucas-clemente/conn-close-metric

add a metric for closed connections
This commit is contained in:
Marten Seemann
2020-07-20 12:36:48 +07:00
committed by GitHub
2 changed files with 51 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package metrics
import ( import (
"context" "context"
"fmt"
"net" "net"
"time" "time"
@@ -18,6 +19,7 @@ var (
connections = stats.Int64("quic-go/connections", "number of QUIC connections", stats.UnitDimensionless) connections = stats.Int64("quic-go/connections", "number of QUIC connections", stats.UnitDimensionless)
lostPackets = stats.Int64("quic-go/lost-packets", "number of packets declared lost", stats.UnitDimensionless) lostPackets = stats.Int64("quic-go/lost-packets", "number of packets declared lost", stats.UnitDimensionless)
sentPackets = stats.Int64("quic-go/sent-packets", "number of packets sent", stats.UnitDimensionless) sentPackets = stats.Int64("quic-go/sent-packets", "number of packets sent", stats.UnitDimensionless)
closes = stats.Int64("quic-go/close", "number of connections closed", stats.UnitDimensionless)
) )
// Tags // Tags
@@ -27,6 +29,9 @@ var (
keyEncryptionLevel, _ = tag.NewKey("encryption_level") keyEncryptionLevel, _ = tag.NewKey("encryption_level")
keyPacketLossReason, _ = tag.NewKey("packet_loss_reason") keyPacketLossReason, _ = tag.NewKey("packet_loss_reason")
keyPacketType, _ = tag.NewKey("packet_type") keyPacketType, _ = tag.NewKey("packet_type")
keyCloseReason, _ = tag.NewKey("close_reason")
keyCloseRemote, _ = tag.NewKey("close_remote")
keyErrorCode, _ = tag.NewKey("error_code")
) )
// Views // Views
@@ -46,6 +51,11 @@ var (
TagKeys: []tag.Key{keyPacketType}, TagKeys: []tag.Key{keyPacketType},
Aggregation: view.Count(), Aggregation: view.Count(),
} }
CloseView = &view.View{
Measure: closes,
TagKeys: []tag.Key{keyCloseReason, keyErrorCode},
Aggregation: view.Count(),
}
) )
// DefaultViews collects all OpenCensus views for metric gathering purposes // DefaultViews collects all OpenCensus views for metric gathering purposes
@@ -53,6 +63,7 @@ var DefaultViews = []*view.View{
ConnectionsView, ConnectionsView,
LostPacketsView, LostPacketsView,
SentPacketsView, SentPacketsView,
CloseView,
} }
type tracer struct{} type tracer struct{}
@@ -117,7 +128,33 @@ func (t *connTracer) StartedConnection(local, _ net.Addr, _ logging.VersionNumbe
) )
} }
func (t *connTracer) ClosedConnection(logging.CloseReason) {} func (t *connTracer) ClosedConnection(r logging.CloseReason) {
var tags []tag.Mutator
if timeout, ok := r.Timeout(); ok {
tags = []tag.Mutator{
tag.Upsert(keyCloseReason, timeoutReason(timeout).String()),
tag.Upsert(keyCloseRemote, "false"),
}
} else if _, ok := r.StatelessReset(); ok {
tags = []tag.Mutator{
tag.Upsert(keyCloseReason, "stateless_reset"),
tag.Upsert(keyCloseRemote, "true"),
}
} else if errorCode, remote, ok := r.ApplicationError(); ok {
tags = []tag.Mutator{
tag.Upsert(keyCloseReason, "application_error"),
tag.Upsert(keyErrorCode, errorCode.String()),
tag.Upsert(keyCloseRemote, fmt.Sprintf("%t", remote)),
}
} else if errorCode, remote, ok := r.TransportError(); ok {
tags = []tag.Mutator{
tag.Upsert(keyCloseReason, "transport_error"),
tag.Upsert(keyErrorCode, errorCode.String()),
tag.Upsert(keyCloseRemote, fmt.Sprintf("%t", remote)),
}
}
stats.RecordWithTags(context.Background(), tags, closes.M(1))
}
func (t *connTracer) SentTransportParameters(*logging.TransportParameters) {} func (t *connTracer) SentTransportParameters(*logging.TransportParameters) {}
func (t *connTracer) ReceivedTransportParameters(*logging.TransportParameters) {} func (t *connTracer) ReceivedTransportParameters(*logging.TransportParameters) {}
func (t *connTracer) SentPacket(hdr *logging.ExtendedHeader, _ logging.ByteCount, _ *logging.AckFrame, _ []logging.Frame) { func (t *connTracer) SentPacket(hdr *logging.ExtendedHeader, _ logging.ByteCount, _ *logging.AckFrame, _ []logging.Frame) {

View File

@@ -65,3 +65,16 @@ func (t packetType) String() string {
panic("unknown packet type") panic("unknown packet type")
} }
} }
type timeoutReason logging.TimeoutReason
func (r timeoutReason) String() string {
switch logging.TimeoutReason(r) {
case logging.TimeoutReasonHandshake:
return "handshake_timeout"
case logging.TimeoutReasonIdle:
return "idle_timeout"
default:
panic("unknown timeout reason")
}
}