Skip to content

Commit

Permalink
add a metric for closed connections
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jul 20, 2020
1 parent f10894a commit 8a21cf7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
39 changes: 38 additions & 1 deletion metrics/metrics.go
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"context"
"fmt"
"net"
"time"

Expand All @@ -17,6 +18,7 @@ var (
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)
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
Expand All @@ -26,6 +28,9 @@ var (
keyEncryptionLevel, _ = tag.NewKey("encryption_level")
keyPacketLossReason, _ = tag.NewKey("packet_loss_reason")
keyPacketType, _ = tag.NewKey("packet_type")
keyCloseReason, _ = tag.NewKey("close_reason")
keyCloseRemote, _ = tag.NewKey("close_remote")
keyErrorCode, _ = tag.NewKey("error_code")
)

// Views
Expand All @@ -45,13 +50,19 @@ var (
TagKeys: []tag.Key{keyPacketType},
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
var DefaultViews = []*view.View{
ConnectionsView,
LostPacketsView,
SentPacketsView,
CloseView,
}

type tracer struct{}
Expand Down Expand Up @@ -106,7 +117,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) ReceivedTransportParameters(*logging.TransportParameters) {}
func (t *connTracer) SentPacket(hdr *logging.ExtendedHeader, _ logging.ByteCount, _ *logging.AckFrame, _ []logging.Frame) {
Expand Down
13 changes: 13 additions & 0 deletions metrics/types.go
Expand Up @@ -65,3 +65,16 @@ func (t packetType) String() string {
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")
}
}

0 comments on commit 8a21cf7

Please sign in to comment.